#include <iostream>
using namespace std;
class CPerson
{
public:
CPerson(int iAge, const char* sName)
{
this->iAge = iAge;
strcpy_s(this->sName, 32, sName); //安全函数,保障有效的缓冲区尺寸
}
virtual const char* WhoAmI()
{
return "I am a Person";
}
private:
int iAge;
char sName[32];
};
class CWorker : public CPerson
{
public:
CWorker(int iAge, const char* sName, const char* sEmploymentStatus)
: CPerson(iAge, sName)
{
strcpy_s(this->sEmploymentStatus, 32, sEmploymentStatus);
}
virtual const char* WhoAmI()
{
return "I am a worker";
}
private:
char sEmploymentStatus[32];
};
class CStudent : public CPerson
{
public:
CStudent(int iAge, const char* sName, const char* sStudentIdentityCard)
: CPerson(iAge, sName)
{
strcpy_s(this->sStudentIdentityCard, 32, sStudentIdentityCard);
}
virtual const char* WhoAmI()
{
return "I am a student";
}
private:
char sStudentIdentityCard[32];
};
int main(int argc,char* argv[])
{
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;
}
运行结果