面向对象程序设计(++)-实验7-任务书.docVIP

  • 35
  • 0
  • 约4.08千字
  • 约 7页
  • 2016-06-30 发布于贵州
  • 举报
面向对象程序设计()-实验7-任务书

实验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*radius); r

文档评论(0)

1亿VIP精品文档

相关文档