实验三多态性实验报告

合集下载

实验3 虚函数和多态性

实验3 虚函数和多态性

实验3 虚函数和多态性1实验目的(1)了解多态性的概念。

(2)了解虚函数的作用和使用方法。

(3)了解静态关联和动态关联的概念和用法。

(4)了解纯虚函数和抽象类的概念和用法。

2实验内容任务1:参照书中的例6.4,设计一个程序,声明抽象基类shape,由它派生出3个类:Circle (圆形)、Rectangle(矩形)、Triangle(直角三角形),用一个函数printArea分别输出以上三者的面积。

任务1参考程序#include <iostream.h>//定义抽象基类Shapeclass Shape{public:virtual double printArea() const=0;};//定义Circle类class Circle:public Shape{public:Circle(double r):radius(r){}virtual double printArea() const;protected:double radius;};double Circle::printArea( ) const{return 3.14159*radius*radius;}//定义矩形类class Rect:public Shape{public:Rect(double w,double h):width(w),height(h){}virtual double printArea( ) const;protected:double width,height;};double Rect::printArea() const{return width*height;}//定义直角三角形class Triangle:public Shape{public:Triangle(double w,double h):width(w),height(h){}virtual double printArea( ) const;protected:double width,height;};double Triangle::printArea() const{return 0.5*width*height;}void main(){Circle circle(12.6);cout << "area of circle = "<<circle.printArea() <<endl;Rect rect(4.5,8.4);cout << "area of rectangle = "<< rect.printArea() <<endl;Triangle trl(4.5,8.4);cout << "area of triangle = "<<trl.printArea() <<endl;}3预习内容第6章。

实验报告多态性

实验报告多态性

一、实验目的1. 理解多态性的概念及其在面向对象编程中的重要性。

2. 掌握多态性的实现方式,包括方法重载和方法覆盖。

3. 学习如何利用多态性提高代码的可读性和可维护性。

4. 通过实例分析,加深对多态性在实际编程中的应用理解。

二、实验背景多态性是面向对象编程中的一个核心概念,它允许同一个接口(或方法)根据不同的数据类型执行不同的操作。

在Java、C++等面向对象编程语言中,多态性主要通过方法重载和方法覆盖来实现。

三、实验内容1. 方法重载方法重载是指在同一个类中,允许存在多个同名方法,但参数列表不同。

编译器通过参数列表的匹配来确定调用哪个方法。

(1)实验步骤1)创建一个名为“Animal”的类,包含一个方法名为“makeSound”。

2)在“Animal”类中,添加三个重载的“makeSound”方法,分别接受不同类型的参数(如int、String、double)。

3)创建一个名为“Dog”的类,继承自“Animal”类,并重写“makeSound”方法,使其输出“Woof! Woof!”。

4)创建一个名为“Cat”的类,继承自“Animal”类,并重写“makeSound”方法,使其输出“Meow! Meow!”。

(2)实验结果当创建“Dog”和“Cat”对象时,调用“makeSound”方法会根据对象类型输出相应的声音。

2. 方法覆盖方法覆盖是指在子类中重写父类的方法,使子类的方法具有与父类方法相同的签名,但具有不同的实现。

(1)实验步骤1)创建一个名为“Vehicle”的类,包含一个方法名为“move”,该方法无参数。

2)创建一个名为“Car”的类,继承自“Vehicle”类,并重写“move”方法,使其输出“Car is moving”。

3)创建一个名为“Bike”的类,继承自“Vehicle”类,并重写“move”方法,使其输出“Bike is moving”。

(2)实验结果当创建“Car”和“Bike”对象时,调用“move”方法会根据对象类型输出相应的移动信息。

多态性与虚函数实验报告

多态性与虚函数实验报告

多态性与虚函数实验报告实验目的:通过实验掌握多态性和虚函数的概念及使用方法,理解多态性实现原理和虚函数的应用场景。

实验原理:1.多态性:多态性是指在面向对象编程中,同一种行为或者方法可以具有多种不同形态的能力。

