#include#include using namespace std; class Student { private: int number; char* name; int age; float score; public: Student();//默认构造函数 Student(int no, const char* n, int a, float s);//带参构造函数 Student(Student& s);//拷贝构造函数 ~Student();//析构函数 void show(); }; Student::Student() {//默认构造函数 number = 0; name = NULL; age = 0; score = 0; } Student::Student(int no, const char* n, int a, float s) {//带参构造函数 number = no; name = new char[strlen(n) + 1]; strcpy_s(name, strlen(n) + 1, n); age = a; score = s; } Student::Student(Student& s) {//拷贝构造函数 number = s.number; name = new char[strlen(s.name) + 1]; name = s.name; age = s.age; score = s.score; } Student::~Student() {//析构函数 if (name != NULL) { cout << name << "同学分解掉" << endl; } else { cout << "有一个同学即将退出" << endl; } } void Student::show() { if (name != NULL) { cout << "学号:" << number << "t"; cout << "姓名:" << name << "t"; cout << "年龄:" << age << "t"; cout << "成绩:" << score << endl; } } int main(void) { Student zhangsan(1, "张三", 20, 97); Student lisi(2, "李四", 21, 100); zhangsan.show(); lisi.show(); Student anonymous(zhangsan); anonymous.show(); return 0; }



