#include <iostream>
using namespace std;
class Per
{
private:
string name;
int age;
int* height;
int* weight;
public:
Per(){}
Per(string name,int age,int height,int weight):name(name),age(age),height(new int(height)),weight(new int(weight))
{
cout << "有参构造" << endl;
}
Per(const Per &other):name(other.name),age(other.age),height(new int(*(other.height))),weight(new int(*(other.weight)))
{
cout << "拷贝构造" << endl;
}
Per &operator=(const Per &other)
{
if(this != &other)
{
name = other.name;
age = other.age;
height = new int(*(other.height));
weight = new int(*(other.weight));
}
cout << "赋值拷贝" << endl;
return *this;
}
~Per()
{
cout << "析构函数" << endl;
}
};
class Stu
{
private:
double score;
Per p1;
public:
Stu(){}
Stu(double score,string name,int age,int height,int weight):score(score),p1(name,age,height,weight)
{
cout << "有参构造1" << endl;
}
Stu(const Stu &other):score(other.score),p1(other.p1)
{
cout << "拷贝构造1" << endl;
}
const Stu &operator=(const Stu &other)
{
if(this != &other)
{
score = other.score;
p1 = other.p1;
}
cout << "复制拷贝1" << endl;
return *this;
}
~Stu()
{
cout << "析构函数1" << endl;
}
};
int main()
{
Stu s1(99.99,"叶欢贤",18,60,180);
return 0;
}
Day11.7
最新推荐文章于 2024-07-15 13:32:43 发布