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; }
标签:status,初始化,name,driver,C++,列表,面向对象,Rider,string 来源: https://www.cnblogs.com/RGBTH/p/13528717.html