欢迎来到天天文库
浏览记录
ID:48263705
大小:45.51 KB
页数:7页
时间:2019-12-04
《实验6多态性与虚函数》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、《C++面向对象程序设计》实验报告序号:实验6多态性与虚函数[实验目的]1、了解多态性的概念;2、了解虚函数的用途及使用方法;3、了解纯虚函数和抽象类的概念和用法。[实验要求]给出以下各实验内容的源程序代码,并把编译、运行过程中出现的问题以及解决方法填入实验报告中,按时上交。[实验学时]2学时。[实验内容]1、写一个程序,定义抽象基类Shape,由它派生出3个派生类:Circle(圆形)、Square(正方形)、Rectangle(矩形)。利用指针、虚函数printArea()分别输出以上三者的面积,3个图形的数据在定义对象时给定。[
2、源程序]#includeusingnamespacestd;classShape{public:virtualfloatarea()const=0;virtualvoiddisplay()const=0;};classCircle:publicShape{public:Circle(doublea):r(a){}virtualfloatarea()const{return3.14rr;}virtualvoiddisplay()const{cout<<"圆面积"<3、ler;};第7页共1页《C++面向对象程序设计》实验报告classRectangle:publicShape{public:Rectangle(doublea,doubleb):l(a),w(b){}virtualfloatarea()const{returnlw;}virtualvoiddisplay()const{cout<<"矩形面积"<4、irtualfloatarea()const{returna1a1;}virtualvoiddisplay()const{cout<<"正方形面积"<display();m=m+p[i]->area();}第7页共1页《C++面向对象程序5、设计》实验报告cout<<"总面积:"<6、cted:floatx,y;};//Point.cpp#include#include"Point.h"Point::Point(floata,floatb){x=a;y=b;}voidPoint::display(){cout<<"Point:"<7、0);voiddisplay();protected:floatradius;};//Circle.cpp#include#include"Point.h"#include"Circle.h"Circle::Circle(floata,floatb,floatr):Point(a,b),radius(r){}voidCircle::display(){cout<<"Circle:"<8、dius<
3、ler;};第7页共1页《C++面向对象程序设计》实验报告classRectangle:publicShape{public:Rectangle(doublea,doubleb):l(a),w(b){}virtualfloatarea()const{returnlw;}virtualvoiddisplay()const{cout<<"矩形面积"<
4、irtualfloatarea()const{returna1a1;}virtualvoiddisplay()const{cout<<"正方形面积"<display();m=m+p[i]->area();}第7页共1页《C++面向对象程序
5、设计》实验报告cout<<"总面积:"<6、cted:floatx,y;};//Point.cpp#include#include"Point.h"Point::Point(floata,floatb){x=a;y=b;}voidPoint::display(){cout<<"Point:"<7、0);voiddisplay();protected:floatradius;};//Circle.cpp#include#include"Point.h"#include"Circle.h"Circle::Circle(floata,floatb,floatr):Point(a,b),radius(r){}voidCircle::display(){cout<<"Circle:"<8、dius<
6、cted:floatx,y;};//Point.cpp#include#include"Point.h"Point::Point(floata,floatb){x=a;y=b;}voidPoint::display(){cout<<"Point:"<7、0);voiddisplay();protected:floatradius;};//Circle.cpp#include#include"Point.h"#include"Circle.h"Circle::Circle(floata,floatb,floatr):Point(a,b),radius(r){}voidCircle::display(){cout<<"Circle:"<8、dius<
7、0);voiddisplay();protected:floatradius;};//Circle.cpp#include#include"Point.h"#include"Circle.h"Circle::Circle(floata,floatb,floatr):Point(a,b),radius(r){}voidCircle::display(){cout<<"Circle:"<8、dius<
8、dius<
此文档下载收益归作者所有