#include <iostream>#include <string>using namespace std;/*** 定義類:Student* 數(shù)據(jù)成員:m_strName* 無參構(gòu)造函數(shù):Student()* 有參構(gòu)造函數(shù):Student(string _name)* 拷貝構(gòu)造函數(shù):Student(const Student& stu)* 析構(gòu)函數(shù):~Student()* 數(shù)據(jù)成員函數(shù):setName(string _name)、getName()*/class student{public: student() {? cout << "student()" << endl; } student(string _name)? {? m_student = _name; cout << "student("<<_name<<")"<< endl; } student(const student& stu)? { cout << "const student& stu" << endl; } ~student()? { cout << "~student()" << endl; } void setName(string _name) { m_student = _name; } string getName() { return m_student; }private: string m_student;};int main(void){ // 通過new方式實(shí)例化對(duì)象*stu student *stu = new student; // 更改對(duì)象的數(shù)據(jù)成員為“慕課網(wǎng)” stu-> setName( "慕課網(wǎng)"); // 打印對(duì)象的數(shù)據(jù)成員 cout << stu->getName() << endl; delete stu; stu = NULL; return 0;}
- 1 回答
- 0 關(guān)注
- 3703 瀏覽
添加回答
舉報(bào)
0/150
提交
取消