C++实验多态性实验报告
C++多态性实验报告含代码和结果截图

C++多态性实验报告含代码和结果截图实验报告课程:面向对象技术学号:姓名:班级:教师:计算机科学与技术系实验六多态性一、实验目的及要求1.掌握运算符重载的方法;2.掌握使用虚函数实现动态多态性。
二、实验环境硬件:计算机软件:Microsoft Visual C++三、实验内容声明一个车(vehicle)基类,有Run、Stop等成员函数,由此派生出自行车(bicycle)类、汽车(motorcar)类,从bicycle和motorcar派生出摩托车(motorcycle)类,它们都有Run、Stop等成员函数。
观察虚函数的作用。
四、实验结果(附截图)五、总结通过本次实验,我对虚函数、多态性有了进一步了解,对多态性也有了更深的认识,实验中还是有很多的问题不是很清楚,平时要认真学习好理论知识,这样才能在做实验时更好的理解代码,才能更快的改正自己调试时遇到的错误。
六、附录(源程序清单)#includeusing namespace std;int sign=0;class vehicle{vehicle(float m,float w){if(m<240&&m>0)MaxSpeed=m;else{cout<<"汽车超速!"<<endl;< p="">sign=1;return;}if(w<500&&w>0)Weight=w;else{cout<<"汽车超重!"<<endl;< p="">sign=1;return;}cout<<"构造了一个vehicle对象"<<endl;< p="">}virtual void Run() { cout<<"vehicle Run 函数被调用"<<endl;}< p="">virtual void Stop(){ cout<<"vehicle Stop 函数被调用"<<endl<<="">float MaxSpeed;float Weight;}class bicycle:virtual public vehicle{public:bicycle(float h,float m,float w):vehicle(m,w){if(h<1.5&&h>0)Height=h;elsecout<<"自行车超高!"<<endl;< p="">sign=1;return;}cout<<"构造了一个bicycle对象"<<endl;< p="">}void Run() { cout<<"bicycle Run 函数被调用"<<endl;}< p=""> void Stop(){ cout<<"bicycle Stop 函数被调用"<<endl<<endl;}< p="">private:float Height;}class motorcar:virtual public vehicle{public:motorcar(float s,float m,float w):vehicle(m,w){if(s<2&&s>0)SeatNum=s;else{cout<<"摩托车超载!"<<endl;< p="">sign=1;return;}cout<<"构造了一个motorcar对象"<<endl;< p="">}void Run() { cout<<"motorcar Run 函数被调用"<<endl;}< p="">void Stop(){ cout<<"motorcar Stop 函数被调用"<<endl<<endl;}< p="">private:float SeatNum;}class motorcycle:public bicycle,public motorcar{public:motorcycle(float h,float s,float m,float w):bicycle(h,m,w),motorcar(s,m,w),vehi cle(m,w){if(sign==0){cout<<"构造了一个motorcycle对象"<<endl;< p="">}}void Run() { cout<<"motorcycle Run 函数被调用"<<endl;}< p="">void Stop(){ cout<<"motorcycle Stop 函数被调用"<<endl<<endl;}< p="">};void main (){float m,w,h,s;int p;do{sign=0;cout<<"请输入参数:"<<endl<<endl;< p="">cout<<"汽车最高时速(km/h)";cin>>m;cout<<"汽车重量(t)";cin>>w;cout<<"自行车高度(m)";cin>>h;cout<<"摩托车座位(个)";cin>>s;motorcycle car(h,s,m,w);if(sign==0){car.Run();car.Stop();}else{cout<<"1—重新输入2——结束程序";cin>>p;if(p==2)return;elsecout<<endl<<endl;< p=""> }}while(sign==1);}</endl<<endl;<></endl<<endl;<></endl<<endl;}<></endl;}<></endl;<></endl<<endl;}<></endl;}<></endl;<></endl;<></endl<<endl;}<></endl;}<></endl;<></endl;<></endl<</endl;}<></endl;<></endl;<></endl;<>。
C++实验 多态性

