2023-06-07作业
类中特殊成员函数:构造函数、析构函数、拷贝构造函数、拷贝赋值函数的使用和实现。
#include <iostream>
using namespace std;
class Stu
{
string name;
int age;
int score;
int *high;
public:
Stu(){};
Stu(string name,int age,int score,int high):name(name),age(age),score(score),high(new int(high))
{
cout << "Stu:的有参构造" << endl;
}
Stu(const Stu &other)
{
age = other.age;
name = other.name;
score = other.score;
//深拷贝
high = new int(*(other.high));
cout << "拷贝构造 " << endl;
}
void show()
{
cout << name << "\n" << age << "\n" << score << "\n" << *high << endl;
}
};
int main()
{
Stu *p1 = new Stu("zhangsan",18,100,90);
p1->show();
return 0;
}