欢迎来到天天文库
浏览记录
ID:36591093
大小:54.00 KB
页数:5页
时间:2019-05-12
《C实验报告-实验4继承与派生》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、实验4继承与派生一、实验目的和要求(1)掌握派生类的定义方法和派生类构造函数的定义方法。(2)掌握不同继承方式的情况下,基类成员在派生类中的访问权限。(3)掌握在多继承方式的情况下,构造函数与析构函数的调用时机与顺序。二、实验内容和原理(1)实验指导书P861~2任选一题。(2)实验指导书P893~4任选一题。三、实验环境联想计算机,WindowsXP操作系统,VisualC++6.0四、算法描述及实验步骤(1)输入源程序。(2)检查程序有无错误(包括语法错误和逻辑错误),有则改之。(3)编译和连接,仔细分析编译信息,如有错误应找出原因并改正之。(4)运行程
2、序,分析结果,在原有程序得出正确结果后,修改程序,将其改写为在类模板外定义,再按第(3)步骤运行。(5)将调试好的程序保存在自己的用户目录中,文件名自定。五、调试过程(1)2(2)4六、实验结果(1)2(2)4七、总结(1)掌握构造函数和析构函数的定义方法(2)构造函数调用顺序:先调用所有基类的构造函数,然后调用派生类的构造函数;(3)析构函数调用顺序:先调用派生类的析构函数,然后调用基类的析构函数,其顺序正好与构造函数调用顺序相反。八、附录:(1)代码如下#includeusingnamespacestd;classBase1{publ
3、ic:Base1(){cout<<"constructingBase1"<4、endl;}~Derived1(){cout<<"destructingDerived1"<5、structingDerived3"<usingnamespacestd;constdoublePI=3.14;classShape{public:doublearea()const{return0.0;}voiddisplay(){};};classTwoDimShape:virtualpublicShape{};classThree6、DimShape:virtualpublicShape{};classCircle:publicTwoDimShape{public:Circle(doublemyr){R=myr;}doublearea()const{returnPI*R*R;}voiddisplay(){cout<<"Areaofcircleis";}private:doubleR;};classRectangle:publicTwoDimShape{public:Rectangle(doublemyl,doublemyw){L=myl;W=myw;}doublearea()const{7、returnL*W;}voiddisplay(){cout<<"Areaofrectangleis";}private:doubleL,W;};classCylinder:publicThreeDimShape{public:Cylinder(doublemyr,doublemyh){R=myr;H=myh;}doublearea()const{return2*PI*R*(H+R);}voiddisplay(){cout<<"superficialareaofcylinderis";}private:doubleR,H;};classCube:publicT8、hreeDimShape{public:Cube(d
4、endl;}~Derived1(){cout<<"destructingDerived1"<5、structingDerived3"<usingnamespacestd;constdoublePI=3.14;classShape{public:doublearea()const{return0.0;}voiddisplay(){};};classTwoDimShape:virtualpublicShape{};classThree6、DimShape:virtualpublicShape{};classCircle:publicTwoDimShape{public:Circle(doublemyr){R=myr;}doublearea()const{returnPI*R*R;}voiddisplay(){cout<<"Areaofcircleis";}private:doubleR;};classRectangle:publicTwoDimShape{public:Rectangle(doublemyl,doublemyw){L=myl;W=myw;}doublearea()const{7、returnL*W;}voiddisplay(){cout<<"Areaofrectangleis";}private:doubleL,W;};classCylinder:publicThreeDimShape{public:Cylinder(doublemyr,doublemyh){R=myr;H=myh;}doublearea()const{return2*PI*R*(H+R);}voiddisplay(){cout<<"superficialareaofcylinderis";}private:doubleR,H;};classCube:publicT8、hreeDimShape{public:Cube(d
5、structingDerived3"<usingnamespacestd;constdoublePI=3.14;classShape{public:doublearea()const{return0.0;}voiddisplay(){};};classTwoDimShape:virtualpublicShape{};classThree
6、DimShape:virtualpublicShape{};classCircle:publicTwoDimShape{public:Circle(doublemyr){R=myr;}doublearea()const{returnPI*R*R;}voiddisplay(){cout<<"Areaofcircleis";}private:doubleR;};classRectangle:publicTwoDimShape{public:Rectangle(doublemyl,doublemyw){L=myl;W=myw;}doublearea()const{
7、returnL*W;}voiddisplay(){cout<<"Areaofrectangleis";}private:doubleL,W;};classCylinder:publicThreeDimShape{public:Cylinder(doublemyr,doublemyh){R=myr;H=myh;}doublearea()const{return2*PI*R*(H+R);}voiddisplay(){cout<<"superficialareaofcylinderis";}private:doubleR,H;};classCube:publicT
8、hreeDimShape{public:Cube(d
此文档下载收益归作者所有