Constructor and member variable initialization

1 Why do you need to define a constructor?

The main purpose of the constructor is to initialize the member variables to ensure that the class object has a good initial state.

2 Initialization of member variables by constructor

Next, we will improve the several constructors mentioned above so that they can effectively complete the initialization of member variables.

For this, we also need to add corresponding member variables.

struct Student
{
    //默认构造函数 default constructor
    Student() {};
    //非默认构造函数,给m_age成员初始化
    Student(int age):m_age(age) {};
    //非默认构造函数,给两个成员变量初始化
    Student(int age, bool sex):m_age(age), m_sex(sex){};
private:
    int m_age;//年龄
    bool m_sex;//性别
};

int main()
{
    Student stu1;//调用默认构造函数
    Student stu2(10);//调用带一个整形参数的构造函数
    Student stu3(10, true);//调用两个参数的那个构造函数

    return 0;
}

Note that using constructors to initialize member variables is written like this:

In the constructor of the class, member variables are initialized in the form of a constructor parameter list, for example: member variable 1 (parameter 1), member variable 2 (parameter 2), . . . At the same time, you need to use a colon to mark it before the constructor body.

For example, suppose there is a class named Student with two member variables age and sex, and the definition of the constructor is as follows:

Student(int age, bool sex) : m_age(age), m_sex(sex) {}

It should be noted that the colon is essential in this grammatical structure.

Then, through such a constructor definition, the member variables are indeed initialized.

From the above figure we can see that:

1) The member variable m_name is a class type . If you initialize it in the constructor, m_name will use the value you initialized. If you don't initialize m_name in the constructor, it calls string's default constructor to initialize itself (string's default constructor constructs itself as an empty string "").

2) The member variable is a basic type , if you do not initialize it in the constructor, its value is a random value (for example, m_age=-858993460, m_sex=true).

Note: As for why m_sex still shows 204, I don't know. I guess the smallest address unit of a computer is a byte, and the bool type only occupies one bit of a byte. So the computer simply showed us the content of that byte (I guess, after all, this is not the content of C++).

3 Is the member variable not initialized without defining the constructor?

If no constructor is defined, the compiler will synthesize a default constructor for us.

The default constructor is of class type for member variables, and initializes by calling the default constructor of the class where the member variable is located; for member variables of basic type, C++ does not stipulate (you can think of it as a random value).

 

I hope the sharing in this issue can help everyone. If you need more learning materials, please click the link below!

C language/C++ learning materials icon-default.png?t=N5K3https://dersl.xet.tech/s/2mfOCf

Guess you like

Origin blog.csdn.net/xiangxin1030/article/details/131493990