1、实验三继承与派生实验三-继承与派生本科实验报告课程名称: C+面向对象程序设计 实验项目: 实验地点: 明向校区 专业班级: 软件1419 学号: 2014006061 学生姓名: 刘国鑫 指导教师: 王丽娟 2015年5月10日实验名称 实验三 继承与派生实验目的1. 理解继承与派生的概念;2. 掌握派生类定义格式与使用方法;3. 初步掌握派生类构造函数的定义与使用方法,理解构造函数的调用过程,及基类成员的初始化过程;4. 理解冲突、支配规则与赋值兼容性原则的概念;实验要求1. 完善、编写程序,并调试程序,要给出测试数据和实验结果;2. 整理上机步骤,总结经验和体会;3. 完成实验报告和上交
2、程序。实验内容考察一个点、圆、圆柱体的层次结构,计算圆和圆柱体的面积,阅读程序并运行。主要仪器设备台式或笔记本电脑实验记录(写出实验程序代码及运行结果)#include#includeusing namespace std;class Point friend ostream &operator(ostream &,const Point &);public: Point(int=0,int=0); void setPoint(int,int); int getX() constreturn x; int getY() constreturn y; protected: int x,y;cla
3、ss Circle:public Point friend ostream &operator(ostream &,const Circle &); public: Circle(double r=0.0,int x=0,int y=0); void setRadius(double); double getRadius() const; double area() const; protected: double radius;class Cylinder:public Circle friend ostream &operator(ostream &,const Cylinder &);
4、public: Cylinder(double h=0.0,double r=0.0,int x=0,int y=0); void setHeight(double); double getHeight() const; double area() const; double volume() const; protected: double height;Point:Point(int a,int b) setPoint(a,b);void Point:setPoint(int a,int b) x=a; y=b;ostream &operator(ostream &output,const
5、 Point &p) outputp.x,p.y=0?r:0);double Circle:getRadius() const return radius;double Circle:area() const return 3.14159*radius*radius;ostream &operator(ostream &output,const Circle &c) outputCenterc.x,c.y;Radius=setiosflags(ios:fixed|ios:showpoint)setprecision(2)=0?h:0);double Cylinder:getHeight() c
6、onst return height;double Cylinder:area() const return 2*Circle:area()+2*3.14159*radius*height;double Cylinder:volume() const return Circle:area()*height;ostream &operator(ostream &output,const Cylinder &cy) outputCenter=cy.x,cy.y;Radius=setiosflags(ios:fixed|ios:showpoint)setprecision(2)cy.radius;H
7、eight=cy.heightendl; return output;int main() Point p(70,110); coutThe initial location of p ispendl; p.setPoint(9,9); coutThe new location of p ispendl; Circle c(2.4,35,42); coutThe initial location and radius of c arencnArea=c.area()n; c.setRadius(4.15); c.setPoint(3,3); coutnThe new location and
8、radius of c arencnArea=c.area()n; Cylinder cyl(5.6,2.6,10,20); coutnThe initial location,radius and height of cyl arencylArea=cyl.area()nVolume=cyl.volume()n; cyl.setHeight(9); cyl.setRadius(4.35); cyl.setPoint(3,3); coutnThe new location,radius and height of cyl arencylArea=cyl.area()nVolume=cyl.volume()n; system(pause); return 0;运行结果:实验中遇到的问题和解决办法1.不能熟练设定类、运用类,通过多练一些相关题目解决;2.继承与派生的知识掌握还不是很牢固,导致有些程序题目解答不准确,通过重新学习,理性分析解决;心得体会继承与派生这部分内容相对自己来说较难掌握,想要做到熟练掌握比较困难,需要时间和大量精力来学习,并要多做练习。并且这部分编写是应简洁,不该繁琐。
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2