C + + Fundamentals (vi)

Source: Internet
Author: User
Tags class definition function prototype gety

Scope and visibility of identifiers

    • A scope is an area in which an identifier is valid in the body of the program.
    • Scope classification
    1. Function prototype scope
    2. Local scope (block scope)
    3. Class scope
    4. File scope
    5. namespace scope (see Chapter 10th)

Function prototype scope

    • A parameter in a function prototype whose scope begins with "(", Ending with ")".
    • Example of a function prototype: Double area (double radius);

Local scope

    • The formal parameter of the function, the identifier declared in the block;
    • Its scope is limited to the block from the point of declaration.
    • Local scope examples

void Fun (int a) {

int b = A;

CIN >> B;

if (b > 0) {

int C;

......

}

}

Class scope

    • A member of a class has a class scope, and its scope includes the body of the class body and the function of the non-inline member function.
    • If you access a member of a class outside of the class scope, you want to pass the class name (access to a static member), or the class's object name, object reference, object pointer (access non-static member).

File scope

    • Declarations that do not appear in the preceding scopes have a file scope, so that the declared identifier begins at the declaration point and ends at the end of the file.

Visibility of

    • Visibility is a concept that is discussed from the perspective of references to identifiers
    • Visibility indicates what can be seen from the inner scope to the outer scope "see".
    • If the identity is visible somewhere, you can reference this identifier at that point.
    • If an identifier is declared in the outer layer and there is no declaration of the same identifier in the inner layer, the identifier is visible in the inner layer.
    • For two nested scopes, the outer scope identifier is not visible at the inner layer if an identifier with the same name as the outer scope is declared within the inner scope.

Lifetime of the object

Static Life Time

    • This lifetime is the same as the running period of the program.
    • Objects declared in a file scope have this lifetime.
    • Declare a static lifetime object inside a function, prefixed with the keyword static.

Dynamic life cycle

    • Objects declared in a block scope that are not static decorated are objects with dynamic lifetimes (custom local lifetime objects).
    • Begins when a program executes to a claim point, ending at the end of the scope at which the identifier is named.

Static data members of the class

    • Use the keyword static declaration
    • Shared for all objects of the class, static data members have a static lifetime.
    • must be defined and initialized outside of the class, using (::) to indicate the owning class

Case:

#include <iostream>using namespacestd;classPoint {//The point class definition Public://external InterfacePoint (intx =0,inty =0): x (x), Y (y) {//constructor Function//The count is incremented in the constructor, and all objects together maintain the same countcount++; } Point&AMP;P) {//copy Constructorx =p.x; Y=p.y; Count++; }    ~point () {count--; } intGetX () {returnx;} intGetY () {returny;} voidShowcount () {//output static data memberscout <<"Object count ="<< Count <<Endl; }Private://Private data members    intx, y; Static intCount//static data member declaration, which is used to record the number of points};intPoint::count =0;//static data member definition and initialization, using Class name qualificationintMain () {//Main functionPoint A (4,5);//define object A, whose constructor returns count 1cout <<"Point A:"<< A.getx () <<", "<<a.gety (); A.showcount (); //number of output objectsPoint B (a);//defines object B, whose constructor returns the count by 1cout <<"Point B:"<< B.getx () <<", "<<b.gety ();       B.showcount (); //number of output objects    return 0;}

In the above case, we need to record the number of objects in the point class, and it is not possible to store this variable in the private member of the class, because it is accessible only through the object, and the number is incremented by 1 for each object that declares a class. Therefore, we can define it as a static member variable of the class, which is common to all class objects. However, there is also a problem: when there is no object defining the class, can we know the number of class objects? If you want to access this variable through the object of the class, you need to declare an object first, but the object of the class is not yet declared, how do you solve the problem? This requires the use of the static function members of the class.

Static function members of a class

    • Out-of-class code can invoke a static member function using the class name and scope operators.
    • Static member functions are primarily used to handle static data members of the class (why?). ), you can call the static member function directly.
    • If you access a non-static member, you access it through the object.

Modify the above program slightly, as follows:

#include <iostream>using namespacestd;classPoint { Public: Point (intx =0,inty =0): x (x), Y (y) {count++;}//constructor FunctionPoint (Point &p) {//copy Constructorx =p.x; Y=p.y; Count++; }    ~point () {count--; } intGetX () {returnx;} intGetY () {returny;} Static voidShowcount () {cout<<"Object count ="<< Count <<Endl; }Private:    intx, y; Static intCount//static data member declaration, which is used to record the number of points};intPoint::count =0;//static data member definition and initialization, using Class name qualificationintMain () {Point A (4,5);//define object A, whose constructor returns count 1cout <<"Point A:"<< A.getx () <<", "<<a.gety ();       Point::showcount (); //number of output objectsPoint B (a);//defines object B, whose constructor returns the count by 1cout <<"Point B:"<< B.getx () <<", "<<b.gety ();       Point::showcount (); //number of output objects    return 0;}

We add a static member function to the function: Showcount (), then access the static member variable of the class without being accessed through the object, even if there is no object declaring the class. However, a static member function cannot access the object's data members directly , but rather to pass the object to a static function.

Friend of Class

    • Friend is a mechanism provided by C + + that destroys data encapsulation and data hiding.
    • By declaring a module as a friend of another module, a module can refer to the information that is hidden in another module.
    • You can make the UF meta-function and friend class.
    • In order to ensure the integrity of data and the principles of data encapsulation and concealment, it is recommended that you try not to use or use friends as much as possible.

Friend function

    • A friend function is a non-member function that is described by a keyword friend in a class declaration, which allows access to private and protected members through the object name in its function body
    • Role: Increases flexibility, allowing programmers to make reasonable choices in terms of encapsulation and speed.
    • Access to the members of the object must pass the object name, that is, the object of the class must be passed as a parameter to the function, generally we choose to pass the reference, because the program is less expensive, but the reference when the data is bidirectional, so it can be set as a constant reference when passing parameters, This will not allow the function to be passed in the parameters are modified to ensure the security of the data.

Friend class

    • If a class is a friend of another class, all members of this class can access the private members of the other class.
    • Declarative syntax: Use friend to decorate the description of a friend class name in another class.
    • A friend of a class is one-way: If declaring Class B is a friend of Class A, member functions of Class B can access private and protected data of Class A, but member functions of Class A cannot access the private, protected data of Class B.

C + + Fundamentals (vi)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.