- Lifetime of variable: static keyword modifies a local variable. The local variable has a global lifetime, but the local variable is visible. If it leaves the scope, it will not be released
-
int i = 1;//i is a global variable with static lifetime. void other() { static int a = 2; static int b; //a. B is a static local variable with global life and local visibility //Is initialized only the first time the function is entered int c = 10; //C is a local variable with dynamic survival //Initializes each time a function is entered a += 2; i += 32; c += 5; cout << "---OTHER---\n"; cout << "i: " << i << " a: " << a << " b: " << b << " c: " << c << endl; b = a; } int main(){ static int a;//Static local variables, with global lifetime and local visibility. int b = -10;//b. C is a local variable with dynamic survival. int c = 0; cout << "---MAIN---\n"; cout << "i: " << i << " a: " << a << " b: " << b << " c: " << c << endl; c += 8; other(); cout << "---MAIN---\n"; cout << "i: " << i << " a: " << a << " b: " << b << " c: " << c << endl; i += 10; other(); return 0; }
- The static data member of a class is declared with the keyword static. It belongs to the whole class and does not belong to an object. It is declared within the class and defined and initialized outside the class
- Because static data members belong to the whole class, special static function members need to be defined when accessing to process static data members
- Out of class code can use class names and scope operators to call static member functions. Even so, you can use objects to access static function members. If you access non static members, you should access them through objects.
-
class Point {//Point Class definition public: Point(int x = 0, int y = 0) :x(x), y(y) {//Constructor //Count is accumulated in the constructor, and all objects maintain the same count count++; } Point(Point &p) { //copy constructor x = p.x; y = p.y; count++; } ~Point() { count--; } int getX() { return x; } int getY() { return y; } static void showCount() { //Static function members output static data members cout << " Object count= " << count << endl; } private: int x, y; static int count; //Static data member declaration, used to record the number of points }; int Point::count = 0;//Static data member definition and initialization, qualified by class name int main(){ Point::showCount(); Point a(4, 5); //Define object a and its constructor will increase count by 1 cout << "Point A: " << a.getX() << ", " << a.getY(); //a.showCount();// Number of output objects Point::showCount(); //a.showCount(); Point b(a);//Using the copy construct to define object b, its constructor will increase count by 1 cout << "Point B: " << a.getX() << ", " << a.getY(); //b.showCount();// Number of output objects Point::showCount(); return 0; }
- Friends of class:
- Friends can destroy the mechanism of data encapsulation and data hiding. You can use friend functions and friend classes. In order to ensure the integrity of data and the principle of data encapsulation and hiding, it is recommended not to use friends or less.
- Friend function:
- Friend function is a non member function modified by the keyword friend in the class declaration. It can be accessed through the object name in its function body private And protected members
- Function: increase flexibility, so that programmers can make reasonable choices in encapsulation and rapidity. Members in an object must be accessed by object name.
-
class Point { //Point class declaration public: //External interface Point(int x = 0, int y = 0) : x(x), y(y) { } int getX() { return x; } int getY() { return y; } friend float dist(Point &a, Point &b); //Declare friend function private: //Private data member int x, y; }; float dist(Point& a, Point& b) { //friend function double x = a.x - b.x; double y = a.y - b.y; return static_cast<float>(sqrt(x * x + y * y)); }
- 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. Declaration syntax: use the friend modifier to describe the friend class name in another class.
- Class E in the code is a friend of class A, but class A is not a friend of class E, and the friend is one-way. All members of class E can access private members of class A
- //Class E is a friend of class A, but class A is not a friend of class E. friends are unidirectional
-
class A { friend class E; //Friend class declaration public: void display() { cout << x << endl; } private: int x; }; class E { public: void set(int i); void display(); private: A a; }; void E::set(int i) { a.x = i; } void E::display() { a.display(); };
- Constant type: data that needs to be shared and prevented from changing should be declared as constant type (decorated with const), and member functions that do not change the object state should be declared as constant functions.
- Constant object: must be initialized and cannot be updated. const Class name Object name ,
- Constant member: class member decorated with const: Constant data members (object members decorated with const) and constant function members (parameter tables decorated with const)
- Constant reference: referenced objects cannot be updated. const type specifier & Reference name
- Constant group: array elements cannot be updated type specifier const Array name [size]
- Constant pointer: a pointer to a constant
- Constant objects can only call constant functions, ordinary objects can only call extraordinary functions, and constant functions do not update the data members of the object. Constant function description format:
- type specifier Function name (parameter table) const; Here, const is a component of the function type, so the const keyword should also be carried in the implementation part.
- const keyword can be used to distinguish overloaded functions. Only its constant member functions can be called through a constant object.
-
class A{ public: A(int i,int j) {x=i; y=j;} ... private: int x,y; }; A const a(3,4); //A is a constant object and cannot be updated It can also be written as const A a(3,4) class R { public: R(int r1, int r2) : r1(r1), r2(r2) { } void print(); void print() const; //Constant function member private: int r1, r2; }; void R::print() { cout << r1 << ":" << r2 << endl; } void R::print() const { //Constant function member cout << r1 << ";" << r2 << endl; } int main() { R a(5, 4); a.print(); //Call void print() const R b(20, 52); //Constant objects can also be written as R const b(20, 52); b.print(); //Call constant function void print() const return 0; }
- Constant data members: constant data members must be initialized and cannot be updated
-
class A { public: A(int i); //Constructor void print(); private: const int a; //Constant data member static const int b; //Static constant data member Declare global presence in class }; const int A::b = 10; //Static members are defined and initialized outside the class A::A(int i) : a(i) { } //Constructor void A::print() { cout << a << ":" << b << endl; } int main() { //Create objects a and b, and call constructors with 100 and 0 as initial values, //Assign initial values to the constant data members of the object through the initialization list of the constructor A a1(100), a2(0); a1.print(); a2.print(); return 0; }
- Constant reference: a read-only reference that implements one-way transfer of an object without modifying an argument. It can not only achieve efficient transfer, but also ensure that the argument object will not be modified
- If you use const when declaring a reference, the declared reference is a constant reference.
- Objects referenced by constant references cannot be updated.
- If you use constant references as formal parameters, you won't accidentally change the arguments. The frequently cited declaration forms are as follows:
- const type specifier & Reference name;
-
class Point { //Point class definition public: //External interface Point(int x = 0, int y = 0): x(x), y(y) {} int getX() { return x; } int getY() { return y; } friend float dist(const Point &p1, const Point &p2); //Friends often refer to formal parameter functions private: //Private data member int x, y; }; float dist(const Point &p1, const Point &p2) { //Constant reference parameter function double x = p1.x - p2.x; double y = p1.y - p2.y; return static_cast<float>(sqrt(x*x + y * y)); } int main() { //Main function const Point myp1(1, 1), myp2(4, 5); cout << "The distance is: "; cout << dist(myp1, myp2) << endl; return 0; }
"C + + language programming basics" learning and objects
Posted by elentz on Wed, 06 Oct 2021 05:57:32 -0700