派生类Student的构造函数和析构函数

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_27928585/article/details/80232998

题目内容:

已知基类Person的定义如下:
class Person
{ char Name[10]; //姓名
int Age; //年龄
public:
Person(char* name,int age)
{ strcpy(Name, name);
Age = age;
cout<<”constructor of person “<

#include<iostream>
#include <cstring>
using namespace std;

class Person
{
private:
    char Name[10];
    int Age;
public:
    Person(char* name,int age)
    {
        strcpy(Name, name);
        Age = age;
        cout<<"constructor of person "<<Name<<endl;

    }
    ~Person()
    {
        cout<<"deconstructor of person "<<Name<<endl;
    };
};

class Student:public Person
{
private:
    char ClassName[10];
    Person Monitor;
public:
    Student(char *name, int age, char *classname, char *name1, int age1):Person(name,age),Monitor(name1,age1)
    {
        strcpy(ClassName, classname);
        cout<<"constructor of Student"<<endl;
    }
    ~Student()
    {
        cout<<"deconstructor of Student"<<endl;
    }

};

int main()
{
    char name[20],classname[10],name1[20];
    int age,age1;
    cin>>name>>age>>classname>>name1>>age1;
    Student s(name,age,classname,name1,age1);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_27928585/article/details/80232998