作业:定义一个Person类,包含私有成员,int *age,string &name,一个Stu类,包含私有成员double *score,Person p1,写出Person类和Stu类的特殊成员函数,并写一个Stu的show函数,显示所有信息。
#include <iostream>
using namespace std;
class Person
{
int *age;
string &name;
public:
Person(int age, string name):age(new int(age)), name(name)
{
cout << "per的构造函数" << endl;
}
int *get_age();
string get_name();
};
int *Person::get_age()
{
return age;
}
string Person::get_name()
{
return name;
}
class Stu
{
double *score;
Person p1;
public:
~Stu()
{
cout << "析构函数" << endl;
}
Stu(double score, int age, string name):score(new double(score)),p1(age,name)
{
cout << "stu的构造函数" << endl;
}
void show();
};
void Stu::show()
{
cout << "score= " << *score << endl;
cout << "age= " << *p1.get_age() << endl;
cout << "name= " << p1.get_name() << endl;
}
int main()
{
Stu s1(88.8, 67, "嗨嗨嗨");
s1.show();
return 0;
}
思维导图