1、指出程序的错误
student.h文件
#includeiostream
#includestring
usingnamespacestd;
classstudent
{
public:
voidstudent(stringname,floatg=0.0); //构造函数没有返回值类型
~student();
~student(inti);//析构函数不能带参数,不能重载
private:
stringsname;
floatgpa;
};
voidstudent::student(stringname,floatg=0.0)
//构造函数没有返回值类型,默认参数仅出现在函数声明中
{
cout"studentconstructorisrunning..."endl;
sname.append(name);
gpa=g;
}
student::~student()
{
cout"studentdestructorisrunning..."endl;
}
student::~student(inti)//析构函数不能带参数,不能重载
{
cout"studentNo."i"destructorisrunning..."endl;
}
main.cpp文件
#includeiostream
#include"student.h"
usingnamespacestd;
intmain()
{
students1("Venus",3.5);
students2("Jon");
students3(s2);
cout"StudentName:"s1.snameendl;
//sname和gpa都是私有成员数据,不能直接访问,应编写成员函数
cout"StudentGPA:"s1.gpaendl;
cout"StudentName:"s2.snameendl;
cout"StudentGPA:"s2.gpaendl;
cout"StudentName:"s3.snameendl;
cout"StudentGPA:"s3.gpaendl;
return0;
}
2、运行结果
OrignalC1constructorisrunning...
C1constructorwithparameterisrunning...
C2constructorisrunning...
C2clonerisrunning...
C2destructorisrunning...
C1destructorisrunning...
C1destructorisrunning...
C2destructorisrunning...
C1destructorisrunning...
C1destructorisrunning...
3、参考代码
请到官方网站下载。
北京治疗白癜风的最好医院北京治疗白癜风专家医院