定义一个描述教师的类Teacher,数据成员包括工号(Num),姓名(Name、性别(Sex、家庭住址( Addr}、联系电话(Tel}, E-mail地址(Email )、职务(Headship )、职称(Post)和工资(Salary对于数据成员,要求用字符数组实现工号、姓名、家庭住址、联系电话、E-mail地址、职务和职称,用字符型量实现性别,用整型量实现工资。成员函数包括:①设置工号。②设置姓名。③设置性别。④设置家庭住址。⑤设置联系电话。⑥设置email地址。⑦设置职务。⑧设置职称。⑨设置工资。⑩输出一个教师的全部描述信息,函数原型是OutputInfo( )。在主函数中定义一个教师类对象,然后对所有成员函数进行测试。
代码如下:
#include <iostream>
#include <cstring>
using namespace std;
class Teacher
{
private:
char Num[15];
char Name[10];
char Addr[30];
char Tel[20];
char Email[30];
char Headship[10];
char Post[10];
char Sex;
int Salary;
public:
void SetNum(char []);
void GetNum(char *);
void SetName(char []);
void GetName(char *);
void SetSex(char);
char GetSex();
void SetAddr(char []);
void GetAddr(char *);
void SetTel(char []);
void GetTel(char *);
void SetEmail(char []);
void GetEmail(char *);
void SetHeadship(char []);
void GetHeadship(char *);
void SetPost(char []);
void GetPost(char *);
void SetSalary(int);
int GetSalary();
void OutputInfo()
{
cout<<"工号:"<<Num<<endl;
cout<<"姓名:"<<Name<<endl;
cout<<"性别:"<<Sex<<endl;
cout<<"家庭住址:"<<Addr<<endl;
cout<<"联系电话:"<<Tel<<endl;
cout<<"E-mail地址:"<<Email<<endl;
cout<<"职务:"<<Headship<<endl;
cout<<"职称:"<<Post<<endl;
cout<<"工资:"<<Salary<<"元"<<endl;
}
};
void Teacher::SetNum(char num[])
{
strcpy(Num,num);
}
void Teacher::GetNum(char *num)
{
strcpy(num,Num);
}
void Teacher::SetName(char name[])
{
strcpy(Name,name);
}
void Teacher::GetName(char *name)
{
strcpy(name,Name);
}
void Teacher::SetSex(char sex)
{
Sex=sex;
}
char Teacher::GetSex()
{
return Sex;
}
void Teacher::SetAddr(char addr[])
{
strcpy(Addr,addr);
}
void Teacher::GetAddr(char *addr)
{
strcpy(addr,Addr);
}
void Teacher::SetTel(char tel[])
{
strcpy(Tel,tel);
}
void Teacher::GetTel(char *tel)
{
strcpy(tel,Tel);
}
void Teacher::SetEmail(char ema[])
{
strcpy(Email,ema);
}
void Teacher::GetEmail(char *ema)
{
strcpy(ema,Email);
}
void Teacher::SetHeadship(char hs[])
{
strcpy(Headship,hs);
}
void Teacher::GetHeadship(char *hs)
{
strcpy(hs,Headship);
}
void Teacher::SetPost(char pot[])
{
strcpy(Post,pot);
}
void Teacher::GetPost(char *pot)
{
strcpy(pot,Post);
}
void Teacher::SetSalary(int sal)
{
Salary=sal;
}
int Teacher::GetSalary()
{
return Salary;
}
int main()
{
Teacher t1;
char Num[15],Name[10],Addr[30],Tel[20],Email[30],Headship[10],Post[10],Sex;
int Salary;
t1.SetNum("20181237890");
t1.GetNum(Num);
t1.SetName("X老师");
t1.GetName(Name);
t1.SetSex('M');
t1.GetSex();
t1.SetAddr("广东省广州市");
t1.GetAddr(Addr);
t1.SetTel("13812345678");
t1.GetTel(Tel);
t1.SetEmail("gdufs@gdufs.com");
t1.GetEmail(Email);
t1.SetHeadship("教师");
t1.GetHeadship(Headship);
t1.SetPost("高级教师");
t1.GetPost(Post);
t1.SetSalary(999999999);
t1.GetSalary();
t1.OutputInfo();
}