派生类Student的构造函数和析构函数 代码参考

 1 #include <iostream>
 2 #include <cstring>
 3 
 4 using namespace std;
 5 
 6 class Person
 7 {
 8     private:
 9         char Name[10];
10         int Age;
11     public:
12         Person(char *name, int age)
13         {
14             strcpy(Name,name);
15             Age=age;
16             cout<<"constructor of person "<<Name<<endl;
17         }
18         ~Person(){cout<<"deconstructor of person "<<Name<<endl;}
19 };
20 
21 class Student:public Person
22 {
23     private:
24         char ClassName[10];
25         Person Monitor;
26     public:
27         Student(char *name, int age, char *classname, char *name1, int age1);
28         ~Student();
29 };
30 
31 Student::Student(char *name, int age, char *classname, char *name1, int age1):Person(name,age),Monitor(name1,age1)
32 {
33     strcpy(ClassName,classname);
34     cout<<"constructor of Student"<<endl;
35 }
36 
37 Student::~Student()
38 {
39     cout<<"deconstructor of Student"<<endl;
40 }
41 
42 int main()
43 {
44     char name1[10],name2[10],classname[20];
45     int age1,age2;
46     cin>>name1>>age1>>classname>>name2>>age2;
47     Student one(name1,age1,classname,name2,age2);
48     return 0;
49 }

猜你喜欢

转载自www.cnblogs.com/Conan-jine/p/12746494.html