实验三 多态性实验报告
- 格式:doc
- 大小:89.50 KB
- 文档页数:11
多态性实验报告一、实验目的本次实验的主要目的是深入研究和理解多态性这一重要的概念,并通过实际操作和观察来验证多态性在不同情境下的表现和作用。
二、实验原理多态性是面向对象编程中的一个关键特性,它允许不同的对象对相同的消息或操作做出不同的响应。
这种特性基于类的继承和方法的重写机制。
当一个父类的引用指向子类对象时,通过该引用调用方法时,实际执行的是子类中重写的方法,从而实现了多态性。
三、实验材料与环境1、编程语言:选择了 Java 作为实验的编程语言。
2、开发工具:使用了 IntelliJ IDEA 作为集成开发环境。
3、实验设备:一台配置良好的计算机,操作系统为 Windows 10。
四、实验步骤1、创建父类`Shape````javaclass Shape {public void draw(){Systemoutprintln("Drawing a shape");}}```2、创建子类`Circle` 和`Square` 继承自`Shape````javaclass Circle extends Shape {@Overridepublic void draw(){Systemoutprintln("Drawing a circle");}}class Square extends Shape {@Overridepublic void draw(){Systemoutprintln("Drawing a square");}}```3、在主函数中进行测试```javapublic class PolymorphismTest {public static void main(String args) {Shape shape1 = new Circle();Shape shape2 = new Square();shape1draw();shape2draw();}}```五、实验结果运行上述代码,输出结果为:```Drawing a circleDrawing a square```这表明,通过父类的引用调用`draw` 方法时,实际执行的是子类中重写的方法,实现了多态性。
一、实验目的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中覆盖不仅仅发生在类的继承/扩展上,还可能发生在接口的实现上。
实验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(),分别用来计算正方体、球体和圆柱体的表面积及体积。
淮海工学院计算机科学系实验报告书课程名:《 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. 在掌握继承与派生关系的基础上,进一步理解虚函数与多态性的关系,实现运行时的多态。
2. 学会定义和使用纯虚函数二、实验容1.例:了解"单接口,多方法"的概念。
现有称为figure的基类,存放了各二维对象(三角形、矩形和圆形三个类)的各维数据,set_dim()设置数据,是标准成员函数。
show_area()为虚函数,因为计算各对象的面积的方法是不同的。
【程序】#include < iostream >using namespace std;class figure{protected:double x,y;public:void set_dim(double i,double j=0){ x=i; y=j; }virtual void show_area(){ cout<<"No area computation defined for this class.\n";}};class triangle:public figure{void show_area(){ cout<<"Triangle with height "<< x<<" and base "<< y<<" has an area of "<< x*0.5*y<< endl;}};class square:public figure{public:void show_area(){ cout<<"Square with dimensions "<< x<<" and "<< y<<" has an area of "<< x*y<< endl;}};class circle:public figure{public:void show_area(){ cout<<"Circle with radius "<< x<<" has an area of "<<3.14159*x*x<< endl;}};int main(){figure *p;triangle t;square s;circle c;p->set_dim(10.0,5.0);p->show_area();p=&s;p->set_dim(10.0,5.0);p->show_area();p=&c; p->set_dim(10.0);p->show_area();return 0;}【要求】(1)建立工程,录入上述程序,调试运行并记录运行结果。
实验三多态与模板一、声明一个Shape抽象类,在此基础上派生出Rectangle和Circle类,二者都有GetArea ()和GetPerim()函数,GetArea用来计算对象的面积,GetPerim用来计算对象的周长。
封装上述各类,并编写测试程序。
Shape.h的内容:class Shape{public:Shape();virtual void GetArea();virtual void GetPerim();virtual ~Shape();};Shape.cpp的内容:#include "Shape.h"Shape::Shape(){}Shape::~Shape(){}void Shape::GetArea(){}void Shape::GetPerim(){}Circle.h的内容:#include "Shape.h"class CCircle : public Shape{public:virtual void GetPerim();virtual void GetArea();CCircle();virtual ~CCircle();private:float r;};Circle.cpp中的内容#include "Circle.h"#include "math.h"#include "iostream.h"CCircle::CCircle(){cout<<"请输入圆的半径:"<<endl;cin>>r;}CCircle::~CCircle(){cout<<"Thank you very much!";}void CCircle::GetArea(){ float s;float pi=3.14;s=pi*r*r;cout<<"这个圆形的面积是:"<<s<<endl;} void CCircle::GetPerim(){ float c;float pi=3.14;c=2*pi*r;cout<<"这个圆形的周长是:"<<c<<endl; } Rectangle.h中的内容:#include"Shape.h"class Rectangle: public Shape{private:float a,b;public:Rectangle();virtual void GetArea();virtual void GetPerim();virtual ~Rectangle();};Rectangle.cpp中的内容:#include "Rectangle.h"#include "math.h"#include "iostream.h"Rectangle::Rectangle(){ cout<<"请输入矩形的长和宽:"<<endl;cin>>a>>b;}Rectangle::~Rectangle(){ cout<<"Thank you very much!"<<endl;}void Rectangle::GetArea(){ float s;s=a*b;cout<<"这个矩形的面积是:"<<s<<endl;} void Rectangle::GetPerim(){ float c;c=a+a+b+b;cout<<"这个矩形的周长是:"<<c<<endl;} Shapetest.cpp中的内容:#include"Circle.h"#include"Rectangle.h"#include "iostream.h"int main(){ CCircle C;C.GetArea();C.GetPerim();Rectangle R;R.GetArea();R.GetPerim();return 0;}二、有一种栈,这种栈是按数组索引减小的方向增长的。
浙江理工大学信息学院实验指导书实验名称:类的多态性的实现学时安排: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接口输出当月员工薪酬情况报表。
实验报告一、实验名称:类的多态性的实现二、实验目的:1)掌握用成员函数重载运算符的方法2)掌握用友元函数重载运算符的方法三、实验内容:模拟STL库中string类的功能,编写MyString类,至少实现字符串间的赋值、比较(包括>、<、=、!=)和字符串间的”+”(连接)操作,同时能通过下面列举的形式对字符串对象进行初始化。
如:MyString str1(“I am ”),str2=“a student”;if(str1>str2) cout<<“larger\n”;if(str1<str2) cout<<“smaller\n”;if(str1==str2) cout<<“equal\n”;if(str1!=str2) cout<<“unequal\n”;MyString str3=str1+str2;思考在为字符串分配空间时采用动态还是静态的分配方式,各有什么优缺点。
四、实验结果:#include<string.h>#include<iostream.h>class MyString{public:MyString(char *s=""){ str=new char[strlen(s)+1];strcpy(str,s);return;}MyString(const MyString& other){ str=new char[strlen(other.str)+1];strcpy(str,other.str);return;}int operator >(MyString& right) //重载>运算符{ int n;n=strcmp(str,right.str);if(n>0)return 1;else return 0;}int operator <(MyString& right) //重载<运算符{int n=strcmp(str,right.str);if(n<0)return 1;else return 0;}int operator ==(MyString& right) //重载==运算符 {int n=strcmp(str,right.str);if(n==0)return 1;else return 0;}int operator !=(MyString& right) //重载!=运算符 {int n=strcmp(str,right.str);if(n!=0)return 1;else return 0;}MyString operator +(MyString& right) //重载+运算符 {MyString temp;temp.str=strcat(str,right.str);return temp;}~MyString() // 析构函数{ return; }char* getstr() //取类中字符{ return str; }private :char *str;};void main(){MyString str1("I am "),str2="a student";if(str1>str2) cout<<"larger\n";if(str1<str2) cout<<"smaller\n";if(str1==str2) cout<<"equal\n";if(str1!=str2) cout<<"unequal\n";MyString str3=str1+str2;cout<<str3.getstr()<<endl; //输出str3类中字符 return;}[运行结果]思考题:字符串静态分配优点:使用方便缺点:定义的数组过大,会浪费大量的内存空间,定义的数组不够大时,可能引起下标越界错误,甚至导致严重后果。
实验四 NaCl盐析法提DNA及HLA多态性分析一、实验目的(1)掌握盐析法提取DNA方法;(2)掌握PCR扩增HLA基因序列的方法;(3)了解HLA多态性。
二、实验原理(1)盐析法提取DNA原理DNA和蛋白质等其他成分在不同浓度的NaCl溶液中溶解度不同,利用这一特点选择适当的盐浓度就能使DNA充分溶解而使杂质沉淀或者相反以达到分离目的。
DNA在NaCl溶液中的溶解度先增大后减小,在DNA溶解度最低时DNA从溶液中析出,而其他杂质还留在溶液中达到粗提取的目的。
(2)PCR技术的原理在DNA聚合酶催化下,以母链DNA为模板,以特定引物为延伸起点,通过变性、退火、延伸等步骤,体外复制出与母链模板DNA 互补的子链DNA的过程。
是一项DNA体外合成放大技术,能快速特异地在体外扩增任何目的DNA。
三、实验材料(1)全血样品;NaCl;Tris-HCl;蛋白酶K;70%乙醇;无水乙醇;MgCl2;SDS;乙二胺四乙酸(EDTA);琼脂糖;PCR试剂。
四、操作方法DNA提取:1.新鲜抗凝全血或冻存抗凝全血0.5ml置于2ml离心管中,补3倍体积的红细胞裂解液(0.01mol/L Tris-HCL pH7.6; 0.01mol/L NaCl; 0.005mol/L MgCl2)混合,2500 rpm离心15 min,移去上清,再用同体积的红细胞裂解液两次裂解红细胞,沉淀用生理盐水洗涤1次,收集白细胞沉淀。
2.沉淀中加入150μl TE混匀,再加15μl 10% SDS及蛋白酶K 20μl,56℃消化过夜, 得到清亮粘绸的液体。
加入235μl 5mol/L NaCl,轻轻摇匀至少1min,5000rpm离心10min;3.将上清转至另一支1.5ml离心管中,加860μl无水乙醇颠倒摇动数次至DNA絮状析出,离心10000转/分,10分钟;4.加860μl冰冻70%乙醇混合去盐,12000rpm离心3min,弃上清,重复一次。
一、实验目的通过本次实验,加深对多态性的理解,掌握多态性的基本概念、实现方式以及在Java语言中的应用,提高面向对象编程的能力。
二、实验环境1. 操作系统:Windows 102. 开发工具:Eclipse3. 编程语言:Java三、实验内容1. 多态性基本概念多态性是指同一个操作作用于不同的对象,可以有不同的解释,产生不同的执行结果。
在Java中,多态性主要表现在两个方面:方法重载和方法重写。
2. 方法重载方法重载是指在一个类中,允许存在多个名称相同、参数列表不同的方法。
编译器通过参数列表来区分这些方法,实现多态性。
(1)实验步骤1) 创建一个名为“Shape”的类,包含一个方法“draw()”,该方法不接受任何参数。
2) 在“Shape”类中添加三个方法:draw(String color),draw(String color, int size),draw(int x, int y)。
3) 创建一个名为“Circle”的类,继承自“Shape”类,并添加一个名为“draw”的方法,该方法接受一个int类型的参数radius。
4) 创建一个名为“Rectangle”的类,继承自“Shape”类,并添加一个名为“draw”的方法,该方法接受两个int类型的参数width和height。
5) 在主类中创建“Circle”和“Rectangle”对象,并调用它们的draw()方法。
(2)实验结果当执行程序时,根据传入的参数不同,调用不同的draw()方法。
3. 方法重写方法重写是指子类在继承父类的基础上,对父类的方法进行修改,实现多态性。
(1)实验步骤1) 创建一个名为“Animal”的类,包含一个名为“makeSound”的方法。
2) 创建一个名为“Dog”的类,继承自“Animal”类,并重写“makeSound”方法,使其输出“汪汪”。
3) 创建一个名为“Cat”的类,继承自“Animal”类,并重写“makeSound”方法,使其输出“喵喵”。
继承与多态实验报告继承与多态实验报告在面向对象编程中,继承和多态是两个重要的概念。
通过继承,我们可以创建新的类,并从现有的类中继承属性和方法。
而多态则允许我们使用父类的引用来指向子类的对象,实现同一操作具有不同的行为。
本实验旨在通过实际的编程实践,加深对继承和多态的理解。
实验一:继承在这个实验中,我们创建了一个动物类(Animal),并从它派生出了两个子类:狗类(Dog)和猫类(Cat)。
动物类具有一些共同的属性和方法,如名字(name)和发出声音(makeSound)。
子类继承了父类的属性和方法,并可以添加自己的特定属性和方法。
在编写代码时,我们首先定义了动物类,并在其中实现了共同的属性和方法。
然后,我们创建了狗类和猫类,并分别在这两个类中添加了自己的特定属性和方法。
通过继承,我们可以在子类中直接使用父类的方法,并且可以根据需要进行重写。
实验二:多态在这个实验中,我们使用多态的概念来实现一个动物园的场景。
我们定义了一个动物园类(Zoo),并在其中创建了一个动物数组。
这个数组可以存储不同类型的动物对象,包括狗、猫等。
通过多态,我们可以使用动物类的引用来指向不同类型的动物对象。
例如,我们可以使用动物类的引用来指向狗对象,并调用狗类特有的方法。
这样,我们可以统一处理不同类型的动物对象,而不需要为每种类型编写特定的处理代码。
实验三:继承与多态的结合应用在这个实验中,我们进一步探索了继承和多态的结合应用。
我们定义了一个图形类(Shape),并从它派生出了三个子类:圆形类(Circle)、矩形类(Rectangle)和三角形类(Triangle)。
每个子类都实现了自己的特定属性和方法。
通过继承和多态,我们可以在图形类中定义一些通用的方法,如计算面积和周长。
然后,我们可以使用图形类的引用来指向不同类型的图形对象,并调用相应的方法。
这样,我们可以轻松地对不同类型的图形进行统一的处理。
结论:通过本次实验,我们进一步理解了继承和多态的概念,并学会了如何在编程中应用它们。
多态性实验报告多态性实验报告引言:多态性是面向对象编程中的一个重要概念,它允许对象在不同的上下文中表现出不同的行为。
本实验旨在通过一系列的实验来探究多态性的概念和应用,以及它对程序设计的影响。
实验一:多态性的概念在本实验的第一部分,我们首先对多态性的概念进行了深入的研究。
多态性是指同一个方法在不同的对象上表现出不同的行为。
例如,在一个动物类中,不同的子类(如狗、猫、鸟)都可以实现一个叫声的方法,但是每个子类的叫声是不同的。
这种灵活性使得我们可以编写更加通用和可扩展的代码。
实验二:多态性的应用在第二个实验中,我们通过一个图形绘制的例子来展示多态性的应用。
我们创建了一个抽象的图形类,并派生出不同的子类,如圆形、矩形和三角形。
每个子类都实现了一个绘制方法,但是绘制的方式和结果是不同的。
通过将这些不同的子类对象存储在一个通用的图形数组中,我们可以轻松地遍历并绘制每个图形,而无需关心具体的子类类型。
实验三:多态性的优势和局限性在第三个实验中,我们深入研究了多态性的优势和局限性。
多态性使得代码更加灵活和可扩展,可以减少代码的重复性。
然而,过度使用多态性可能会导致代码的复杂性增加,降低程序的性能。
因此,在设计和实现中需要权衡多态性的使用。
实验四:多态性在实际项目中的应用在最后一个实验中,我们通过一个实际的项目来展示多态性的应用。
我们选择了一个图书管理系统作为例子,其中包括了不同类型的图书,如小说、教材和杂志。
通过使用多态性,我们可以统一管理这些不同类型的图书,并实现一套通用的借阅和归还功能。
这样,无论新增了多少种类型的图书,我们都可以轻松地扩展和修改代码。
结论:通过本次实验,我们深入了解了多态性的概念和应用,并通过一系列的实验来验证和探究多态性在程序设计中的作用。
多态性的使用可以使代码更加灵活和可扩展,但也需要在设计和实现中进行合理的权衡。
在实际项目中,多态性可以帮助我们更好地管理和处理不同类型的对象,提高代码的可维护性和可扩展性。
c++实验多态性实验报告C++实验多态性实验报告一、实验目的本次实验的主要目的是深入理解和掌握 C++中的多态性概念及其实现方式。
通过实际编程和运行代码,体会多态性在面向对象编程中的重要作用,提高编程能力和对 C++语言特性的运用水平。
二、实验环境本次实验使用的编程环境为 Visual Studio 2019,操作系统为Windows 10。
三、实验内容(一)虚函数1、定义一个基类`Shape`,其中包含一个虚函数`area()`用于计算图形的面积。
2、派生类`Circle` 和`Rectangle` 分别重写`area()`函数来计算圆形和矩形的面积。
(二)纯虚函数1、定义一个抽象基类`Vehicle`,其中包含一个纯虚函数`move()`。
2、派生类`Car` 和`Bicycle` 分别实现`move()`函数来描述汽车和自行车的移动方式。
(三)动态多态性1、创建一个基类指针数组,分别指向不同的派生类对象。
2、通过基类指针调用虚函数,观察多态性的效果。
四、实验步骤(一)虚函数实现1、定义基类`Shape` 如下:```cppclass Shape {public:virtual double area()= 0;};```2、派生类`Circle` 的定义及`area()`函数的实现:```cppclass Circle : public Shape {private:double radius;public:Circle(double r) : radius(r) {}double area(){return 314 radius radius;}};```3、派生类`Rectangle` 的定义及`area()`函数的实现:```cppclass Rectangle : public Shape {private:double length, width;public:Rectangle(double l, double w) : length(l), width(w) {}double area(){return length width;}```(二)纯虚函数实现1、定义抽象基类`Vehicle` 如下:```cppclass Vehicle {public:virtual void move()= 0;};```2、派生类`Car` 的定义及`move()`函数的实现:```cppclass Car : public Vehicle {public:void move(){std::cout <<"Car is moving on the road"<< std::endl;}};3、派生类`Bicycle` 的定义及`move()`函数的实现:```cppclass Bicycle : public Vehicle {public:void move(){std::cout <<"Bicycle is moving on the path"<< std::endl;}};```(三)动态多态性实现1、创建基类指针数组并指向不同的派生类对象:```cppShape shapes2;shapes0 = new Circle(50);shapes1 = new Rectangle(40, 60);```2、通过基类指针调用虚函数:```cppfor (int i = 0; i < 2; i++){std::cout <<"Area: "<< shapesi>area()<< std::endl;}```五、实验结果(一)虚函数实验结果运行程序后,能够正确计算出圆形和矩形的面积,并输出到控制台。
第1篇一、实验目的1. 理解多态性的概念和作用。
2. 掌握多态性的实现方法,包括方法重载、方法覆盖和构造函数重载。
3. 熟悉多态性在面向对象程序设计中的应用。
二、实验原理多态性是面向对象编程的一个重要特性,它允许同一操作作用于不同的对象上,产生不同的结果。
多态性主要分为两类:编译时多态和运行时多态。
1. 编译时多态:主要指方法重载,编译器在编译阶段就能确定调用哪个方法。
2. 运行时多态:主要指方法覆盖,在运行阶段才能确定调用哪个方法。
三、实验环境1. 操作系统:Windows 102. 开发工具:Java Development Kit (JDK) 1.83. 编译器:Eclipse四、实验步骤1. 创建一个父类`Animal`,包含一个抽象方法`makeSound()`和一个具体实现方法`makeSound()`。
2. 创建两个子类`Dog`和`Cat`,继承自`Animal`类,并分别重写`makeSound()`方法。
3. 在主类中创建一个`Animal`类型的数组,并初始化两个`Dog`对象和两个`Cat`对象。
4. 遍历数组,调用`makeSound()`方法,观察输出结果。
5. 创建一个`Animal`类型的引用,指向`Dog`对象,调用`makeSound()`方法,观察输出结果。
6. 创建一个`Animal`类型的引用,指向`Cat`对象,调用`makeSound()`方法,观察输出结果。
7. 修改`Animal`类中的`makeSound()`方法,增加一个参数,实现方法重载。
8. 在子类中重写`makeSound()`方法,增加一个新的参数,实现方法覆盖。
9. 在主类中创建一个`Animal`类型的引用,指向`Dog`对象,调用重载后的`makeSound()`方法,观察输出结果。
10. 在主类中创建一个`Animal`类型的引用,指向`Cat`对象,调用重载后的`makeSound()`方法,观察输出结果。
浙江理工大学信息学院实验指导书实验名称:类的多态性的实现学时安排: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容器。
五程序清单//main.cpp#include "class.h"#include<map>int main(){cout<<"请输入指定月份"<<endl;int month;cin>>month;Report re;re.insert(new Technicist("0001","王华","男",CDate(2,4,2011),"技术",9000));re.insert(new Technicist("0002","李明","女",CDate(4,10,2009),"技术",10000));map<int,int> bonus;bonus[1] = 1000;bonus[2] = 2000;bonus[3] = 3000;re.insert(new Manager("0003","朱黎明","男",CDate(11,8,2001),"经理",12000,bonus));bonus[1] = 500;bonus[2] = 1500;bonus[3] = 2000;re.insert(new Manager("0004","刘改云","男",CDate(8,7,2003),"经理",10000,bonus));map<int,int> sales;sales[1] = 200000;sales[2] = 100000;sales[3] = 500000;re.insert(new SalesPerson("0005","李志武","男",CDate(10,11,2007),"销售",5000,sales));re.print(month);return 0;}//class.h#ifndef CLASS_H_INCLUDED#define CLASS_H_INCLUDED#include<windows.h>#include<iostream>#include<string>#include<list>#include<vector>#include<map>#include"date.h"using namespace std;class Employee{protected:string name;string ID;string sex;string job;CDate date;double basicmoney;public:Employee(string ID,string name,string sex,CDate date,string job,double basicmoney);string getjob(){return job;}string getname(){return name;}string getID(){return ID;}string getsex(){return sex;}double getbasicmoney(){return basicmoney;}CDate getdate(){return date;}virtual double getpay(int m)=0;};class Report{private:list <Employee*> members;list <Employee*> operator[](string job);double min_pay(list<Employee*> emp_list ,int month);double max_pay(list<Employee*> emp_list ,int month);void print(list<Employee*> emp_list ,int month);public:~Report();void insert(Employee* p);void print(int n);};class Manager:public Employee{private:map<int,int> price;public:Manager(string ID,string name,string sex,CDate date,string job,double basicmoney,map<int,int> price):Employee( ID,name,sex,date,job,basicmoney){this->price=price;}double getpay(int m);};class Technicist:public Employee{public:Technicist(string ID,string name,string sex,CDate date,string job,double basicmoney):Employee(ID,name,sex,date,job,basicmoney){}double getpay(int m);};class SalesPerson:public Employee{private:map<int,int> sales;public:SalesPerson(string ID,string name,string sex,CDate date,string job,double basicmoney,map<int,int> sales):Employee(ID,name,sex,date,job,basicmoney){ this->sales=sales;}double getpay(int m);};#endif//fuctions.cpp#include "class.h"#include <iostream>#include <algorithm>using namespace std;Employee::Employee(string ID,string name,string sex,CDate date,string job,double basicmoney){this->ID= ID;this->name = name;this->sex = sex;this->date = date;this->basicmoney = basicmoney;this->job = job;}double Manager::getpay(int m){return price[m]+basicmoney;}list <Employee*> Report::operator[](string job){list<Employee*> cp;list<Employee*>::iterator it;for(it=members.begin();it!=members.end();it++){if((*it)->getjob()==job)cp.push_back(*it);}return cp;}void Report::print(int month){cout << " 第" << month << "月职工收入报表" <<endl;cout << "------------------------------------------------------" << endl;cout << "职位:经理" << endl;cout << "工号\t"<< "姓名\t"<< "性别\t"<< "入职时间\t"<< "基本工资\t"<< "薪酬"<< endl;list<Employee*> emp_ls;emp_ls = (*this)["经理"];print(emp_ls,month);cout << "最低薪酬:" << min_pay(emp_ls, month) << endl;cout << "最高薪酬:" << max_pay(emp_ls, month) << endl;cout << "------------------------------------------------------" << endl;cout << "职位:销售" << endl;cout << "工号\t"<< "姓名\t"<< "性别\t"<< "入职时间\t"<< "基本工资\t"<< "薪酬"<< endl;emp_ls = (*this)["销售"];print(emp_ls,month);cout << "最低薪酬:"<< min_pay(emp_ls, month) << endl;cout << "最高薪酬:"<< max_pay(emp_ls, month) << endl;cout << "------------------------------------------------------" << endl;cout << "职位:技术" << endl;cout << "工号\t"<< "姓名\t"<< "性别\t"<< "入职时间\t"<< "基本工资\t"<< "薪酬"<< endl;emp_ls = (*this)["技术"];print(emp_ls,month);cout << "最低薪酬:" << min_pay(emp_ls, month) << endl;cout << "最高薪酬:" << max_pay(emp_ls, month) << endl;}double Report::min_pay(list<Employee*> emp_list, int month){vector<double> pay;list<Employee*>::iterator it;for(it = emp_list.begin(); it != emp_list.end(); it ++){pay.push_back((*it)->getpay(month));}return *min_element(pay.begin(), pay.end()) ;}double Report::max_pay(list<Employee*> emp_list, int month){vector<double> pay;list<Employee*>::iterator it;for(it = emp_list.begin(); it != emp_list.end(); it ++) {pay.push_back((*it)->getpay(month));}return *max_element(pay.begin(), pay.end()) ;}void Report::print(list<Employee*> emp_list,int month){ list<Employee*>::iterator it;for(it = emp_list.begin();it != emp_list.end();it ++) {cout<<(*it)->getID()<<"\t"<<(*it)->getname()<<"\t";cout<<(*it)->getsex()<<"\t";cout<<(*it)->getdate().format("ddd")<<"\t";cout<<(*it)->getbasicmoney()<<"\t"<<"\t";cout<<(*it)->getpay(month) << endl;}}void Report::insert(Employee* p){members.push_back(p);}Report::~Report(){list<Employee*>::iterator it;for(it = members.begin(); it != members.end(); it ++){ delete *it;}}double Technicist::getpay(int m){return basicmoney;}double SalesPerson::getpay(int m){return basicmoney + sales[m] * 0.05;}//date.h//增加友元函数,重载<<friend ostream &operator<<(ostream &os,CDate &date) {os<<date.y<<"年"<<date.m<<"月"<<date.d<<"日"<<endl; return os;}六运行结果七实验心得此次试验涉及内容较多,主要是多态和STL容器的运用,我了解并运用了vector、list、map容器,并使用了迭代器对容器元素进行访问,复习了重载、继承的知识。