#include <iostream>
using namespace std;
class Person
{
int *age;
string &name;
public:
// Person()
// {
// }
Person(int a,string &b):age(new int(a)),name(b)
{
cout << "Person的有参构造" << endl;
}
Person(const Person &other):age(new int(*(other.age))),name(other.name)
{
cout << "Person的拷贝构造函数" << endl;
}
~Person()
{
delete age;
cout << "Person的析构函数" << endl;
}
};
class Stu
{
double *score;
Person p1;
public:
// Stu():
// {
// }
Stu(double score,int a,string b):score(new double(score)),p1(a,b)
{
cout << "Stu的有参构造" << endl;
}
~Stu()
{
delete score;
cout << "Stu的析构函数" << endl;
}
Stu(const Stu &other):score(new double (*(other.score))),p1(other.p1)
{
cout << "Stu的拷贝构造函数" << endl;
}
// Stu &operator=(const Stu& other)
// {
// this->score=other.score;
// this->p1 = other.p1;
// }
void show();
};
void Stu::show()
{
cout << "score= " << score << endl;
cout << "*score= " << *score << endl;
// cout << "age= " << age << endl;
// cout << "*age= " << *age << endl;
// cout << "name= " << name << endl;
}
int main()
{
Stu stu(100.00,18,"zhangsan");
stu.show();
cout << "---------------------------------------" << endl;
Stu stu1 = stu;
stu1.show();
cout << "---------------------------------------" << endl;
// Stu stu2;
// stu2 = stu;
return 0;
}
效果图: