c++继承理解

#include <iostream>
using namespace std;

class CPerson
{
public:
    CPerson(int iAge, char * sName)
    {
        this->iAge = iAge;
        strcpy_s(this->sName, sName);
    }
    virtual char * WhoAmi()
    {
        return "I am a person";
    }
private:
    int iAge;
    char sName[32];
};

class CWorker :public CPerson
{
public:
    CWorker(int iAge, char * sName, char * sEmploymentStatus) :CPerson(iAge, sName)
    {
        strcpy_s(this->sEmploymentStatus, 32, sEmploymentStatus);
    }
    virtual char * WhoAmi()
    {
        return "i am a worker";
    }
private:
    char sEmploymentStatus[32];
};

class CStudent :public CPerson
{
public: 
    CStudent(int iAge, char *sName, char * sStudentIdentityCard) :CPerson(iAge, sName)
    {
        strcpy_s(this->sStudentIdentityCard, 32, sStudentIdentityCard);
    }
    virtual char* WhoAmi()
    {
        return "I am a student";
    }
private:
    char sStudentIdentityCard[32];
};

int main()
{
    CPerson cPerson(10, "john");
    cout << cPerson.WhoAmi() << endl;

    CWorker cWorker(35, "Mary", "On wacation");
    cout << cWorker.WhoAmi() << endl;

    CStudent cStudent(22, "Sandra", "Phisician");
    cout << cStudent.WhoAmi() << endl;

    return 0;


}

猜你喜欢

转载自blog.csdn.net/u014133104/article/details/80048919