[C ++] class and object (construction and analysis), Object Construction

Source: Internet
Author: User

[C ++] class and object (construction and analysis), Object Construction
Class

Class is an abstraction and encapsulation mechanism. It describes a group of objects with the same attributes and behaviors and is the basic unit of code reuse.

Access Permissions of Class Members

One of the key features of object orientation is to hide data. The adoption mechanism is to set the access control permissions of class members. Class Members have three access permissions:

  • Public type: it is declared by the keyword public. Only public members can be accessed outside the class.
  • Private type: it is declared by the keyword private. A private member can only be accessed by the member functions of the class.
  • Protection Type: it is declared by protect. It is similar to a private member. The difference is that during the inheritance process, the protected member can be accessed by the function of the derived class member.

Note the member functions and class objects of the classification area.

In general, public members are the external performance of the class, while private members are the internal implementation of the class and do not want to be understood by the outside world.

Implementation of member functions

The data member of the class describes the characteristics of the object, and the member functions of the class determine the operation behavior of the object. A member function is an algorithm implementation part and the only way to operate encapsulated data. The implementation methods can be classified into in-class implementation and out-of-class implementation. The external implementation form is as follows:

Return Value Type Class Name: member function name (form parameter table)
{
Function body;
}
Class Object

Objects are class instances. The system does not allocate storage space for abstract classes, but allocates memory space for the objects. However, this memory space can only be used to store data members of an object. Its member functions do not store copies of each object.

Constructor

The creation of an object is more complex than the creation of common variables. It needs to write the initial values of data members along with the memory space allocated. A constructor is used to construct an object with specific values when an object is created.

The syntax format for declaring a constructor is as follows:

Public:
Class Name (<parameter table> );

A constructor is a special member function of a class. It has the same name as the class name and can have any type of parameters, but cannot have a return type. When a new object is created, the compiler automatically calls the constructor. Constructors can be compiled and provided by themselves. If they are not provided, the compiler automatically generates a default constructor without parameters (without any specific work ).

Overload constructors:

  The so-called overload constructor refers to the same constructor name, and its parameter table is different. Note: Avoid ambiguity when the constructor has default parameters.

Copy constructor:

A copy constructor is a special constructor used to copy objects. You can use a created object (specified by the copy constructor parameter) to initialize a similar object being created.

The syntax format for declaring a copy constructor is as follows:

Class Name
{
Public:
Class Name (Class Name & Object Name );
};

A copy constructor can have only one parameter and reference a similar object. Each class must have a copy constructor. If the function only assigns the value of the created object data member to the data member of the object being created, there is no need to display and define it, the compiler automatically generates a default copy paparazzi function in the preceding format.

Destructor

The role of the Destructor is almost the opposite to that of the constructor. When an object disappears, Or you use delete to delete an object created with new, the system will automatically call the class destructor for cleanup. After it is called, the object disappears and the corresponding memory space is released.

The syntax format for declaring an destructor is as follows:

Class Demo
{
Public:
Demo (<parameter table> );
~ Demo (void );
}

The Destructor has the same name as the class name, but adds "~ ". It does not have parameters or return values, but can be virtual functions. A class can only define one destructor, so the Destructor cannot be overloaded. The sequence in which the Destructor is called is the first to die out of the last created object.

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.