桂电C++面向对象程序设计实验 : 实验五 继承与派生

桂电C++面向对象程序设计实验: 实验五 继承与派生

实验内容:

(1)将《C++面向对象程序设计》第5章例5.1的程序片段补充和改写成一个完整、正确的程序,用公用继承方式。在程序中应包括输入数据的函数,在程序运行时输入num,name,sex,age,addr的值,程序应输出以上5个数据的值。本题是《C++面向对象程序设计》第5章第1题。
(2)将《C++面向对象程序设计》第5章例5.3的程序修改、补充,写成一个完整、正确的程序,用保护继承方式。在程序中应包括输入数据的函数。本题是《C++面向对象程序设计》第5章第3题。
(3)修改上面第(2)题的程序,改为用公用继承方式。上机调试程序,使之能正确运行并得到正确的结果。本题是《C++面向对象程序设计》第5章第4题。
(4)分别声明Teacher(教师)类和Cadre(干部)类,采用名重继承方式由这两个类派生出新类Teacher_Cadre(教师兼干部)。要求:
① 在两个基类中都包含姓名、年龄、性别、地址、电话等数据成员。
② 在Teacher类中还包含数据成员title(职称),在Cadre类中还包含数据成员post (职务)。在Teacher_Cadre类中还包含数据成员wages(工资)。
③ 对两个基类中的姓名、年龄、性别、地址、电话等数据成员用相同的名字,在引用这些数据成员时,指定作用域。
④ 在类体中声明成员函数,在类外定义成员函数。
⑤ 在派生类Teacher_Cadre的成员函数show中调用Teacher类中的display函数,输出姓名、年龄、性别、职称、地址、电话,然后再用cout语句输出职务与工资。

源码

"5.(1~3).cpp

#include<iostream>//1~3
#include<string>
using namespace std;
class Student
{
public:
	Student(long n,string nam,string s)
	{
		num=n; 
		name=nam; 
		sex=s; 
	};
protected:
	long num;
	string name;
	string sex;
};
class Student1 :public Student
{ 
public:
	Student1(long n,string nam,string s,int a,string add);
	void display();
private:
	int age;
	string addr;
};
Student1::Student1(long n,string nam,string s,int a,string add):Student(n,nam,s),age(a),addr(add){}
void Student1::display()
{
	cout << "学号:" << num << endl;
	cout << "姓名:" << name << endl;
	cout << "性别:" << sex << endl; 
	cout << "年龄:" << age << endl;
	cout << "地址:" << addr << endl;
}
int main()
{
	Student1 stud(170071234,"张三","男",20,"桂电花江");
	stud.display();
}

"4.cpp"

#include<iostream>
#include<string>
using namespace std;
class Teacher
{
public:
	Teacher(string n, string s, string tt, string ad, string tp, int age);
	void display();
protected:
	string name;
	string sex;
	string title;
	string addr;
	string telp;
	int age;
};
Teacher::Teacher(string n, string s, string tt, string ad, string tp, int a) :
name(n), sex(s), age(a),title(tt), addr(ad), telp(tp){}
void Teacher::display()
{
	cout << "姓名:" << name << endl;
	cout << "性别:" << sex << endl;
	cout << "年龄:" << age << endl;
	cout << "职称:" << title << endl;
	cout << "地址:" << addr << endl;
	cout << "电话:" << telp << endl;
}
class Cadre
{
public:
	Cadre(string n, string s, string p, string ad, string tp, int age);
	void display();
protected:
	string name;
	string sex;
	string post;
	string addr;
	string telp;
	int age;
};
Cadre::Cadre(string n, string s, string p, string ad, string tp, int a):
name(n), sex(s),age(a), post(p), addr(ad), telp(tp){}
void Cadre::display()
{
	cout << "姓名:" << name << endl;
	cout << "性别:" << sex << endl;
	cout << "年龄:" << age << endl;
	cout << "职务:" << post << endl;
	cout << "地址:" << addr << endl;
	cout << "电话:" << telp << endl;
}
class worker :public Teacher, public Cadre
{
public:
	worker(string n, string s, string tt, string p, string ad, string tp, int a, float w);
	void show();
private:
	float wage;
};
worker::worker(string n, string s, string tt,string p, string ad, string tp, int a, float w) :
Teacher(n, s, tt, ad, tp, a), Cadre(n, s, p, ad, tp, a),wage(w){}
void worker::show()
{
	Teacher::display();
	cout << "职务:" << Cadre::post << endl;
	cout << "工资:" << wage << endl;
}
int main()
{
	worker worker1("张三","男","技术人员","教师","广西","110",40,6666.6);
	worker1.show();
}


  • 7
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值