📄 vc0801.cpp
字号:
//Example 8.1: 人员类(Person)及其子类雇员类(Employee)的定义及使用
#include <iostream.h>
#include <string.h>
class Person //人员类定义
{
protected:
char m_strName[10]; //姓名
int m_nAge; //年龄
int m_nSex; //性别
public:
void Register(char *name, int age, char sex) //设置数据成员
{
strcpy(m_strName, name);
m_nAge = age;
m_nSex=(sex=='m'?0:1);
}
char* GetName() //取姓名
{ return m_strName; }
char GetSex() //取性别
{ return m_nSex==0?'m':'f'; }
int GetAge() //取年龄
{ return m_nAge; }
void ShowMe() //显示数据成员
{cout<<GetName()<<"\t"<<GetSex()<<"\t"<<GetAge()<<"\t";}
};
class Employee: public Person //雇员类定义
{
char m_strDept[20]; //工作部门
float m_fSalary; //月薪
public:
Employee()
{ EmployeeRegister("XXX",0,'m',"XXX",0); }
EmployeeRegister(char *name, int age, char sex, char *dept, float salary);
void ShowMe(); //显示雇员信息
};
Employee::EmployeeRegister(char *name, int age, char sex, char *dept, float salary)
{
Register(name,age,sex);
strcpy(m_strDept, dept);
m_fSalary = salary;
}
void Employee::ShowMe ()
{
cout<< GetName()<<"\t"<<GetSex()<<"\t"<<GetAge()<<"\t";
cout<<m_strDept<<"\t"<<m_fSalary<<endl;
}
//主函数
void main()
{
Employee emp;
emp.ShowMe();
emp.EmployeeRegister("张莉",40,'f',"图书馆",2000);
emp.ShowMe();
cout<<"调用基类GetName()返回值为: "<<emp.GetName()<<endl;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -