实验8继承和多态.doc
文本预览下载声明
实验08:继承与多态
实验学时:6
实验类型:验证、设计
实验要求:必修
一、实验目的
1.理解继承的概念,了解面向对象设计中继承和多态的合理性;
2.掌握派生类的构造与析构;
3.掌握在对象中使用类层次和继承思想进行设计、实现和测试;
4.区别运行时的多态性的实现,理解重载与同名覆盖的差异;
5.理解虚函数与多态性;
6.实现运行时多态性的程序设计。
二、实验内容
1.Difine a class called PartFileledArrayWMax that is a derived class of the class PartFilledArray. The class PartFilledArrayWMax has one additional member variable named max_value that holds the maximum value stored in the array. Define a member accessor function named get_max that returns the maximum value stored in the array. Redefine the member function add_value and define two constructors, one of which has an int argument for the maximum number of entries in the array. Also define a copy constructor, an overloaded assignment operator, and a destructor. (A real class would have more member functions, but these will do for an exercise.)
2.某公司雇员(employee)包括经理(Manager)、技术人员(Technician)和销售员(Saleman)。开发部经理(developermanager)既是经理也是技术人员,销售部经理(salesmanager)既是经理也是销售员。
以employee类为虚基类,派生出manager、technician和saleman类,再进一步派生出developermanager和salesmanager类。
Employee类的属性包括姓名、职工号、工资级别、月薪(实发基本工资加业绩工资);操作包括月薪计算函数pay(),该函数要求输入请假天数,扣除应扣工资后,得出实发基本工资。
Technician类派生的属性有每小时附加酬金和当月工作时数,以及研究完成进度系数,业绩工资为三者之积。也包括同名的pay函数,工资总额为基本工资加业绩工资。
Saleman类派生的属性有当月销售额和酬金提取百分比,业绩工资为两者之积。也包括同名的pay函数,工资总额为基本工资加业绩工资。
Manager类派生的属性有固定奖金额和业绩系数,业绩工资为两者之积。工资总额也为基本工资加业绩工资。而在developermanager类中,pay函数是将作为经理和作为技术人员业绩工资之和的一半作为业绩工资。在salesmanager类中,pay函数则是经理的固定奖金额的一半,加上部门总销售额与提成比例之积,这是业绩工资。
编程实现工资管理。特别注意pay()的定义和调用方法:先用同名覆盖,再用运行时多态。
3.继承范例:
定义一个继承与派生关系的类体系,在派生类中访问基类成员。
[要求]定义一个点类,包含x,y 坐标数据成员,显示函数和计算面积的函数成员;以点为基类派生一个圆类,增加表示半径的数据成员,重载显示和计算面积的函数;定义一个直线类,以两个点类对象作数据成员,定义显示、求面积及长度函数。编程测试所定义的类体系。
[程序]
#include iostream.h
#include math.h
#define PI 3.14159
class Point{
friend class Line;
protected:
double x, y ;
public:
Point(){x = 0 ; y = 0 ; }
Point(double xv,double yv){ x = xv; y = yv; }
double Area(){return 0;}
void Show() {
coutx=x y=yendl;
}
};
class Circle :public Point{
double radius;
public:
Circle(){ x = 0; y = 0; radius = 0; }
C
显示全部