cpp
复制代码
#include <iostream>
using namespace std;
class Per{
private:
string name;
int age;
double* height;
double* weight;
public:
Per(string name, int age, double height, double weight);
Per(Per &per);
~Per();
void show();
};
class Stu{
private:
double score;
Per p;
public:
Stu(double score,string name, int age, double height, double weight);
Stu(Stu &stu);
~Stu();
void show();
};
Per::Per(string name, int age, double height, double weight)
:name(name),age(age),height(new double(height)),weight(new double(weight)){
cout << "Per::有参构造函数" << endl;
}
Per::Per(Per &per)
:name(per.name),age(per.age),height(new double(*per.height)),weight(new double(*per.weight)){
cout << "Per::拷贝构造函数" << endl;
}
Per::~Per(){
delete height;
delete weight;
cout << "Per::析构函数" << endl;
}
void Per::show(){
cout << "name = " << name << "\tage = " << age << "\theight = " << *height << "\tweight = " << *weight;
}
Stu::Stu(double score,string name, int age, double height, double weight)
:score(score),p(name,age,height,weight){
cout << "Stu::有参构造函数" << endl;
}
Stu::Stu(Stu &stu):score(stu.score),p(stu.p){
cout << "Stu::拷贝构造函数" << endl;
}
Stu::~Stu(){
cout << "Stu::析构函数" << endl;
}
void Stu::show(){
p.show();
cout << "\tscore = " << score << endl;
}
int main()
{
Stu s(95.5,"zhang",18,175,120);
s.show();
Stu s1=s;
s1.show();
return 0;
}