#include <iostream>#include <string>using namespace std;/*** 定义类:Student* 数据成员:m_strName* 无参构造函数:Student()* 有参构造函数:Student(string _name)* 拷贝构造函数:Student(const Student& stu)* 析构函数:~Student()* 数据成员函数: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方式实例化对象*stu student *stu = new student; // 更改对象的数据成员为“慕课网” stu-> setName( "慕课网"); // 打印对象的数据成员 cout << stu->getName() << endl; delete stu; stu = NULL; return 0;}
1 回答
- 1 回答
- 0 关注
- 3668 浏览
添加回答
举报
0/150
提交
取消