C++程序设计实验名称:多态性专业班级:计科1402学号:2014006935学生姓名:陈志棚指导教师:王丽娟2016年5月8日一、实验目的:1、掌握C++中运算符重载的机制和运算符重载的方式;2、理解类型转换的必要性,掌握类型转换的使用方法;3、理解多态性,掌握虚函数的设计方法;4、学习使用visual studio 调试虚函数;二、案例:某小型公司,主要有三类人员:管理人员、计时人员和计件人员。
现在,需要存储这些人的姓名、编号、时薪、工时、每件工件薪金、工件数,计算月薪并显示全部信息。
三:程序及其运行结果:#include <iostream>#include <cstring>using namespace std;class Employee{ //雇员类private:int number;//编号string name;//姓名public:Employee(int nu,string na);int getnumber();string getname();virtual double earnings()=0;//计算工资virtual void display();//显示员工信息};class Manager:public Employee{ //管理员类private:double monthlySalary;//月薪public:Manager(int nu,string na,double ms=0.0);void setMonthlySalary(double sms);//置月薪virtual double earnings();//计算月薪virtual void display();//显示所有信息};class HourlyWorker:public Employee{private:double wage;//时薪int hours;//工时public:HourlyWorker(int nu,string na,double w=0,int h=0);void setWage(double w);//置时薪void setHours(int h);//置工时virtual double earnings();//计算月薪virtual void display();//显示所有信息};class PieceWorker:public Employee{//计件人员类private:double wagePerPiece;//每件工件薪金int quantity;//工件数public:PieceWorker(int nu,string na,double wp=0.0,int q=0);void setWagePerPiece(double wp);//置每件工件薪金void setQuantity(int q);//置工件数virtual double earnings();//计算月薪virtual void display();//显示所有信息};int main(){Manager m1(101,"Chen Xiao",10000);Manager m2(102,"Chen YanXi");m2.setMonthlySalary(12000);HourlyWorker hw1(201,"Zhou Rong",10,100);HourlyWorker hw2(202,"Yang Zi");hw2.setWage(8);hw2.setHours(120);PieceWorker pw1(301,"Huo JianHua",1.5,1000);PieceWorker pw2(302,"Zhao LiYin");pw2.setWagePerPiece(1.8);pw2.setQuantity(800);Employee *p;p=&m1;p->display();p=&m2;p->display();p=&hw1;p->display();p=&hw2;p->display();p=&pw1;p->display();p=&pw2;p->display();return 0;}//****************************************雇员类中函数定义Employee::Employee(int nu,string na){number=nu;name=na;}int Employee::getnumber(){return number;}string Employee::getname(){return name;}void Employee::display(){cout<<"编号:"<<number<<" 姓名:"<<name<<endl;}//****************************************管理员类中函数定义Manager::Manager(int nu,string na,double ms):Employee(nu,na){monthlySalary=ms;}void Manager::setMonthlySalary(double sms){monthlySalary=sms;}double Manager::earnings(){return monthlySalary;}void Manager::display(){cout<<"编号:"<<Employee::getnumber()<<" 姓名:"<<Employee::getname()<<" 月薪:"<<monthlySalary<<endl;}//*****************************************************计时人员类中函数定义HourlyWorker::HourlyWorker(int nu,string na,double w,int h):Employee(nu,na){wage=w;hours=h;}void HourlyWorker::setWage(double w){wage=w;}void HourlyWorker::setHours(int h){hours=h;}double HourlyWorker::earnings(){return wage*hours;}void HourlyWorker::display(){cout<<"编号:"<<Employee::getnumber()<<" 姓名:"<<Employee::getname()<<" 时薪:"<<wage<<" 工时:"<<hours<<" 月薪:"<<HourlyWorker::earnings()<<endl; }//*****************************************************计件人员类中函数定义PieceWorker::PieceWorker(int nu,string na,double wp,int q):Employee(nu,na){wagePerPiece=wp;quantity=q;}void PieceWorker::setWagePerPiece(double wp){wagePerPiece=wp;}void PieceWorker::setQuantity(int q){quantity=q;}double PieceWorker::earnings(){return wagePerPiece*quantity;}void PieceWorker::display(){cout<<"编号:"<<Employee::getnumber()<<" 姓名:"<<Employee::getname()<<" 每件工件薪金:"<<wagePerPiece<<" 工件数:"<<quantity<<" 月薪:"<<PieceWorker::earnings()<<endl;}运行结果:四:分析与总结:1、通过该程序让我更加了解C++的多态性,熟悉虚函数的定义和使用;2、进一步熟悉函数的继承以及继承的特点!。
实验报告多态性

