Detailed C + + constructors

Source: Internet
Author: User

Earlier, we introduced C + + as an object-oriented high-level programming language, knowing the object-oriented features. Today began to formally learn C + +.

We know that the core of C + + is the function of the program, structured, then structured, it must have classes.

A class in which there are variables and methods (functions). The variables inside the class, which we call a member variable, are called member functions.

Because of the characteristics of C + + encapsulation, generally by default, the member variables and member methods inside the class are private, that is, the external object is inaccessible, in which case the class has no effect, so we generally set the member variable to private, the member method is set to public, Public member methods are also known as interfaces.

constructor, which is the same as the class name, has no return value and is responsible for initializing member variables.

Access the function to get the value of the member variable

Set a function, set (modify) the value of a member variable

Destructors, which are enabled when the object is disposed, start with a ~, and have the same class name.

In general, even if we do not write the constructor, the system will also add a constructor to us, the constructor is called the default constructor, he does not have any action on member variables. The constructor we write will mask the default constructor of the system. However, constructors can be overloaded.

The so-called overloads are the same function names, different parameter lists (variable types, or different numbers of variables)

The default constructor is divided into two types, with no arguments to the constructor, with a default value constructor with parameters.

constructors without parameters, which is the system-generated constructor, does not have any action on member variables. However, we have declared that the system will not generate a default constructor.

A constructor with a parameter with a default value, which can contain many cases, with no value, with one value, or more values.

1234567891011121314151617181920212223 classThing {private:        intx,y;public:    //Thing(int a=0,int b=0);    Thing ();    Thing (inta,intb);};classThing {    Thing (){}//默认构造函数        Thing (inta,)    {        x=a;    }//重载的构造函数    Thing (inta,intb)    {        x=a,y=b;    }//带有默认值得构造函数    //Thing (int a,int b); 这条语句和上面的语句不能同时存在,因为带默认值的构造参数包含了上面的情况,不复合重载的规则}

Of course, the constructor also has an expression, that is, the initialization list, with ":" After the beginning of the member variable (parameter value)

Such as:

1   Thing (inta,nt b):x(a),y(b)P{}

The initialization list must be used in the following cases:

A const member in a class, a reference member in a class, no default constructor in a parent class, and initialization of a parent class member in a subclass must use the initialization list.

There is also a small knowledge point, which is the const-modified object:

In C, we know that const-modified variables cannot be changed once they are initialized, so what happens in C + +?

All content in C + + is divided into two classes, object Const decorated object Ordinary object

Member Const decorates ordinary member

Method Const Modification Method non-const method

Where a non-const decorated object can call a const member function or call an ordinary member function

Const objects can only call const member functions

The const member function has only read-only permissions, so it is not possible to modify the value of a member variable

There are three ways in which the function is defined:

1, the function return value is a const reference , the function return value cannot be modified (cannot do the left value)

2, the function parameter is the const reference function body cannot modify the value of the parameter int func (Constint &)

3. function modifier member function (only for class definition) int function (int) const; Member variables cannot be modified in the body (commonly used with print output functions, protect permissions)

Const-Modified function: This function can only be a class member function

1. You cannot modify the value of a member variable in a const member function

2. Only member functions of a class can be modified by const

3, the function body cannot call the object of the function

4. Only class member functions can be decorated as const member functions

5. The const member function can only call the const member function in this class

6. In a class, a member function in a class has a const version and a non-const version

Detailed C + + constructors

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.