New Features of C ++ (constructor/destructor/common type)

Source: Internet
Author: User
Tags getcolor

Constructor: A constructor used to initialize an object when an object is created. It is called a constructor:

Statement:

Class class name {

Function Name ();

};

The name of the constructor. It must be the same as the class name. If no default constructor exists in the class, the system automatically creates a constructor. Feel the constructor through code:

Examples of casual footwear:

 

# Include <iostream> using namespace STD; Class cshoes // declare class cshoes {public: cshoes (); // default constructor cshoes () int getsize (); Private: int size ;}; cshoes: cshoes () {// default constructor implementation size = 40; // initialize data member size} int cshoes: getsize () {return size;} int main () {cshoes shoes; // defines a cshoes object shoes; cout <"The initial size of the shoes object is:" <shoes. getsize () <Endl; return 0 ;}

When defining the shoes object of the cshoes class, the system automatically calls the default constructor (from the code, we can see that the constructor is not called), and sets the data member size of the object to the initial value of 40.

 

Now, if you want to create casual shoes based on the size and color required by the user, you can set parameters for the class constructor. A class can have multiple constructors, which is equivalent to overloading the default constructor.

Constructors with Parameters

Statement:

Class Name

{

Constructor name (parameter table );

};

Through examples, we can feel: casual footwear that meet new needs:

 

# Include <iostream> # include <string. h> using namespace STD; Class cshoes {public: cshoes (); // default constructor cshoes (int s, char * strcolor ); // The constructor with parameters int getsize (); char * getcolor (); Private: int size; char color [10] ;}; cshoes: cshoes () {size = 40; strcpy (color, "Red") ;}cshoes: cshoes (int s, char * strcolor) {strcpy (color, strcolor); size = s ;} int cshoes: getsize () {return size;} Char * cshoes: getcolor () {return color;} int main () {cshoes shoes1; // define a cshoes object shoes; cshoes shoes2 (38, "White"); // call the constructor cout based on the number of parameters <"The initial size of the shoes object is: "<shoes1.getsize () <" color: "<shoes1.getcolor () <Endl; cout <" Initial shoe size: "<shoes2.getsize () <"color:" <shoes2.getcolor () <Endl; return 0 ;}

Constructor with default parameters

Cshoes (int s, char * strcolor = "white ");

 

Object "clone" --- copy constructor

After the demand comes out, large-scale production is required. If you customize each one, it is a waste of time. To save costs, you need to create new objects based on the created objects.

The function of copying constructor is called during initialization to copy the values of data members of a known object to another similar object being created.

Format:

Class class name {

Copy the const name (const Class Name & reference name );

};

The copy constructor does not specify the return type and has only one parameter, which is a reference to an object. The function names are the same, and the const In the parameter table is the referenced object in the function, cannot be modified. If the copy constructor is not declared in the class, the compilation system automatically generates the copy constructor.

Experience through examples

# Include <iostream> # include <string. h> using namespace STD; Class cshoes {public: cshoes (); // default constructor cshoes (int s, char * strcolor ); // const cshoes (const cshoes & S); // copy the constructor int getsize (); char * getcolor (); Private: int size; char color [10] ;}; cshoes: cshoes () {size = 40; strcpy (color, "Red") ;}cshoes: cshoes (INT s, char * strcolor) {strcpy (color, strcolor); size = s;} cshoes: cshoes (const cshoes & S) {// copy constrcpy (color, s. color); size = S. size;} int cshoes: getsize () {return size;} Char * cshoes: getcolor () {return color;} int main () {cshoes shoes1; // define a cshoes object shoes; cshoes shoes2 (38, "White"); // call the constructor cshoes shoes3 (shoes2) based on the number of parameters ); // use the shoes2 object to initialize shoes3. The shoes3 object is completely a clone cout of the shoes2 object. <"The initial size of the shoes2 object is:" <shoes1.getsize () <"color:" <shoes1.getcolor () <Endl; cout <"Initial shoe size:" <shoes2.getsize () <"color: "<shoes2.getcolor () <Endl; cout <" shoes2.getcolor (): "<shoes3.getsize () <" color: "<shoes3.getcolor () <Endl; return 0 ;}

Essence: use one known object to initialize another object

It is generally used in three cases:

1. explicitly indicates that when one object is used to initialize another object;

2. When the object is passed to the form parameter as the real parameter of the function, you need to call the copy constructor;

3. When an object is returned as a function, you need to call the copy function.

 

Destructor

Clean up object garbage. Before the object is deleted, clean up the memory occupied by the object. The function is to release an object.

Statement format:

Class Name

{

~ Destructor name ();

};

There are two differences from constructors:

1. Only one destructor can exist in a class, unlike constructors that can be overloaded;

2. destructor are generally automatically called by the system, but can also be called proactively. constructors cannot be called proactively.

If no destructor exists in a function, the compilation system automatically generates a default destructor, which is an empty function.

 

Class

Regular objects: Objects of the regular type defined by the class

Class Name const Object Name

Const class name Object Name

After the definition is complete, Initialization is required and cannot be modified after the definition is complete. Otherwise, a compilation error will occur.

 

Common member functions:

Purpose: It is provided to call a common object. If it is not a common member function, a compilation error occurs when it is called.

Statement format:

Data Type Function Name (parameter table) const;

Let's use an example to feel the common objects and common member functions:

# Include <iostream> # include <string. h> using namespace STD; Class cconstant {public: cconstant (int x, int y); void disp () const; private: int m_x; int m_y;}; cconstant: cconstant (int x, int y) {m_x = x; m_y = y;} void cconstant: disp () {cout <"x =" <m_x <"*** y =" <m_y <Endl;} void cconstant: disp () const {cout <"x =" <m_x <"$ Y =" <m_y <Endl ;}int main () {cconstant T1 (5, 10 ); const cconstant T2 (8, 30); t1.disp (); t2.disp (); // call the common member function return 0 through a common object ;}

The reload of a common member function can also be carried out through the function return value type. For example, in the cconstant class, there are two member functions with the same name disp, which is a function overload based on the type of the function return value. The first is the void type, and the second is the void plus the const type.

 

Regular member data

The initialization of member data in a class can only be performed through the member initialization list. In a program, we can see that:

# Include <iostream> # include <string. h> using namespace STD; Class cconstant {public: cconstant (int n); void disp (); Private: const Int & rnum; // const int nnum; // regular data member}; cconstant: cconstant (int n): nnum (N), rnum (nnum) {// initialize the regular data member through the initialization list} void cconstant:: disp () {cout <nnum <":" <rnum <Endl;} int main () {cconstant T1 (25), T2 (88 ); t1.disp (); t2.disp (); // call the common member function return 0 through a common object ;}

Data member initialization list:

Cconstant: cconstant (int n): nnum (N), rnum (nnum ){

} It first initializes nnum with the value of the form parameter n, and then uses rnum to reference nnum, which is equivalent:

Nnum = N;

Rnum = & nnum

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.