Teach you how to distinguish between constructors, ordinary member functions, copy constructors and destructors

Table of contents

1. Constructor

2. Ordinary member functions

3. Copy constructor

Fourth, the destructor


In C++, constructors, ordinary member functions, copy constructors, and destructors are several types of class member functions.

1. Constructor

Constructors are used to create and initialize objects with the same function name as the class name and no return type. When an object is instantiated, the constructor is automatically called and the member variables of the object are initialized. For example, here is an example of a simple constructor:

class Person { // 定义类Person
public:
    Person() { // 默认构造函数,初始化age为0,name为"Unknown"
        age = 0;
        name = "Unknown";
    }

    Person(int a, string n) { // 带参数的构造函数,初始化age和name
        age = a;
        name = n;
    }

private:
    int age; // 成员变量age
    string name; // 成员变量name
};

In the above code, Person()it is the default constructor, Person(int a, string n)which is a constructor with parameters. When instantiating Personan object, if no parameters are passed, the constructor is called by default Person(); if parameters are passed, Person(int a, string n)the constructor is called.

2. Ordinary member functions

An ordinary member function is an ordinary function that can access member variables and member functions in the class, but does not contain the static keyword. For example, here is an example of a simple ordinary member function:

// 定义了一个名为 Person 的类
class Person {
public:
    // 定义了一个公有的成员函数 setName,它有一个参数 n,类型为 string
    void setName(string n) {
        // 在成员变量 name 中存储参数 n 的值
        name = n;
    }

private:
    // 定义了一个私有的成员变量 name,类型为 string
    string name;
};

The preceding code defines a Personclass named , which has a public member function setNamefor setting namethe value of a member variable of the class. A member variable is defined in the private member of this class nameto store Personthe name of the object.

3. Copy constructor

The copy constructor is used to copy an object, its function name is the same as the class name, and the parameter is a constant reference pointing to the object of this class. The function of the copy constructor is to initialize a new object with the value of the existing object when creating a new object. For example, here is an example of a simple copy constructor:

class Person {                      // 定义 Person 类
public:
    Person(const Person& other) {   // 拷贝构造函数,参数为指向该类对象的常引用
        age = other.age;            // 复制现有对象的 age 成员变量值
        name = other.name;          // 复制现有对象的 name 成员变量值
    }

private:
    int age;                        // 年龄成员变量
    string name;                    // 姓名成员变量
};

The above code defines a Personclass named , which includes a copy constructor and two private member variables. The copy constructor is used to copy an Personobject, and its parameter is a constant reference pointing to the object of this class. When constructing a new object, the copy constructor copies the member variable values ​​of the existing object and assigns them to the corresponding member variables of the new object. ageThe and namemember variables represent the person's age and name, respectively.

Fourth, the destructor

A destructor is a function used to destroy an object, whose function name is the same as the class name, but starts with ~, and has no return type. The destructor is automatically called at the end of the object's life cycle to clean up the resources used by the object. For example, here is an example of a simple destructor:

class Person {    // 声明一个类Person
public:
    ~Person() {   // 定义析构函数,以 ~ 开头,没有返回类型
        cout << "Person object is destroyed" << endl;  // 输出一条消息,表示对象被销毁
    }

private:
    int age;      // 声明一个私有成员变量age
    string name;  // 声明一个私有成员变量name
};

In the above code, ~Person()it is a destructor. When Personthe object is destroyed, this function will be called automatically, and a message of destroying the object will be output.

Guess you like

Origin blog.csdn.net/qq_50635297/article/details/130030710