定义一个Student类,包含一个私有数据成员(string name)定义无参构造函数......
定义一个Student类,包含一个私有数据成员(string name)定义无参构造函数,有参构造函数,拷贝构造函数,析构函数以及对name修改的函数,在每个构造函数和析构函数中添加输出语句,来体现调用。在main函数中实现实例化student对象,并访问相关函数。请路过的朋友给个建议,实在不会。
程序代码:
#include<cstdio>
#include<string>
#include<iostream>
using namespace std;
class Student {
public:
Student(string tName="null"); //构造函数,默认名字为null(不是NULL)
Student(Student& t); //复制构造函数,利用当前类中的成员变量附给同类实例
void Print(); //打印函数
private:
string name; //私有成员
};
Student::Student(string tName) { //以下都是函数的定义
name=tName;
}
Student::Student(Student& t) {
t.name=name;
}
void Student::Print() {
cout<<name;
}
int main() {
Student t("xiaohong"); //类的初始化,当函数的参数有默认值是,传入新的内容将会以新的内容操作,不传入默认以默认值操作。“("xiaohong")”可写可不写
t.Print();
return 0;
}