"C + + language programming basics" learning and objects

Keywords: C C++ C#

  1. 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   
  2. 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;  
    }  

  3. 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   
  4. Because static data members belong to the whole class, special static function members need to be defined when accessing to process static data members   
  5. 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.   
  6.   
    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;  
    }  

  7. Friends of class:   
  8. 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.   
  9. Friend function:   
  10. 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   
  11. Function: increase flexibility, so that programmers can make reasonable choices in encapsulation and rapidity. Members in an object must be accessed by object name.   
  12.   
  13. 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));  
    }  

  14. Friend class:   
  15. 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.   
  16. 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   
  17.   
  18. //Class E is a friend of class A, but class A is not a friend of class E. friends are unidirectional   
  19. 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();  
    };  

  20. 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.   
  21. Constant object: must be initialized and cannot be updated.   const   Class name   Object name  ,  
  22. Constant member: class member decorated with const:      Constant data members (object members decorated with const) and constant function members (parameter tables decorated with const)   
  23. Constant reference: referenced objects cannot be updated.        const    type specifier   & Reference name   
  24. Constant group: array elements cannot be updated                   type specifier    const    Array name [size]   
  25. Constant pointer: a pointer to a constant   
  26.   
  27. 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:   
  28. 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.   
  29. const keyword can be used to distinguish overloaded functions. Only its constant member functions can be called through a constant object.   
  30. 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;  
    }  

  31. Constant data members: constant data members must be initialized and cannot be updated   
  32. 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;  
    }  
  33. 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   
  34. If you use const when declaring a reference, the declared reference is a constant reference.   
  35. Objects referenced by constant references cannot be updated.   
  36. If you use constant references as formal parameters, you won't accidentally change the arguments. The frequently cited declaration forms are as follows:   
  37. const    type specifier   & Reference name;   
  38.   
    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;  
    }  

Posted by elentz on Wed, 06 Oct 2021 05:57:32 -0700