欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-06-30 17:38:53
1 #include 2 #include 3 4 using namespace std; 5 6 class Person 7 { 8 private: 9 char Name[10]; 10 int Age; 11 public: 12 Person( ......
 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 }