C++实验指导 实验10 运算符重载.pdfVIP

  • 29
  • 0
  • 约1.65万字
  • 约 17页
  • 2017-06-06 发布于河南
  • 举报
C实验指导实验10运算符重载

实验十、运算符重载 (2 学时) 1.实习目的及要求: 1)掌握运算符重载的基本概念和方法; 2 )熟习几种特殊的运算符的重载 2 .预习: 预习运算符重载的概念、运算符重载的一般方法,包括成员函数重载和友元函数重载; 以及几种特殊的运算符,包括++和- - 运算符、赋值运算符、数组下标运算符、函数调用 运算符以及new 和delete 的重载等相关知识。 3 .实验内容: 3 .1 分析下面的程序,指出程序运行的结果: 请各位同学用你的学号命名你的作业文件夹,将各题的答案放入(用word 文 挡或写字板录入均可,要求为每一题建立一个文档),并发到教师机相应目录下(通 常是教师机的最后一个硬盘)的作业文件夹中。具体位置按老师的指示操作。 1) 分析下面的程序,指出程序运行的结果: #includeiostream.h class Point { int x,y; public: Point(int vx,int vy){x=vx;y=vy;} Point(){x=0,y=0;} Point operator + (Point p1) { Point p; int px=x+p1.x; int py=y+p1.y; return Point(px,py); } Point operator -(Point p1) { Point p; int px=x-p1.x; 1 int py=y-p1.y; return Point(px,py); } void print() {coutx,yendl;} }; void main (void) {Point p1(10,10),p2(20,20); p1=p1+p2; p1.print(); //p2.print(); //p3.print(); } 答案: /* 30,30 Press any key to continue */ 2) 分析下面的程序,指出程序运行的结果: #includeiostream.h static int dys[ ]={31,28,31,30,31,30,31,31,30,31,30,31}; class date { int mo,da,yr; public: date (int m , int d, int y){mo=m;da=d ;yr=y;} date(){} void disp() {coutmo/da/yrendl;} date operator + (int day) {date dt=*this; day+=dt.da;//coutdayendl; while(daydys[dt.mo-1]) {day-=dys[dt.mo-1]; //coutdayendl; if(++dt.mo==13) {dt.mo=1; dt.yr++; } 2 } //coutdayendl; //dt.disp(); dt.da=day; return dt; }

文档评论(0)

1亿VIP精品文档

相关文档