"Classes and objects" of C + +

Source: Internet
Author: User

concepts of classes and objects first, the characteristics of the class:1, just a type;
2, the data members in the class do not occupy memory;
3. When defining data members;
4. Variables of this class type cannot be defined;

II. member of the object1, public: access to such members and all objects;
2, Protected: Access to the class and its sub-class members;
3. Private: Access to the members of the class;


Constructors and Destructors first, the characteristics of the structure function1, the function name must and define his similar name;2, there is no return value, if it is wrong to see void before the constructor function;
3, the declaration is defined as a public function;
4, when the object is established by the system automatically call;
5, can be overloaded, define multiple constructors;

difference between a constructor and other functions1, the name must be the same as the class name, and the general function can not be the same as the class name;
2, no return value, and no void to decorate, and the general function does not return the value must be described by void;
3, when the object is created, it will be called automatically, and the general function is not invoked when the program executes to it;
4. When a constructor is not defined, the compiler provides a default constructor, and the general function does not;
the characteristics of the destructor1, without any parameters, can not be overloaded;
2, no return value;
3, precede the class name with a logical non-operator "~";
Four, the difference between the copy constructor and the constructor function1. When initializing a newly created object with a constant, the constructor is called and the copy constructor is not called;2, when the object is built, the constructor and copy constructors have and only one is called;


friend Yuan the characteristics of a friend relationship1, friend relationship is not transitive, such as: Class B is a friend of Class A, Class C is a friend of Class B, Class C and a if there is no declaration, there is no friend relationship;
2, friend relationship is one-way, such as: Declare Class B is a friend of Class A, the member function of Class B can access the private and protected data of Class A, but the member function of Class A cannot access the private and protected data of Class B;

advantages of the relationship between two and friendsIt improves the sharing of data, strengthens the relation between function and function, class and class, and greatly improves the efficiency of the program.

three, the disadvantage of the friend relationshipcorrupted data hiding and data encapsulation, resulting in poor program maintainability.


The memory space distribution of the object one, the release of the object memory space1. The global object is released at the end of the program;2. The local object is released at the end of the function call;
3, dynamic objects need to use DELETE statement release;
4. The object member function is automatically disposed at the end of all object life cycle;
   

"Classes and objects" of C + +

Related Article

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.