1思维导图

cpp
#include <iostream>
using namespace std;
//设计一个per类
class Per
{
private:
string name;
int age;
double *weight;
double *high;
public:
//无参构造函数
Per()
{
weight = nullptr;
high = nullptr;
cout << "Per:: 无参构造函数" << endl;
}
//有参构造函数
Per(string name,int age,double weight,double high):name(name),age(age),weight(new double(weight)),high(new double(high))
{
cout << "Per::有参构造函数" << endl;
}
//拷贝构造函数
Per(const Per &other):name(other.name),age(other.age),weight(new double(*other.weight)),high(new double(*other.high))
{
cout << "拷贝构造函数" << endl;
}
//拷贝赋值函数
Per& operator=(const Per &other)
{
if(this!=&other)
{
name = other.name;
age = other.age;
weight = new double(*other.weight);
high = new double(*other.high);
}
cout << "Per::拷贝赋值函数" << endl;
return *this;
}
//析构函数
~Per()
{
delete weight;
delete high;
weight = nullptr;
high = nullptr;
cout << "Per::析构函数" << endl;
}
void show()
{
cout << "name = " << name << endl;
cout << "age = " << age << endl;
cout << "high = " << *high << endl;
cout << "weight = " << *weight << endl;
}
};
//Stu类
class Stu
{
private:
double score;
Per p1;
public:
Stu()
{
cout << "Stu::无参构造函数" << endl;
}
Stu(double score,string name ,int age,double wight,double high):score(score),p1(name,age,wight,high)
{
cout << "Stu::有参构造函数" << endl;
}
Stu& operator=(const Stu &other)
{
if(this!=&other)
{
score = other.score;
p1 = other.p1;
}
cout << "Stu::拷贝赋值函数" << endl;
return *this;
}
Stu(const Stu &other):score(other.score),p1(other.p1)
{
cout << "Stu::拷贝构造函数" << endl;
}
~Stu()
{
cout << "Stu::析构函数" << endl;
}
void show()
{
p1.show();
cout << "Stu::score = " << score << endl;
}
};
int main()
{
Stu s1;
Stu s2(99,"张三",18,200,160);
s1 =s2;
s1.show();
Stu s3 = s1;
return 0;
}