作业11.30
1.设计一个Per类,类中包含私有成员:姓名、年龄、指针成员身高、体重,再设计一个Stu类,类中包含私有成员:成绩、Per类对象p1,设计这两个类的构造函数、析构函数和拷贝构造函数。
#include <iostream>
using namespace std;
class Per
{
private:
string name;
int age;
double weight;
double height;
public:
Per()
{
// cout << "Per::无参构造函数" << endl;
}
Per(string name,int age,double weight,double height):name(name),age(age),weight(weight),height(height)
{
// cout << "Per::有参构造函数" << endl;
}
~Per()
{
// cout << "Stu::析构函数" << endl;
}
Per(const Per &other):name(other.name),age(other.age),weight(other.weight),height(other.height)
{
// cout << "Stu::拷贝函数" << endl;
}
void show()
{
cout << "Per name=" << name << endl;
cout << "Per age=" << age << endl;
cout << "Per weight=" << weight << endl;
cout << "Per height=" << height << endl;
}
};
class Stu
{
private:
double score;
Per p1;
public:
Stu()
{
// cout << "Stu::无参构造函数" << endl;
}
Stu(double score,string name,int age,double weight,double height):score(score),p1(name,age,weight,height)
{
// cout << "Stu::有参构造函数" << endl;
}
~Stu()
{
// cout << "Stu::析构函数" << endl;
}
Stu(const Stu &other):score(other.score)
{
// cout << "Stu::拷贝函数" << endl;
}
void show()
{
cout << "Stu score=" << score << endl;
p1.show();
}
};
int main()
{
Per p1;
Per p2("张三",28,56,176);
Per p3(p2);
p3.show();
Stu s1;
Stu s2(98,"李四",27,55,176);
Stu s3=s2;
s2.show();
return 0;
}
2.思维导图