C ++ Study Notes 3-class encapsulation (1), learning notes Encapsulation

Source: Internet
Author: User

C ++ Study Notes 3-class encapsulation (1), learning notes Encapsulation

Encapsulation:

1. encapsulate attributes and methods.

2. Access Control for attributes and methods.

 

Access Controller:

1. public: Members can be accessed throughout the program. public Members define class interfaces.

2. private: The member can be accessed by the member functions of the class, but cannot be accessed by the code using the class. The private part encapsulates the implementation details of the class.

3. protected: A member can be accessed by a class or a derived class, but cannot be accessed by code using this class.

 

Class constructor:

1. the constructor name is the same as the class name.

2. the constructor can have a parameter or no parameter during definition, but there is no declaration of any return type.

3. the constructor is automatically called when the variable is defined.

Class Test {public: Test () = default; // after a constructor is defined in the class, the system will not generate the default constructor. Test () = defualt can generate a new default constructor. Test (int num );};

Class destructor:

1. The name of the Destructor is the same as that of the class. Add ~ to the name header ~.

2. The Destructor has no parameters and no declaration of the return type.

3. The Destructor is automatically called when the object is destroyed.

class Test{public:    ~Test();      };

Class copy constructor:

Class Test {public: Test (const Test & obj); // a special constructor. An object is converted into another object, which also has a copy Depth Problem, the compiler provides a shortest copy };

When the copy constructor is called:

1. When one object of the class is used to initialize another object of the class (or reference), the system automatically calls the copy constructor to realize the copy assignment.

Test t1;Test t2(t1);Test t3 = t1;

2. If the form parameter of a function is a class object, when a function is called, the real parameter is assigned to the form parameter, and the system automatically calls the copy constructor.

3. When the return value of a function is a class object, the system automatically calls the copy constructor and returns an anonymous object.

The life cycle of an anonymous object only exists in one row. When no class variable is connected, the Destructor is called directly. When a class variable is connected, the anonymous variable is directly converted into a class variable.

 

Class combination:

Class A {public: A (int index) {this-> a = a;} private: int a;}; class B {public: B (int a1Init, int a2Init ): a1 (a1Init), a2 (a2Init), B (3) // class initialization list, including other classes (constructor with parameters) and const variables must be initialized using the class initialization list {;} private: A a1; A a2; const int B ;};

Class combination call sequence:

1. When a member variable in the class is an object of other classes, call the constructor of the member variable first. The Calling sequence is the same as the declared sequence, and then call the constructor of the class.

2. The Calling sequence of the Destructor is opposite to that of the corresponding constructor.

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.