C ++ class and Object

Source: Internet
Author: User

C ++ class and Object
Concepts of classes and objectsI. Features of the class:1. It is only one type;
2. Data members in the class do not occupy the memory;
3. When defining data members;
4. Variables of this type cannot be defined;

Ii. Object Member1. public: Access members of this class and all objects;
2. protected: access this class and its subclass members;
3. private: Access members of this class;


Constructor and destructorI. Features of Constructor1. The function name must have the same name as the class defined for it. 2. No return value is returned. If void is incorrect before the constructor;
3. declared as a public function;
4. the system automatically calls the object when it is created;
5. Reload allowed. Multiple constructors can be defined;

Ii. Differences between constructors and other functions1. the name must be the same as the class name, while the general function cannot be the same as the class name;
2. No return value and void is not required for modification. Generally, void must be used to describe functions without return values;
3. An object is automatically called only when it is created. Generally, a function is not called when it is executed by a program;
4. When no constructor is defined, the compiler will provide a default constructor, but the general function will not;
Iii. Features of destructor1. There are no parameters and cannot be overloaded;
2. No return value;
3. Add a logical non-operator "~" before the class name;
Iv. Differences between the copy constructor and the constructor1. When a new object is initialized with a constant, the constructor is called instead of the copy constructor. 2. when an object is created, only one constructor and one copy constructor are called;


YouyuanI. Features of youyuan relationship1. The relationship between friends and meta cannot be passed. For example, Class B is A friend of Class A, Class C is A friend of Class B, and class C and Class A are not declared, there will be no relationship with friends;
2. The relationship between friends and Meta is one-way. For example, if Class B is declared as A friend of Class A, member functions of Class B can access the private and protected data of Class, however, member functions of Class A cannot access and protect data of Class B;

Ii. Advantages of youyuan relationshipThis improves data sharing, enhances the connection between functions, classes, and classes, and greatly improves program efficiency.

Iii. disadvantages of youyuan relationshipData Hiding and Data encapsulation are damaged, resulting in poor maintainability of the program.


Memory space distribution of ObjectsI. Release of object memory space1. Global objects are released when the program ends; 2. Local objects are released when the function call ends;
3. dynamic objects must be released using the delete statement;
4. The object member function is automatically released when the lifecycle of all objects ends;

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.