Scope and visibility of identifiers

1.1 scope and visibility of identifiers

  • A scope is the area where an identifier is valid in the body of a program.
  • Scope classification:
    • Function prototype scope
    • Local scope (block scope)
    • The class scope
    • File scope
    • Namespace scope

1.2. Function primitive scope

  • Parameters in a function prototype whose scope begins at “(” and ends at “)”.

1.3. Local scope

  • Function parameters, identifiers declared in blocks;
  • Its scope starts at the declaration and is limited to the block.

1.4. Class scope

  • Members of a class have class scope, which includes the body of the class and the body of a function that is not an inline member function.
  • If a member of a class is accessed outside the scope of the class, either by the class name (accessing static members) or by the class’s object name, object reference, or object pointer (accessing non-static members).
  • File scope
  • Declarations that do not appear in any of the preceding scopes have file scope, such that the scope of declared identifiers begins at the declaration point and ends at the end of the file.

2. Lifetime of the object

2.1. Static lifetime

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

2.2. Dynamic survival

  • Objects declared in a block scope that are not static are dynamically lifetime objects (commonly known as local lifetime objects).
  • Begins when the program executes to the declaration point and ends at the end of the scope that names the identifier.

Class and static members

3.1. Static Data Members

  • Must be defined and initialized outside the class, using (::) to indicate the class to which it belongs.

Class friends

4.1. Friends of the class

Friends are a mechanism provided by C++ to break data encapsulation and data hiding. By declaring a module as a friend of another module, one module can refer to information that would otherwise be hidden in another module. You can use friend functions and friend classes.

Note: In order to ensure data integrity and the principle of data encapsulation and hiding, it is recommended to use friends as little as possible.

4.2 friend functions

A friend function is a non-member function, specified in the class declaration by the keyword friend, that has access to private and protected members by object name in the function body

What it does: Increases flexibility, allowing programmers to make reasonable choices in packaging and rapidity. Members in an object must be accessed by the object name.

Example 5-6 Use a friend function to calculate the distance between two points

#include <iostream>
#include <cmath>
using namespace std;
class Point { / / the Point class declaration
  public: // External interface
    Point(int x=0.int y=0) : x(x), y(y) { }
      int getX(a) { return x; }
      int getY(a) { return y; }
      friend float dist(Point &a, Point &b);
  private: // Private data member
      int x, y;
};
float dist( Point& a, Point& b) {
  double x = a.x - b.x;
  double y = a.y - b.y;
  return static_cast<float> (sqrt(x * x + y * y));
}
int main(a) {
  Point p1(1.1).p2(4.5);
  cout <<"The distance is: ";
  cout << dist(p1, p2) << endl;
  return 0;
}
Copy the code

4.3 friends

If a class is a friend of another class, all members of that class can access the private members of the other class.

Declarative syntax: Modifies the friend class name with friend in another class.

class A {
    friend class B;

public:
    void display(a) {
        cout << x << endl;
    }

private:
    int x;
};

class B {
public:
    void set(int i);

    void display(a);

private:
    A a;
};

void B::set(int i) {
    a.x = i;
}

void B::display(a) {
    a.display(a); };Copy the code

4.4 friends of a class are one-way

If class B is declared A friend of class A, member functions of class B can access the private and protected data of class A, but member functions of class A cannot access the private and protected data of class B.

5. Multi-file structures and precompilation commands

5.1 general organization structure of c++ programs

  • A project can be divided into multiple source files:
    • Class declaration file (.h file)
    • Class implementation file (.cpp file)
    • Class usage file (the.cpp file where the main method is located)
  • Use projects to combine files
// File 1, class definition, point.h
class Point { // Class definition
public:          // External interface
       Point(int x = 0.int y = 0) : x(x), y(y) { count++; }
       Point(const Point &p);
       ~Point() { count--; }
       int getX(a) const { return x; }
       int getY(a) const { return y; }
       static void showCount(a);          // Static function member
private:         // Private data member
       int x, y;
       static int count; // Static data member
};
 
// File 2, class implementation, point.cpp
#include "Point.h"
#include <iostream>
using namespace std;
 
int Point::count = 0;            // Initialize the static data member with the class name
 
Point::Point(const Point &p) : x(p.x), y(p.y) {
       count++;
}
 
void Point::showCount(a) {
       cout << " Object count = " << count << endl;
}
 
// File 3, main function, 5_10.cpp
#include "Point.h"
#include <iostream>
using namespace std;
 
int main(a) {
       Point a(4.5);      // Define object A whose constructor increases count by 1
       cout <<"Point A: "<<a.getX() < <","<<a.getY(a); Point::showCount(a);// Outputs the number of objects
       Point b(a);         // Define object b, whose constructor returns count increment by 1
       cout <<"Point B: "<<b.getX() < <","<<b.getY(a); Point::showCount(a);// Outputs the number of objects
       return 0;
}
Copy the code

5.2. External variables

Conditional compilation instruction#ifAnd #endif

#ifConstant expression
 // Compiles when constant expression is non-zerotext#endif. Conditional compilation instruction#else

  #ifConstant expression
     // Compiles when constant expression is non-zerotext1
#else
  // Compiles when constant expression is zerotext2
#endifConditional compilation instruction#elif

#ifConstant expression 1text1  // Compiles when constant expression 1 is non-zero
#elifConstant expression 2text2  // Compiles when constant expression 2 is non-zero
#elsetext3  // In other cases
#endifConditional compilation instruction#ifdefidentifierProcedures section1
#elseProcedures section2
#endif
Copy the code