⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vc0802.cpp

📁 VC例题源代码,书本上的。。几乎是通用的
💻 CPP
字号:
//Example 8.2: 人员类(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: private 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(); 				//显示雇员信息 
	char* GetEmployeeName()	{ return	GetName(); }//取姓名
	char GetEmployeeSex() 	{ return	GetSex();  }//取性别
	int   GetEmployeeAge()	{ return	GetAge();  }//取年龄
};
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";		//调用私有成员GetName函数 
	cout<<GetAge()<<"\t";  		//调用私有成员GetAge函数
	cout<<GetSex()<<"\t";  		//调用私有成员GetSex函数
	cout<<m_strDept<<"\t"<<m_fSalary<<endl;
}	
//主函数
void main()
{
	Employee emp;
	emp.EmployeeRegister("张三",40,'m', "图书馆",2000);
	emp.ShowMe();
	cout<<"调用GetEmployeeName()返回值为:"<<emp.GetEmployeeName()<<endl;
	cout<<"调用GetEmployeeSex()返回值为: "<<emp.GetEmployeeSex()<<endl;
	cout<<"调用GetEmployeeAge()返回值为: "<<emp.GetEmployeeAge()<<endl;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -