西加加:继承和派生练习1

代码实现: 

#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;
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值