- 1、本文档共7页,可阅读全部内容。
- 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
实验7 多态性
专业:计算机科学与技术 班级: 12嵌入 学号:3121101232 姓名: 林珍妮 实验地点: 工A208 实验时间: 2013-11-27 指导教师:李佐勇
一、实验目的
1.掌握用函数成员实现运算符重载的方法;
2.掌握用友元函数实现运算符重载的方法;
3.掌握纯虚函数的概念及应用。
二、实验环境
一台PC机,Windows XP操作系统,Visual C++ 6.0开发环境。
三、实验内容
1、三维坐标类对象之间的直接运算。
三维坐标类有数据成员x、y、z,对象间运算时要求通过函数成员实现“+”、前置“--”、“= =”运算符的重载,通过友元函数实现后置“--”、“+=”、“”和“”运算符的重载,实现三维坐标类对象间的直接运算。main()完成对象的定义和有关运算符重载函数的测试。
2、由二维坐标点类(含有带默认形参值的构造函数、getX、getY、show函数)作为基类派生出圆类(含有带默认形参值的构造函数、getR、getarea、show函数);再由圆类作为基类派生出圆柱体类(含有带默认形参值的构造函数、getH、getV、show函数);考虑将show作为虚函数。
3、将实验内容2改为通过纯虚函数show完成数据成员的输出。
四、实验记录
1、#includeiostream
using namespace std;
class Point
{
public:
Point(int xx=0,int yy=0,int zz=0):x(xx),y(yy),z(zz){}
Point operator+(const Point p)const
{
return Point(x+p.x,y+p.y,z+p.z);
}
Point operator--()
{
--x;
--y;
--z;
return *this;
}
bool operator==(const Point p1)
{
if(x==p1.xy==p1.yz==p1.z)
return true;
return false;
}
friend Point operator--(Point p,int)
{
return Point(--p.x,--p.y,--p.z);
}
friend void operator+=( Point p1, Point p2)
{
p1.x+=p2.x;
p1.y+=p2.y;
p1.z+=p2.z;
}
friend ostream operator(ostream out,const Point p)
{
out(p.x,p.y,p.z);
return out;
}
friend istream operator(istream in,const Point p)
{
inp.xp.yp.z;
return in;
}
private:
int x,y,z;
};
int main()
{
Point p1(5,3,2);
Point p2(1,5,4);
Point p3;
coutp1: p1endl;
coutp2: p2endl;
p3=p1+p2;
coutp3=p1+p2: p3endl;
--p1;
cout--p1: p1endl;
if(p1==p2)
coutYESendl;
else
coutNOendl;
p1+=p2;
coutp1+=p2: p1endl;
return 0;
}
运行结果:
2、#includeiostream
#define PI 3.1415
using namespace std;
class Point
{
public:
Point(int xx=0,int yy=0):x(xx),y(yy){}
int GetX(){return x;}
int GetY(){return y;}
virtual void show()
{
cout(x,y)endl;
}
private:
int x,y;
};
class Circle:public Point
{
public:
Circle(int xx,int yy,int r):Point(xx,yy),radius(r){}
int GetR() {return radius;}
float Getarea()
{
float area;
area=static_castfloat(2*PI*ra
文档评论(0)