一、实验目的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”方法会根据对象类型输出相应的移动信息。
C++程序设计实验报告(多态性)

西安科技大学《C/C++语言程序设计》实验报告题目)___院、系(部) ___________专业及班级 ________姓名 _________ 学号日期 ____一系统功能分析用同一个名字来访问不同函数的性质即为多态性,使用多态性,一些功能相似的函数可用同一个名字来定义,这样会使得概念更清晰,还可达到动态链表的目的,实现运行的多态性以下程序使用了函数的重载,其使用对象名加以区分,还是用:类名::加以区分。
二总体设计学生系统分析图三详细设计1 首先,定义一个类名为student的类class Student{char name[20];int num;int age;public:void Build();void Delete();void correct();void seek();Student(){strcpy(name,"");num=0;age=0;}Student(char nam[20],int nu,int ag){strcpy(name,nam);num=nu;age=ag;}void show(){cout<<"name:"<<name[20]<<endl;cout<<"num:"<<num<<endl;cout<<"age:"<<age<<endl;}~Student(){}};2 然后,在外面有各种功能函数的实现函数 Student *p[100];int i=0;int j=0;char name[20];int num;int age;void Student::Build(){Student *p[100];cout<<"name:"<<endl;cin>>p[i]->name[20];cout<<"num:"<<endl;cin>>p[i]->num;cout<<"age:"<<endl;cin>>p[i]->age;j++;p[i]=new Student();i++;}void Student::Delete(){cout<<"请输入您要删除的学生到底姓名:"<<endl; cin>>name;for(int s=0;s<i;s++){if(strcmp(name,p[s]->name)==0){i--;p[s]=p[s+1];s++;}cin.clear();}}void Student::correct(){cout<<"请输入您要改正学生信息的学生姓名:"<<endl;cin>>name[20];// char name[20];int num;int age;for(int s=0;s<i;s++){if(strcmp(name,p[s]->name)==0){cout<<"name:"<<p[s]->name<<endl;cout<<"num:"<<p[s]->num<<endl;cout<<"age:"<<p[s]->age<<endl;}}}void Student::seek(){//char name[20],int num,int age;cout<<"请输入学生的姓名:"<<endl;cin>>name[20];for(int s=0;s<i;s++){if(strcmp(name,p[s]->name)==0){cout<<"该学生的信息为:"<<endl;cout<<"name:"<<name[20]<<endl;cout<<"num:"<<num<<endl;cout<<"age:"<<age<<endl;break;}}}3 在函数yunxing中对上述函数进行调用void yunxing(int n){Student m;switch(n){case 1:m.Build();break;case 2:m.Delete();break;case 3:m.correct();break;case 4:m.seek();break;}4 在main函数中写出欢迎界面和初始化语句Student s1("lili",1101,19);Student s2("wangfang",1102,20);Student s3("wangmeng",1103,18);Student s4("linan",1104,21);Teacher t1("liangshaohui",1105,30,5000,"gaoshu");Teacher t2("liuwei",1106,29,6000,"dawu");Teacher t3("yuyawei",1107,28,7000,"lisan");Teacher t4("chenming",1108,27,8000,"yingyu");s1.show();s2.show();s3.show();s4.show();t1.show();t2.show();t3.show();t4.show();t1.Student::show();t2.Student::show();t3.Student::show();t4.Student::show();cout<<" 菜单界面 \n"<<endl;cout<<" 1:新建用户信息 \n"<<endl;cout<<" 2:删除用户信息 \n"<<endl;cout<<" 3:改正用户信息 \n"<<endl;cout<<" 4:查找用户信息 \n"<<endl;cout<<"请选择你要选择的项目! "<<endl;四测试与实现五总结之前我们用C语言编写的程序-学生管理系统是非常繁琐的,后来我们学习了C++语言,学习了类与对象,学习了继承与派生,最后学习了多态性,一步步的我们在原先的学生管理系统中添加了这些函数,可以明显的感觉到程序在一步步简化,一步步完善,这便突出了C++语言的简捷。
c 多态性的实验报告

c 多态性的实验报告C++多态性的实验报告引言:多态性是面向对象编程中的一个重要概念,它允许我们使用统一的接口来处理不同类型的对象。
在C++中,多态性通过虚函数和继承来实现。
本实验旨在通过一个简单的实例来演示C++多态性的使用方法和效果。
实验目的:1. 理解多态性的概念和原理;2. 掌握在C++中实现多态性的方法;3. 通过实例了解多态性的实际应用。
实验步骤:1. 创建基类Animal和派生类Dog、Cat;2. 在基类中声明虚函数"makeSound()",并在派生类中实现该函数;3. 在主函数中创建Animal类型的指针数组,分别指向Dog和Cat对象;4. 通过循环调用虚函数"makeSound()",观察不同对象的行为。
实验结果:在本实验中,我们创建了一个基类Animal和两个派生类Dog、Cat。
Animal类中声明了虚函数"makeSound()",而Dog和Cat类分别实现了该函数。
在主函数中,我们创建了一个Animal类型的指针数组,分别指向Dog和Cat对象。
通过循环调用虚函数"makeSound()",我们可以观察到不同对象的行为。
实验分析:1. 多态性的实现:多态性通过虚函数和继承来实现。
在本实验中,通过将基类中的函数声明为虚函数,我们可以在派生类中重写该函数,从而实现多态性。
通过使用基类指针数组,我们可以在运行时动态地确定调用哪个对象的函数。
2. 多态性的优势:多态性使得程序更加灵活和可扩展。
通过使用统一的接口处理不同类型的对象,我们可以减少代码的重复性,提高代码的可读性和可维护性。
此外,多态性还支持运行时的动态绑定,使得程序可以根据实际情况来决定调用哪个函数,从而更好地适应不同的需求。
3. 多态性的应用:多态性在实际应用中有着广泛的应用。
例如,在图形界面程序中,我们可以通过多态性来处理不同类型的控件;在游戏开发中,我们可以通过多态性来处理不同类型的角色和敌人;在工程设计中,我们可以通过多态性来处理不同类型的零件和设备。
多态性和虚函数 实验报告

淮海工学院计算机科学系实验报告书课程名:《 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、结果分析与实验体会继承时,子类对基类的访问属性,基类的私有成员无论以何种方式继承在子类中都是不可访问的,唯有调用基类中的成员函数方可访问其私有变量。
c 多态性实验报告
c 多态性实验报告C++多态性实验报告引言:多态性是面向对象编程中的一个重要概念,它允许我们以一种统一的方式处理不同类型的对象。
在C++中,多态性通过虚函数和指针或引用来实现。
本实验旨在探索C++中多态性的实际应用,并通过实验结果来验证其效果。
实验步骤:1. 创建基类和派生类:首先,我们创建一个基类Animal,其中包含一个虚函数makeSound()用于发出动物的声音。
然后,我们创建两个派生类Dog和Cat,它们分别继承自Animal 类,并实现自己的makeSound()函数。
2. 创建动态数组:接下来,我们创建一个动态数组,其中包含不同类型的动物对象。
这样我们就可以在一个数组中存储不同类型的对象,并以统一的方式处理它们。
3. 调用虚函数:通过使用基类指针或引用,我们可以调用派生类中的虚函数。
这样,无论基类指针指向的是派生类的对象还是基类的对象,都可以正确地调用派生类的函数。
我们可以通过遍历动态数组来调用每个动物对象的makeSound()函数,并观察到不同类型的动物发出不同的声音。
实验结果:我们创建了一个动态数组,其中包含了两个Dog对象和两个Cat对象。
通过遍历数组并调用makeSound()函数,我们观察到每个动物对象都发出了与其类型相对应的声音。
这证明了多态性的实际应用,即通过统一的接口处理不同类型的对象。
讨论与总结:多态性是面向对象编程中的重要概念,它提供了一种灵活的方式来处理不同类型的对象。
通过使用虚函数和基类指针或引用,我们可以以统一的方式处理派生类对象,并实现动态绑定。
这种灵活性使得我们的代码更加可扩展和可维护。
然而,多态性也带来了一些性能开销。
由于需要在运行时确定函数的调用地址,多态性可能会导致一定的性能损失。
因此,在实际编程中,我们需要根据具体情况来权衡使用多态性的利与弊。
总之,本实验通过实际应用验证了C++中多态性的效果,并讨论了其优缺点。
多态性是面向对象编程中的重要概念,对于提高代码的可扩展性和可维护性具有重要意义。
c多态实验报告
c多态实验报告C 多态实验报告一、实验目的本次实验旨在深入理解和掌握 C 语言中的多态特性,通过实际编程和运行示例程序,观察和分析多态的行为和效果,提高对 C 语言编程的理解和应用能力。
二、实验环境操作系统:Windows 10编译器:GCC 920三、实验原理1、函数指针函数指针是指向函数的指针变量。
通过函数指针,可以实现对不同函数的动态调用,为多态的实现提供了基础。
2、虚函数表在 C 语言中,通常通过手动构建类似虚函数表的结构来模拟多态。
虚函数表是一个存储函数指针的数组,每个对象都包含一个指向其所属类的虚函数表的指针。
四、实验内容1、简单函数指针示例```cinclude <stdioh>//定义两个函数void function1(){printf("This is function 1\n");}void function2(){printf("This is function 2\n");}int main(){//定义函数指针void (ptr)();ptr = function1;ptr();ptr = function2;ptr();return 0;}```在上述示例中,定义了两个函数`function1` 和`function2`,然后通过函数指针`ptr` 分别指向这两个函数,并进行调用。
2、模拟虚函数示例```cinclude <stdioh>//定义基类typedef struct base_class {void (virtual_function)();} BaseClass;//定义派生类 1typedef struct derived_class1 {BaseClass base;} DerivedClass1;//定义派生类 2typedef struct derived_class2 {BaseClass base;} DerivedClass2;//为基类的虚函数赋值void base_function(){printf("This is the base function\n");}//为派生类 1 的虚函数赋值void derived1_function(){printf("This is the derived class 1 function\n");}//为派生类 2 的虚函数赋值void derived2_function(){printf("This is the derived class 2 function\n");}int main(){//创建派生类 1 对象DerivedClass1 d1;d1basevirtual_function = derived1_function;//创建派生类 2 对象DerivedClass2 d2;d2basevirtual_function = derived2_function;//通过基类指针调用虚函数BaseClass ptr =(BaseClass )&d1;ptr>virtual_function();ptr =(BaseClass )&d2;ptr>virtual_function();return 0;}```在这个示例中,模拟了类的继承和虚函数的重写。
c运算符重载和多态性实验报告
实验5 运算符重载和多态性一、实验目的1.掌握用成员函数重载运算符的方法2.掌握用友元函数重载运算符的方法3.理解并掌握利用虚函数实现动态多态性和编写通用程序的方法4.掌握纯虚函数和抽象类的使用二、实验内容1.复数类加减法乘除运算 (用成员函数定义运算符重载)。
复数类的定义:class complex //复数类声明{ public: //外部接口complex(double r=0.0,double i=0.0) //构造函数{real=r,imag=i;}complex operator +(complex c2); //运算符"+"重载成员函数complex operator - (complex c2); //运算符"-"重载成员函数complex operator *(complex ); //运算符"*"重载成员函数complex operator /(complex); //运算符"/"重载成员函数complex operator =(complex c2); //运算符"="重载成员函数void display(); //输出复数private: //私有数据成员double real; //复数实部double imag; //复数虚部};实验代码:#include <iostream>using namespace std;class Complex{public:Complex(){real=0;imag=0;}Complex(double r,double i){real=r;imag=i;}Complex operator+(Complex &c2); Complex operator-(Complex &c2); Complex operator*(Complex &c2); Complex operator/(Complex &c2); void display();private:double real; double imag;};Complex Complex::operator+(Complex &c2) {Complex c;c.real=real+c2.real;c.imag=imag+c2.imag; return c;}Complex Complex::operator-(Complex &c2) {Complex c;c.real=real-c2.real;c.imag=imag-c2.imag;return c;}Complex Complex::operator*(Complex &c2) {Complex c;c.real=real*c2.real-imag*c2.imag;c.imag=imag*c2.real+real*c2.imag; return c;}Complex Complex::operator/(Complex &c2){Complex c;c.real=(real*c2.real+imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag );c.imag=(imag*c2.real-real*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);return c;}void Complex::display(){cout<<"("<<real<<","<<imag<<"i)"<<endl;}int main(){Complex c1(4,6),c2(3,7),c3;cout<<"c1=";c1.display();cout<<"c2=";c2.display();int i,j=1;while(j){cout<<"\n";cout<<"\t\t"<<"1.复数之和\n"; cout<<"\t\t"<<"2.复数之差\n"; cout<<"\t\t"<<"3.复数之积\n"; cout<<"\t\t"<<"4.复数之商\n"; cout<<"\t\t"<<"0.退出\n"; cout<<"请选择(0--4)"; cin>>i;switch(i){case 1: c3=c1+c2;cout<<"c1+c2=";c3.display(); break;case 2: c3=c1-c2;cout<<"c1-c2=";c3.display();break;case 3: c3=c1*c2;cout<<"c1*c2=";c3.display();break;case 4: c3=c1/c2;cout<<"c1/c2=";c3.display();break;case 0: j=0;break;}}}测试结果:2.复数类比较运算 (用友元函数定义运算重载)。
C 实验多态性实验报告
class Point { public:
Point(int xx,int yy):x(xx),y(yy) {} void display()const; Point &operator++(); Point operator++(int); Point &operator--(); Point operator--(int); private:
using namespace std;
int Double(int x);
long Double(long x);
float Double(float x);
double Double(double x);
int main()
{ int myInt = 6500;
cout<<Double(myInt)<<endl;
学习使用虚函数实现动态多态性。而虚函数就是在基类中被关键字 virtual 说明,
实 并在派生类中重新定义的函数,且在派生类中重工业新定义时,函数原型,包括返回
类型、函数名、参数个数与参数类型的顺序,都必须与基类中的完全相同。此外,构 验
造函数不能是虚函数,但析构函数可以是虚函数。
总
函数的重载方法有一参数个数相同,但是类型不同;二参数个数不同;三 coust
实
验 Visual C++的编译环境下,独立完成实验要求的内容,独立完成编写、编译以及运行
原
的过程
理
实
验 安装了 Visual C++的 PC 机器
仪
器
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
贵州大学实验报告
学院:电子信息学院专业:通信工程班级:
姓名学号实验组5实验时间指导教师成绩
实验项目名称多态性
实
验通过让学生进行实验,使其对于动态多态性有一个较为深入的了解和熟悉。
最终可以目熟练使用。
的
实
1.编写 4 个重载函数 Double( x),返回值为输入参数的两倍;参数类型分别为int 、验long 、float 、 double ,返回值类型与参数类型一样。
2.请编写一个抽象类Shape,在此基础上派生出类Rectangle和Circle,二者都有
要
计算对象面积的函数GetArea ()和计算周长函数GetPerim ()。
求3.对类 Point 重载 ++(自增)、 -- (自减)运算符。
实
验Visual C++的编译环境下,独立完成实验要求的内容,独立完成编写、编译以及运行
原的过程
理
实
验
安装了 Visual C++ 的 PC机器
仪
器
实
验
按照实验要求的内容逐一完成实验的要求。
顺序是编写、编译、运行。
步
骤
实 1. 编写 4 个重载函数 Double(x),返回值为输入参数的两倍;参数类型分别为int、
验
long 、 float 、 double ,返回值类型与参数类型一样。
2. 请编写一个抽象类Shape,在此基础上派生出类Rectangle 和 Circle,二者都有计
内算对象面积的函数GetArea ()和计算周长函数GetPerim ()。
容 3. 对类 Point 重载 ++(自增)、 -- (自减)运算符。
1、代码如下:
#include<iostream>
using namespace std;
int Double(int x);
long Double(long x);
float Double(float x);
double Double(double x);
int main()
{ int myInt = 6500;
cout<<Double(myInt)<<endl;
long myLong = 65000;
cout<<Double(myLong)<<endl;
float myFloat = 6.5F;
cout<<Double(myFloat)<<endl;
double myDouble = 6.5e20;
实
cout<<Double(myDouble)<<endl;}
验int Double(int x){return 2*x;}
数long Double(long x){return 2*x;} float Double(float x){return 2*x;}
据double Double(double x){return 2*x;}运行结果:
2、代码:
#include<iostream>
#define PI 3.1415926;
using namespace std;
class Shape // 抽象类的定义
{
public:
virtual double GetArea()= 0; //纯虚函数
virtual double GetPerim()= 0; };
class Rectangle : public Shape
// 纯虚函数
// 矩形类,公有继承
{
public: Rectangle(double aa, double bb)// 带参数的构造函数{
a=aa;
b=bb;
cout<<" 长 "<<a<<" 宽 "<<b<<endl;
}
virtual double GetArea()
{
return a * b;
}
virtual double GetPerim()
{
return 2*( a + b );
}
private:
double a;
double b;
};
class Circle : public Shape//圆类,公有继承
{
public: Circle(double rr)// 带参数的构造函数{
r=rr;
cout<<" 半径 "<<r<<endl;
}
virtual double GetArea()
{
return r * r * PI;
}
virtual double GetPerim()
{
return 2 * r * PI;
}
private:
double r;
};
void main()
{
double length, width;
cout << " 输入长和宽 : ";
cin >> length >> width;
Rectangle rect(length, width);
cout << " 面积是: "<< rect.GetArea() << endl<<" 周长是: "<<rect.GetPerim()<<endl;
double rr;
cout << " 输入半径 : ";
cin >> rr;
Circle cir(rr);
cout << " 面积是: "<<cir.GetArea() << endl<<"周长是:"<<cir.GetPerim()<<endl; }
运行结果:
3、代码如下:
#include<iostream.h>
class Point
{
public:
Point(int xx,int yy):x(xx),y(yy) {}
void display()const;
Point &operator++();
Point operator++(int);
Point &operator--();
Point operator--(int);
private:
int x,y;
};
void Point::display()const
{
cout<<" 当前 Point("<<x<<","<<y<<")"<<endl;
}
Point &Point::operator++()
{
x++;
y++;
cout<<" 执行 x++,y++ 操作 !"<<endl;
return *this;
}
Point Point::operator++(int)
{
cout<<" 执行 ++x,++y 操作 !"<<endl;
return Point(++x,++y);
}
Point &Point::operator--(){x--;y--;
cout<<" 执行 x--,y-- 操作 !"<<endl; return *this;
}
Point Point::operator--(int)
{cout<<" 执行 --x,--y 操作 !"<<endl; return Point(--x,--y);
}
int main()
{
int x,y;
cout<<"Input x&y:";
cin>>x>>y;
Point point1(x,y);
point1.display();point1++;
point1.display();++point1;
point1.display();point1--;
point1.display();--point1;
point1.display();return 0;
}
运行结果:
学习使用虚函数实现动态多态性。
而虚函数就是在基类中被关键字 virtual 说明,实并在派生类中重新定义的函数,且在派生类中重工业新定义时,函数原型,包括返回
类型、函数名、参数个数与参数类型的顺序,都必须与基类中的完全相同。
此外,构
验
造函数不能是虚函数,但析构函数可以是虚函数。
总函数的重载方法有一参数个数相同,但是类型不同;二参数个数不同;三coust (常量)。
结
指
导
教
师
意
见签名:年月日
注:各学院可根据教学需要对以上栏木进行增减。
表格内容可根据内容扩充。