收藏 分销(赏)

实验八继承与派生虚函数.doc

上传人:仙人****88 文档编号:9313137 上传时间:2025-03-21 格式:DOC 页数:4 大小:37.50KB
下载 相关 举报
实验八继承与派生虚函数.doc_第1页
第1页 / 共4页
实验八继承与派生虚函数.doc_第2页
第2页 / 共4页
点击查看更多>>
资源描述
实验八 派生与继承-虚函数 一、实验目的 1、学习定义和使用类的继承关系,定义派生类。 2、熟悉不同继承方式下对基类成员的访问控制。 3、学习利用虚基类解决二义性问题。 4、学习使用虚函数实现动态多态性。 二、实验任务 1、定义一个shape类如下, //shape.h #ifndef SHAPE #define SHAPE #include <iostream> using namespace std; const double PI=3.14159; class shape { virtual double area()=0; virtual double circumference()=0; //周长 }; #endif 在此基础上派生有矩形和圆形两种图形,求它们各自的面积和周长。设计各个图形求面积和周长的方法。在此基础上派生出长方体、圆柱体,并求出它们的体积和表面积。 已各个类的头文件,要求实现各个类 //rectangle.h 矩形类 #ifndef RECTANGLE #define RECTANGLE #include "shape.h" class rectangle:public shape//矩形 { private: double length; double width; public: rectangle(double leng,double wid); double area(); virtual double get_length(); virtual double get_width(); double circumference();//周长 void display(); }; #endif //circle.h 圆类 #ifndef CIRCLE #define CIRCLE #include "shape.h" class circle:public shape//圆形 { private: double radius; public: circle(double r); double area(); virtual double get_radius(); double circumference();//周长 void display(); }; #endif //cylinder.h 圆柱体类 #ifndef CYLINDER #define CYLINDER #include "circle.h" class cylinder:public circle//圆柱体 { private: double height; public: cylinder(double r,double hei); double get_height(); double volume(); double facearea(); void display(); }; #endif //cuboid.h 长方体类 #ifndef CUBOID #define CUBOID #include "rectangle.h" class cuboid:public rectangle//长方体 { private: double height; public: cuboid(double leng,double wid,double hei); double volume(); double facearea(); virtual double get_height(); void display(); }; #endif ////main #include <iostream> #include "shape.h" #include "circle.h" #include "rectangle.h" #include "cylinder.h" #include "cuboid.h" using namespace std; int main() { cuboid cu1(2,3,4); cu1.display (); cout<<"当前对象的表面积:"<<cu1.facearea ()<<endl; cout<<"当前对象的体积:"<<cu1.volume ()<<endl; cylinder cy1(2,3); cy1.display (); cout<<"当前对象的表面积:"<<cy1.facearea ()<<endl; cout<<"当前对象的体积:"<<cy1.volume ()<<endl; return 0; }
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传
相似文档                                   自信AI助手自信AI助手

当前位置:首页 > 教育专区 > 小学其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服