它是面向对象编程的核心特性之一,能够提供更加灵活和可扩展的代码结构。

多态性主要通过继承和接口来实现。

继承是指子类可以重写父类的方法,实现自己的特定行为;接口是一种约束,定义了类应该实现的方法和属性。

2.虚函数:虚函数是在基类中声明的函数,它可以在派生类中被重新定义,以实现多态性。

在类的成员函数前面加上virtual关键字,就可以将它定义为虚函数。

当使用基类指针或引用调用虚函数时,实际调用的是派生类的重写函数。

实验步骤:1. 创建一个基类Shape,包含两个成员变量color和area,并声明一个虚函数printArea(用于打印面积。

2. 创建三个派生类Circle、Rectangle和Triangle,分别继承Shape类,并重写printArea(函数。

3. 在主函数中,通过基类指针分别指向派生类的对象,并调用printArea(函数,观察多态性的效果。

实验结果与分析:在实验中,通过创建Shape类和派生类Circle、Rectangle和Triangle,可以实现对不同形状图形面积的计算和打印。

当使用基类指针调用printArea(函数时,实际调用的是派生类的重写函数,而不是基类的函数。

这就是多态性的实现,通过基类指针或引用,能够调用不同对象的同名函数,实现了对不同对象的统一操作。

通过实验1.提高代码的可扩展性和灵活性:通过多态性,可以将一类具有相似功能的对象统一管理,节省了代码的重复编写和修改成本,增强了代码的可扩展性和灵活性。

2.简化代码结构:通过虚函数,可以将各个派生类的不同行为统一命名为同一个函数,简化了代码结构,提高了代码的可读性和维护性。

3.支持动态绑定:通过运行时的动态绑定,可以根据对象的实际类型来确定调用的函数,实现了动态绑定和多态性。

类的继承与多态性实验报告

类的继承与多态性实验报告

类的继承与多态性实验报告目录1.介绍2.什么是多态3.多态在Java中的技术基础4.示例5.多态的好处6.总结介绍之前的文章介绍过类的封装性和继承性,现在是时候聊聊面向对象编程的三大特性之一的最后一个类的多态性了。

多态的基础是继承(包括了接口的实现)和方法的覆盖。

什么是多态多态对应的英文单词是polymorphism,百度翻译给出的翻译是:n.多型现象,多态性; 多机组合形式;按字面意思就是多种状态、形态、姿态等等,潜台词就是某个东西具有多种状态、形态、姿态等等。

那是什么东西呢?在面向对象的编程语言里面(当然就包括Java了)就是某个方法或函数。

那方法的多种状态、形态、姿态有是个什么意思呢?这其实是指同一个方法具有多个方法体,就是方法的实现。

而方法的相同与否是由方法签名决定的。

所以,多态其实本质上是指同一个类的同一个方法却具有不同的行为特征。

状态、形态、姿态指的就是行为特征。

多态在Java中的技术基础然而,在Java中,同一个类里面是不可能存在两个签名相同而实现不同的方法的,否则的话会导致无法判断该执行哪个方法,因此在编译时就会报错。

所以,肯定是在两个类中才有可能存在两个签名相同而实现不同的方法,一个实现在这个类,另一个实现在另一个类。

而如果这两个类毫无瓜葛,那么肯定就与多态的本质(同一个类的同一个方法却具有不同的行为特征)自相矛盾了。

所以,这两个类肯定是有某种联系的。

我们再想想,什么概念是能够让两个不同的类却又能称为同一个类的?答案就是类的继承/扩展,就是现实中的“某东西是某类东西”的概念,就是“具体和抽象”的思想。

比如,男人是人,女人也是人,男人类和女人类就借助于人类建立了某种联系,而人类具有的某个行为在男人类和女人类中是有着不同体现的,比如人类的吃饭这个行为,男人类的体现是狼吞虎咽,女人类的体现是细嚼慢咽。

例子不是很恰当,但意思就是这么个意思。

所以说,Java里面多态的技术基础就是方法的覆盖,当然,在Java中覆盖不仅仅发生在类的继承/扩展上,还可能发生在接口的实现上。

实验三 继承和多态

实验三 继承和多态

实验三继承和多态一、实验目的1.熟练掌握封装性,多态性的概念。

2.熟练掌握抽象类abstract的概念。

3.熟练掌握接口interface的概念。

4.熟练包package的概念以及编译运行的方法。

二、实验内容1.编写一个Java Application程序,该程序有个点Point类,它包含横坐标x和纵坐标y 两个属性,再给Point定义两个构造方法和一个打印点坐标的方法Show。

定义一个圆Circle 类,它继承Point类(它是一个点,圆心(Center)),除此之外,还有属性半径Radius,再给圆定义2个构造方法、一个打印圆的面积的方法PrintArea和一个打印圆中心、半径的方法Show(其中显示圆心可以用super.Show()的方式)。

编写一测试类,对其进行编译、运行。

结果如何?如去掉语句“super.Show();”,再看看运行结果。

理解程序中重载和多态性的运用。

2.完成以下步骤要求:(1)设计一个表示二维平面上点的类Point,包含有表示坐标位置的protected类型的成员变量x和y,获取和设置x和y值的public方法。

(2)设计一个表示二维平面上圆的类Circle,它继承自类Point,还包含有表示圆半径的protected类型的成员变量r,获取和设置r值的public方法、计算圆面积的public 方法。

(3)设计一个表示圆柱体的类Cylinder,它继承自类Circle,还包含有表示圆柱体高的protected类型的成员变量h、获取和设置h值的public方法、计算圆柱体体积的public 方法。

(4)建立若干个Cylinder对象,输出其轴心位置坐标、半径、高及其体积的值。

3.学校中有老师和学生两类人,而在职研究生既是老师又是学生,对学生的管理和对教师的管理在他们身上都有体现。

(1)设计两个信息管理接口StudentInterface和TeacherInterfaceo其中,StudentInterface接口包括setFee方法和getFee方法,分别用于设置和获取学生的学费;TeacherInterface接口包括setPay方法和getPay方法,分别用于设置和获取教师的工资。

湖北理工(黄石理工)C++实验 实验三多态性#精选

湖北理工(黄石理工)C++实验 实验三多态性#精选

实验3 多态性实验课程名:面向对象程序设计(C++)专业班级:09计科(一)班学号:2******** 姓名:***** 实验时间:12.6 实验地点:K4-102 指导教师:祁文青{cout<<"D::f3(double)"<<3*x<<endl;}};int main(){D d;B*pb=&d;D*pd=&d;pb->f1(1.23);pb->f1(1.23);pb->f2(1.23);pb->f3(1.23);pb->f3(3.14);return 0;}程序的运行结果。

答:2.编写一个程序,其中设计一个时间类Time,用来保存时、分、秒等私有数据成员,通过重载操作符“+”实现两个时间的相加。

要求将小时范围限制在大于等于0,分钟范围限制在0~59,秒钟范围限制在0~59秒。

【提示】时间类Time{public:Time(int h=0,int m=0,int s=0);Time operator+(Time&);void disptime(string);private:int hourse;cout<<s<<hourse<<":"<<minutes<<":"<<seconds<<endl;}int main(){int hh,mm,ss;do{cout<<"输入第一个时间时分秒(例如2 30 42)"<<endl;cin>>hh>>mm>>ss;}while(hh<0||mm<0||mm>59||ss<0||ss>59);Time t(hh,mm,ss);do{cout<<"输入第二个时间时分秒(例如2 30 42)"<<endl;cin>>hh>>mm>>ss;}while(hh<0||mm<0||mm>59||ss<0||ss>59);Time T(hh,mm,ss),t_total;t_total=t+T;t_total.disptime("输出结果(时/分/秒):");return 0;}程序运行结果:3.给出下面的抽象基类container;class container{protected:double radius;public:container(double radius1);virtual double surface_area()=0;virtual double volume()=0;};要求建立3个继承container的派生类cube、sphere与cylinder,让每一个派生类都包含虚函数surface_area()和volume(),分别用来计算正方体、球体和圆柱体的表面积及体积。

实验兔三个封闭群微卫星DNA多态性遗传分析

实验兔三个封闭群微卫星DNA多态性遗传分析

中国实验动物学报 21 0 2年 4月 第 2 O卷 第 2期
Ac a nt S i i, pi2 1 V 12 . o 2 t L bA i c n A r ,0 2, o.0 N . a a S l
27
微 卫 星序列 ( coa It D A) 称 为 简单 序 mi st le N 又 r e i
间遗 传 分 化 明显 。
【 关键词 】 日本大耳 白兔 ; 青紫蓝兔 ; 新西 兰兔 ; 封闭群 ; 微卫 星
【 图 分 类 号 】Q 53 中 9 —3 【 献标 识 码 】A 文 【 章 编 号 】10 -87 2 1 )2 2 — 文 0 5 4 (0 2 0  ̄0 60 4 6
【 ywod 】 Jp ns h erbi; hnhl abt; e eln b i ; lsdcln s Mi oa lt l i Ke r s a aeew i abt C i iarb i N wZ aadr b s Coe ooi ; c stle o t c l s a t e r e i c
青 紫 蓝 品种 在 1L E 1位 点 IR C D V 2 3位 点 , 211 新 N A C D O8 N A C D 00 日本
c D 阳 23 P<0 0 , 著 偏 离 H c D 0, .5 显 WE, 数 表 现 为 杂 合 子 缺 陷 ; 个 群 体 在 S t 、 多 三 a 3 l
( a oa r nm l nt i e t ,Is tt o a oa r A i a S i c s C ieeA a e yo dc l L b rt y A i a Mo i r gC n r nt u f b rt y nm l c n e , hn s c d m f o on e i e L o e Me i a S in e n e igU i d a C l g , e ig1 0 2 , hn ) ce c s dP kn no Me i l ol e B in 0 0 1 C ia a n c e j

多态性和虚函数 实验报告

多态性和虚函数    实验报告

淮海工学院计算机科学系实验报告书课程名:《 C++程序设计(二)》题目:多态性和虚函数班级:学号:姓名:1、实验内容或题目(1)声明二维坐标类作为基类派生圆的类,把派生类圆作为基类,派生圆柱体类。

其中,基类二维坐标类有成员数据:x、y坐标值;有成员函数:构造函数实现对基类成员数据的初始化、输出的成员函数,要求输出坐标位置。

派生类圆类有新增成员数据:半径(R);有成员函数:构造函数实现对成员数据的初始化、计算圆面积的成员函数、输出半径的成员函数。

派生圆柱体类新增数据有高(H);新增成员函数有:构造函数、计算圆柱体体积的函数和输出所有成员的函数。

请完成程序代码的编写、调试。

(2)教材393页7-8题。

(3)教材416页1、4、5题。

2、实验目的与要求(1)理解继承与派生的概念(2)掌握通过继承派生出一个新的类的方法(3)了解多态性的概念(4)了解虚函数的作用与使用方法3、实验步骤与源程序⑴实验步骤先定义一个基类point,及其成员函数,然后以public的继承方式定义子类circle,再定义一个派生类cylinder,最后在main主函数中定义类对象,调用函数实现其功能。

先定义一个基类A及其重载的构造函数,然后以Public派生出子类B,再定义其构造函数,最后在main主函数中定义类对象,调用成员函数实现其功能。

⑵源代码1.#include <iostream.h>class Point{public:Point(float=0,float=0);void setPoint(float,float);float getX() const {return x;}float getY() const {return y;}friend ostream & operator<<(ostream &,const Point &); protected:float x,y;};Point::Point(float a,float b){x=a;y=b;}void Point::setPoint(float a,float b){x=a;y=b;}ostream & operator<<(ostream &output,const Point &p){cout<<"["<<p.x<<","<<p.y<<"]"<<endl;return output;}class Circle:public Point{public:Circle(float x=0,float y=0,float r=0);void setRadius(float);float getRadius() const;float area () const;friend ostream &operator<<(ostream &,const Circle &); protected:float radius;};Circle::Circle(float a,float b,float r):Point(a,b),radius(r){}void Circle::setRadius(float r){radius=r;}float Circle::getRadius() const {return radius;}float Circle::area() const{return 3.14159*radius*radius;}ostream &operator<<(ostream &output,const Circle &c){cout<<"Center=["<<c.x<<","<<c.y<<"], r="<<c.radius<<", area="<<c.area()<<endl;return output;}class Cylinder:public Circle{public:Cylinder (float x=0,float y=0,float r=0,float h=0);void setHeight(float);float getHeight() const;float area() const;float volume() const;friend ostream& operator<<(ostream&,const Cylinder&);protected:float height;};Cylinder::Cylinder(float a,float b,float r,float h):Circle(a,b,r),height(h){}void Cylinder::setHeight(float h){height=h;}float Cylinder::getHeight() const {return height;}float Cylinder::area() const{return 2*Circle::area()+2*3.14159*radius*height;}float Cylinder::volume() const{return Circle::area()*height;}ostream &operator<<(ostream &output,const Cylinder& cy){cout<<"Center=["<<cy.x<<","<<cy.y<<"], r="<<cy.radius<<", h="<<cy.height <<"\narea="<<cy.area()<<", volume="<<cy.volume()<<endl;return output;}int main(){Cylinder cy1(3.5,6.4,5.2,10);cout<<"\noriginal cylinder:\nx="<<cy1.getX()<<", y="<<cy1.getY()<<", r=" <<cy1.getRadius()<<", h="<<cy1.getHeight()<<"\narea="<<cy1.area()<<", volume="<<cy1.volume()<<endl;cy1.setHeight(15);cy1.setRadius(7.5);cy1.setPoint(5,5);cout<<"\nnew cylinder:\n"<<cy1;Point &pRef=cy1;cout<<"\npRef as a point:"<<pRef;Circle &cRef=cy1;cout<<"\ncRef as a Circle:"<<cRef;return 0;}2.(1)#include <iostream>using namespace std;class A{public:A(){a=0;b=0;}A(int i){a=i;b=0;}A(int i,int j){a=i;b=j;}void display(){cout<<"a="<<a<<" b="<<b;} private:int a;int b;};class B : public A{public:B(){c=0;}B(int i):A(i){c=0;}B(int i,int j):A(i,j){c=0;}B(int i,int j,int k):A(i,j){c=k;}void display1(){display();cout<<" c="<<c<<endl;}private:int c;};int main(){B b1;B b2(1);B b3(1,3);B b4(1,3,5);b1.display1();b2.display1();b3.display1();b4.display1();return 0;}(2)#include <iostream>using namespace std;class A{public:A(){cout<<"constructing A "<<endl;} ~A(){cout<<"destructing A "<<endl;} };class B : public A{public:B(){cout<<"constructing B "<<endl;} ~B(){cout<<"destructing B "<<endl;} };class C : public B{public:C(){cout<<"constructing C "<<endl;}~C(){cout<<"destructing C "<<endl;}};int main(){C c1;return 0;}3.(1)//Point.hclass Point{public:Point(float=0,float=0);void setPoint(float,float);float getX() const {return x;}float getY() const {return y;}friend ostream & operator<<(ostream &,const Point &); protected:float x,y;}//Point.cppPoint::Point(float a,float b){x=a;y=b;}void Point::setPoint(float a,float b){x=a;y=b;}ostream & operator<<(ostream &output,const Point &p){output<<"["<<p.x<<","<<p.y<<"]"<<endl;return output;}//Circle.h#include "point.h"class Circle:public Point{public:Circle(float x=0,float y=0,float r=0);void setRadius(float);float getRadius() const;float area () const;friend ostream &operator<<(ostream &,const Circle &);protected:float radius;};//Circle.cppCircle::Circle(float a,float b,float r):Point(a,b),radius(r){}void Circle::setRadius(float r){radius=r;}float Circle::getRadius() const {return radius;}float Circle::area() const{return 3.14159*radius*radius;}ostream &operator<<(ostream &output,const Circle &c){output<<"Center=["<<c.x<<","<<c.y<<"], r="<<c.radius<<", area="<<c.area()<<endl;return output;}//Cylinder.h#include "circle.h"class Cylinder:public Circle{public:Cylinder (float x=0,float y=0,float r=0,float h=0);void setHeight(float);float getHeight() const;float area() const;float volume() const;friend ostream& operator<<(ostream&,const Cylinder&);protected:float height;};//Cylinder.cppCylinder::Cylinder(float a,float b,float r,float h):Circle(a,b,r),height(h){}void Cylinder::setHeight(float h){height=h;}float Cylinder::getHeight() const {return height;}float Cylinder::area() const{ return 2*Circle::area()+2*3.14159*radius*height;}float Cylinder::volume() const{return Circle::area()*height;}ostream &operator<<(ostream &output,const Cylinder& cy){output<<"Center=["<<cy.x<<","<<cy.y<<"], r="<<cy.radius<<", h="<<cy.height<<"\narea="<<cy.area()<<", volume="<<cy.volume()<<endl;return output;}//main.cpp#include <iostream.h>#include "cylinder.h"#include "point.cpp"#include "circle.cpp"#include "cylinder.cpp"int main(){Cylinder cy1(3.5,6.4,5.2,10);cout<<"\noriginal cylinder:\nx="<<cy1.getX()<<", y="<<cy1.getY()<<", r=" <<cy1.getRadius()<<", h="<<cy1.getHeight()<<"\narea="<<cy1.area()<<", volume="<<cy1.volume()<<endl;cy1.setHeight(15);cy1.setRadius(7.5);cy1.setPoint(5,5);cout<<"\nnew cylinder:\n"<<cy1;Point &pRef=cy1;cout<<"\npRef as a point:"<<pRef;Circle &cRef=cy1;cout<<"\ncRef as a Circle:"<<cRef;return 0;}(2)#include <iostream>using namespace std;class Shape{public:virtual double area() const =0;class Circle:public Shape{public:Circle(double r):radius(r){} virtual double area() const {return 3.14159*radius*radius;}; protected:double radius;};class Rectangle:public Shape{public:Rectangle(double w,double h):width(w),height(h){} virtual double area() const {return width*height;} protected:double width,height; };class Triangle:public Shape{public:Triangle(double w,double h):width(w),height(h){} virtual double area() const {return 0.5*width*height;} protected:double width,height; };void printArea(const Shape &s)cout<<s.area()<<endl;} int main(){Circle circle(12.6);cout<<"area of circle =";printArea(circle);Rectangle rectangle(4.5,8.4);cout<<"area of rectangle =";printArea(rectangle);Triangle triangle(4.5,8.4);cout<<"area of triangle =";printArea(triangle);return 0;}(3)#include <iostream>using namespace std;class Shape{public:virtual double area() const =0;};class Circle:public Shape{public:Circle(double r):radius(r){}virtual double area() const {return 3.14159*radius*radius;}; protected:double radius;class Square:public Shape{public:Square(double s):side(s){}virtual double area() const {return side*side;}protected:double side;};class Rectangle:public Shape{public:Rectangle(double w,double h):width(w),height(h){}virtual double area() const {return width*height;}protected:double width,height; };class Trapezoid:public Shape{public:Trapezoid(double t,double b,double h):top(t),bottom(t),height(h){} virtual double area() const {return 0.5*(top+bottom)*height;} protected:double top,bottom,height;};class Triangle:public Shapepublic:Triangle(double w,double h):width(w),height(h){}virtual double area() const {return 0.5*width*height;} protected:double width,height;};int main(){Circle circle(12.6);Square square(3.5);Rectangle rectangle(4.5,8.4);Trapezoid trapezoid(2.0,4.5,3.2);Triangle triangle(4.5,8.4);Shape *pt[5]={&circle,&square,&rectangle,&trapezoid,&triangle};double areas=0.0;for(int i=0;i<5;i++){areas=areas+pt[i]->area();}cout<<"totol of all areas="<<areas<<endl;return 0;}4、测试数据与实验结果(可以抓图粘贴)5、结果分析与实验体会继承时,子类对基类的访问属性,基类的私有成员无论以何种方式继承在子类中都是不可访问的,唯有调用基类中的成员函数方可访问其私有变量。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

浙江理工大学信息学院
实验指导书
实验名称:类的多态性的实现学时安排:3
实验类别:设计性实验实验要求:1人1组
学号:姓名:
 ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄
一、实验目的
1.理解重载运算符的意义。

2.掌握使用成员函数、友员函数重载运算符的特点。

3.掌握重载运算符函数的调用方法。

4.掌握动态联编的概念。

5.掌握虚函数和纯虚函数的使用方法。

二、实验原理介绍
设计性实验
具体原理请见实验内容和步骤
实现对抽象类的继承,通过operator函数调用的形式,实现运算符的重载
三、实验设备介绍
软件需求: windows或linux下的c++编译器
硬件需求: 对于硬件方面的要求,建议配置是Pentium III 450以上的CPU处理器,64MB以上的内存,200MB的自由硬盘空间、CD-ROM驱动器、能支持24位真彩色的显示卡、彩色显示器、打印机。

四、实验内容
某公司的员工有经理Manager、技术人员Technicist和营销人员SalsePerson,他们的薪金计算方法如下:
经理按月计酬,方法是:基本工资+奖金;技术人员按月计酬,方法是:基本工资;营销人员按月计酬,方法是:基本工资+销售利润*5%。

每类人员都有职工编号、姓名、性别、入职时间、职位、基本工资等数据;各类人员
使用统一接口get_pay()计算各类人员的月薪,重载<<运算符实现员工信息的输出。

其次,设计一个统计并输出该公司员工当月薪金情况的报表类Report,该类提供insert接口向Report类的容器中添加员工信息,并提供print接口用于展示以职位为单位的每个员工的职工编号、姓名、性别、入职时间以及当月该员工的薪酬,并统计出该职位员工薪酬的最高值和最低值。

为了提供更方便的查找功能,请为Report类重载[]运算符,下标值为职位,能根据职位信息查找出所有符合该职位的员工。

在主函数中对实现的类进行测试,首先,创建各类人员对象,通过Report类的insert接口向报表中添加这些人员信息,然后通过Report类的print接口输出当月员工薪酬情况报表。

存储员工对象的容器请选用合适的STL容器。

五程序清单
ormat("ddd")<<"\t";
cout<<(*it)->getbasicmoney()<<"\t"<<"\t";
cout<<(*it)->getpay(month) << endl;
}
}
void Report::insert(Employee* p)
{
(p);
}
Report::~Report(){
list<Employee*>::iterator it;
for(it = (); it != (); it ++){
delete *it;
}
}
double Technicist::getpay(int m){
return basicmoney;
}
double SalesPerson::getpay(int m){
return basicmoney + sales[m] * ;
}
//
//增加友元函数,重载<<
friend ostream &operator<<(ostream &os,CDate &date)
{
os<<<<"年"<<<<"月"<<<<"日"<<endl;
return os;
}
六运行结果
七实验心得
此次试验涉及内容较多,主要是多态和STL容器的运用,我了解并运用了vector、list、map容器,并使用了迭代器对容器元素进行访问,复习了重载、继承的知识。

在编程过程中也遇到了很多困难,比如怎样重载[]来找到所有并访问同一职务的人员的基本信息,比如怎样提取工资并对其进行大小比较,知道了不止一种方法执行此操作。

收获颇丰。

相关文档
最新文档