代码实现:
#include <iostream>
#include <string>
using namespace std;
class Person
{
string Num;
string Name;
char Sex;
string Addr;
string Tel;
string Email;
public:
Person(string num, string name, char sex, string addr, string tel, string email)
{
Num = num;
Name = name;
Sex = sex;
Addr = addr;
Tel = tel;
Email = email;
}
void show1()
{
cout << "Number: " << Num << endl;
cout << "Name: " << Name << endl;
cout << "Sex: " << Sex << endl;
cout << "Address: " << Addr << endl;
cout << "Telephone: " << Tel << endl;
cout << "Email: " << Email << endl;
}
};
class Student: public Person
{
int Math;
int Physics;
int Eng;
int Prog;
public:
Student(string num1, string name1, char sex1, string addr1, string tel1, string email1):Person(num1, name1, sex1, addr1, tel1, email1)
{
}
void SetScore(char tag, int score)
{
if (tag = 'm') Math = score;
if (tag = 'p') Physics = score;
if (tag = 'e') Eng = score;
if (tag = 'c') Prog = score;
}
void show()
{
show1() ;
cout << "Math: " << Math << endl;
cout << "Physics: " << Physics << endl;
cout << "English: " << Eng << endl;
cout << "Program: " << Prog << endl;
}
};
class Teacher: public Person
{
string Headship;
string Post;
int Salary;
public:
Teacher(string num2, string name2, char sex2, string addr2, string tel2, string email2,string Headship0,string Post0,int Salary0):Person( num2, name2, sex2, addr2, tel2, email2)
{
Headship = Headship0;
Post = Post0;
Salary = Salary0;
}
void show()
{
show1();
cout << "Headship: " << Headship << endl;
cout << "Post: " << Post << endl;
cout << "Salary: " << Salary << endl;
}
};
int main()
{
Student S("20191002963","Karen",'f' ,"Guangzhou" ,"123578" ,"123@qq.com");
S.SetScore('m', 80);
S.SetScore('p', 90);
S.SetScore('e', 70);
S.SetScore('c', 100);
S.show();
Teacher T("999999","Miss.Chen",'m',"Beijing","456789","456@qq.com","数学","数学老师",10000);
T.show();
return 0;
}
运行结果:
修改版:(好像更复杂更麻烦=。=
//修改版(set函数要求独立定义
#include <iostream>
#include <string.h>
using namespace std;
class Person
{
string Num;
string Name;
char Sex;
string Addr;
string Tel;
string Email;
public:
void setnum(const string num){
Num = num;
}
void setname(const string name){
Name = name;
}
void setsex(const char sex){
Sex = sex;
}
void setaddr(const string addr){
Addr = addr;
}
void settel(string tel){
Tel = tel;
}
void setemail(string email){
Email = email;
}
void show1()
{
cout << "Number: " << Num << endl;
cout << "Name: " << Name << endl;
cout << "Sex: " << Sex << endl;
cout << "Address: " << Addr << endl;
cout << "Telephone: " << Tel << endl;
cout << "Email: " << Email << endl;
}
};
class Student: public Person
{
int Math;
int Physics;
int Eng;
int Prog;
public:
Student():Person(){
}
void SetScore(char tag, int score)
{
if (tag = 'm') Math = score;
if (tag = 'p') Physics = score;
if (tag = 'e') Eng = score;
if (tag = 'c') Prog = score;
}
void show()
{
show1() ;
cout << "Math: " << Math << endl;
cout << "Physics: " << Physics << endl;
cout << "English: " << Eng << endl;
cout << "Program: " << Prog << endl;
}
};
class Teacher: public Person
{
string Headship;
string Post;
int Salary;
public:
Teacher():Person(){
}
void setheadship(string Headship0)
{
Headship = Headship0;
}
void setpost(string Post0)
{
Post = Post0;
}
void setsalary(int Salary0)
{
Salary = Salary0;
}
void show()
{
show1() ;
cout << "Headship: " << Headship << endl;
cout << "Post: " << Post << endl;
cout << "Salary: " << Salary << endl;
}
};
int main()
{
Student S;
S.setnum("20191002963");
S.setname("Karen");
S.setsex('f');
S.setaddr("GuangZhou");
S.settel("12345678990");
S.setemail("123456@qq.com");
S.SetScore('m', 80);
S.SetScore('p', 90);
S.SetScore('e', 70);
S.SetScore('c', 100);
S.show();
Teacher T;
T.setnum("00000");
T.setname("Mr.chen");
T.setsex('m');
T.setaddr("BeiJing");
T.settel("123456789");
T.setemail("654321@qq.com");
T.setheadship("数学老师");
T.setpost("数学");
T.setsalary(10000);
T.show();
return 0;
}