C++面向对象入门(十)初始化列表

C++在构造函数定义构造函数时, 使用初始化列表初始化对象

语法

 

类名(): 成员属性名(值), ..., {

}

注意事项:值可以为参数列表的参数

#include <iostream>
#include <string>

using namespace std;

/**
 * 语法
 *类名(): 成员属性名(值), ..., {
 *}
 *注意事项:值可以为参数列表的参数
 */
class Rider {
public:
    Rider(): name("Zero One"), status("Rising Hopper"), driver("飞电或人") {

    }
    Rider(string name, string status, string driver): name(name), status(status), driver(driver) {
        /*this->name = name;
        this->status = status;
        this->driver = driver;*/
    }

    ~Rider() {
        cout << "name: " << name << " status: " << status << " driver: " << driver << endl;
    }

private:
    string name;
    string status;
    string driver;
};

void test1() {
    Rider zeroOneRH;
    Rider zeroOneBs("Zero One", "Biting shark", "飞电或人");
}
int main() {
    test1();
    system("pause");

    return 0;
}

 

上一篇:IDEA Rider打开一个外部文件管理器


下一篇:Rider Express expected