#include <iostream>
using namespace std;
class Stu
{
public:
string name;
int age;
int score;
int *high;
Stu(string name, int age, int score, int *high):name(name),age(age),score(score),high(high)//构造函数
{
cout << "构造" << endl;
}
~Stu()//析构函数
{
delete high;
cout << "析构" << endl;
}
Stu(Stu &s):name(s.name),age(s.age),score(s.score),high(s.high)
{
cout << "拷贝构造" << endl;
this->high = new int(*(s.high));
}
void show()
{
cout << name << age << score << *high << endl;
}
};
int main()
{
int *p = new int(10);
*p = 2;
Stu *s = new Stu ("hh",1,100,p);
s->show();
Stu* s1 = new Stu (*s) ;
s1->show();
delete s;
return 0;
}
【C++】类,构造函数,析构函数,拷贝构造函数
最新推荐文章于 2024-06-24 21:38:41 发布