1. 程式人生 > >類與物件(三)

類與物件(三)

定義一個描述教師的類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("

[email protected]");
t1.GetEmail(Email);
t1.SetHeadship("教師");
t1.GetHeadship(Headship);
t1.SetPost("高階教師");
t1.GetPost(Post);
t1.SetSalary(999999999);
t1.GetSalary();
t1.OutputInfo();
}