#include <iostream>
using namespace std;
class Per
{
private:
string name;
int age;
double *height;
double weight;
public:
Per()
{
cout << "Per:无参构造函数" << endl;
}
Per(string name, int age, double height, double weight):name(name),age(age),height(new double(height)),weight(weight)
{
cout << "Per:有参构造函数" << endl;
}
Per(const Per &other):name(other.name),age(other.age),height(new double(*other.height)),weight(other.weight)
{
cout << "Per:拷贝构造函数" << endl;
}
~Per()
{
delete height;
height = nullptr;
cout << "Per:析构函数" << endl;
}
};
class Stu
{
private:
double score;
Per p1;
public:
Stu()
{
cout << "Stu:无参构造函数" << endl;
}
Stu(double score, Per p):score(score),p1(p)
{
cout << "Stu:有参构造函数" << endl;
}
Stu(const Stu &other):score(other.score),p1(other.p1)
{
cout << "Stu:拷贝构造函数" << endl;
}
~Stu()
{
cout << "Stu:析构函数" << endl;
}
};
int main()
{
Per p1;
Per p2("lili", 18, 188, 88);
Per p4 = p2;
cout << "-------------" << endl;
Stu s1;
cout << "-------------" << endl;
Stu s2(100, p2);
return 0;
}
20240830 作业
最新推荐文章于 2024-09-27 11:26:03 发布