实验三继承与派生.docx
《实验三继承与派生.docx》由会员分享,可在线阅读,更多相关《实验三继承与派生.docx(7页珍藏版)》请在冰豆网上搜索。
实验三继承与派生
实验三-继承与派生
本科实验报告
课程名称:
C++面向对象程序设计
实验项目:
实验地点:
明向校区
专业班级:
软件1419学号:
2014006061
学生姓名:
刘国鑫
指导教师:
王丽娟
2015年5月10日
实验名称
实验三继承与派生
实验目的
1.理解继承与派生的概念;
2.掌握派生类定义格式与使用方法;
3.初步掌握派生类构造函数的定义与使用方法,理解构造函数的调用过程,及基类成员的初始化过程;
4.理解冲突、支配规则与赋值兼容性原则的概念;
实验要求
1.完善、编写程序,并调试程序,要给出测试数据和实验结果;
2.整理上机步骤,总结经验和体会;
3.完成实验报告和上交程序。
实验内容
考察一个点、圆、圆柱体的层次结构,计算圆和圆柱体的面积,阅读程序并运行。
主要仪器设备
台式或笔记本电脑
实验记录(写出实验程序代码及运行结果)
#include
#include
usingnamespacestd;
classPoint
{
friendostream&operator<<(ostream&,constPoint&);
public:
Point(int=0,int=0);
voidsetPoint(int,int);
intgetX()const{returnx;}
intgetY()const{returny;}
protected:
intx,y;
};
classCircle:
publicPoint
{
friendostream&operator<<(ostream&,constCircle&);
public:
Circle(doubler=0.0,intx=0,inty=0);
voidsetRadius(double);
doublegetRadius()const;
doublearea()const;
protected:
doubleradius;
};
classCylinder:
publicCircle
{
friendostream&operator<<(ostream&,constCylinder&);
public:
Cylinder(doubleh=0.0,doubler=0.0,intx=0,inty=0);
voidsetHeight(double);
doublegetHeight()const;
doublearea()const;
doublevolume()const;
protected:
doubleheight;
};
Point:
:
Point(inta,intb)
{
setPoint(a,b);
}
voidPoint:
:
setPoint(inta,intb)
{
x=a;
y=b;
}
ostream&operator<<(ostream&output,constPoint&p)
{
output<<'['<
returnoutput;
}
Circle:
:
Circle(doubler,inta,intb):
Point(a,b)
{
setRadius(r);
}
voidCircle:
:
setRadius(doubler)
{
radius=(r>=0?
r:
0);
}
doubleCircle:
:
getRadius()const
{
returnradius;
}
doubleCircle:
:
area()const
{
return3.14159*radius*radius;
}
ostream&operator<<(ostream&output,constCircle&c)
{
output<<"Center"<<'['<:
fixed|ios:
:
showpoint)<(2)<returnoutput;
}
Cylinder:
:
Cylinder(doubleh,doubler,intx,inty):
Circle(r,x,y)
{
setHeight(h);
}
voidCylinder:
:
setHeight(doubleh)
{
height=(h>=0?
h:
0);
}
doubleCylinder:
:
getHeight()const
{
returnheight;
}
doubleCylinder:
:
area()const
{
return2*Circle:
:
area()+2*3.14159*radius*height;
}
doubleCylinder:
:
volume()const
{
returnCircle:
:
area()*height;
}
ostream&operator<<(ostream&output,constCylinder&cy)
{
output<<"Center="<<'['<:
fixed|ios:
:
showpoint)<(2)<returnoutput;
}
intmain()
{
Pointp(70,110);
cout<<"Theinitiallocationofpis"<
p.setPoint(9,9);
cout<<"Thenewlocationofpis"<
Circlec(2.4,35,42);
cout<<"Theinitiallocationandradiusofcare\n"<c.setRadius(4.15);
c.setPoint(3,3);
cout<<"\nThenewlocationandradiusofcare\n"<Cylindercyl(5.6,2.6,10,20);
cout<<"\nTheinitiallocation,radiusandheightofcylare\n"<cyl.setHeight(9);
cyl.setRadius(4.35);
cyl.setPoint(3,3);
cout<<"\nThenewlocation,radiusandheightofcylare\n"<system("pause");
return0;
}
运行结果:
实验中遇到的问题和解决办法
1.不能熟练设定类、运用类,通过多练一些相关题目解决;
2.继承与派生的知识掌握还不是很牢固,导致有些程序题目解答不准确,通过重新学习,理性分析解决;
心得体会
继承与派生这部分内容相对自己来说较难掌握,想要做到熟练掌握比较困难,需要时间和大量精力来学习,并要多做练习。
并且这部分编写是应简洁,不该繁琐。