《[实验报告]继承和派生》由会员分享,可在线阅读,更多相关《[实验报告]继承和派生(5页珍藏版)》请在金锄头文库上搜索。
1、.大学实 验 报 告课程名称: c+程序设计 实验项目: 继承和派生 实 验 室: 实验台号: 班 级: 姓 名: 实验日期:2010 年 12 月 17 日 实验目的:1掌握利用单一继承和多重继承方式定义派生类的方法;2深刻理解在各种继承方式下构造函数和析构函数的执行次序;3理解和掌握公有继承、私有继承和保护继承对基类成员的访问机制;4理解虚基类的概念以及引入虚基类的目的和作用。实验仪器及器材: 计算机、Windows XP实验内容: 1、打开电脑:双击桌面 C+的快捷方式就启动了 C+程序。 2、在 c+程序窗口中输入程序,代码如下: #include #include using nam
2、espace std; #define PI 3.14159 class Point friend class Line; protected: double x, y ; public: Point()x = 0 ; y = 0 ; Point(double xv,double yv) x = xv; y = yv; double Area()return 0; void Show() coutPoint:operator=(cir); /在派生类中重载复制赋值操作符有固定 的标准格式 radius=cir.radius; return *this; double Area() return
3、 PI*radius*radius; void Show() /可以直接访问基类的数据成员 cout“x=“x “y=“y“ radius=“radiusendl; ; class Line Point start,end;/对象成员 public: Line():start(0,0),end(0,0) /注意对象成员初始化的方 法 Line(double xv1,double yv1,double xv2,double yv2) : start(xv1,yv1),end(xv2,yv2) double GetLength() return sqrt(start.x-end.x)*(start
4、.x-end.x)+(start.y- end.y)*(start.y-end.y); double Area()return 0; void Show() cout“start point:n“; start.Show(); cout“end point:n“; end.Show(); ;int main() Point pt(0,0); Circle cl1(100,100,10),cl2(cl1),cl3; Line ln1(0,0,100,100); cout“点面积:“pt.Area()endl; pt.Show(); cout“cl1 圆面积:“cl1.Area()endl; cl
5、1.Show(); cout“cl2 圆面积:“cl2.Area()endl; cl2.Show(); cl3=cl1; cout“cl3 圆面积:“cl3.Area()endl; cl3.Show(); cout“线面积:“ln1. Area()t“线长度:“ln1. GetLength() endl; ln1.Show(); return 0; 3、编译,检查是否有错误;4、运行结果5、单击右上角关闭按钮就 C+退出了程序实验数据记录:1、使用计算机的具体配置信息: CPU 型号:Intel 酷睿 i7 860 标称频率:2800MHz 网卡与声卡:集成 2 程序结果运行如下:结果讨论:通过实验1、理解了继承和派生的概念,加强了应用,在此基础上得以更好的掌握2、激发了我的学习兴趣。