实验报告3继承与多态..docVIP

  • 49
  • 0
  • 约 11页
  • 2017-01-12 发布于重庆
  • 举报
实验报告3继承与多态.

计 算 机 课 程 实 验 报 告 2012—2013学年度 第一 学期 系别:数学与计算机科学学院 实验课程 C++面向对象程序设计 班 级 级计算机科学与技术 学 号 11 姓 名 蔡兴明 指导教师 马学梅 实验题目 继承与多态(一) 日 期 2012-9-29 实验目的 及要求 1、理解继承在面向对象程序中的重要作用、继承和派生的概念; 2、掌握#include iostream.h class COMPLEX { public: COMPLEX (double r=0,double i=0); COMPLEX(const COMPLEX other); void print( ); COMPLEX add(const COMPLEX other); COMPLEX subs(const COMPLEX other); protected: double real, image; }; COMPLEX:: COMPLEX ( double r, double i) { real = r; image =i; return; } COMPLEX:: COMPLEX( const COMPLEX other ) { real = other.real; image =other.image; return;} void COMPLEX:: print( ) { cout real; if (image0) cout “+”image“i”; else if (image 0) cout image “i”; cout “\n”; return; } COMPLEX COMPLEX ::add (const COMPLEX other ) { real = real+other.real; image =image+other.image; return *this; } COMPLEX COMPLEX ::subs(const COMPLEX other ) { real = real-other.real; image =image-other.image; return *this; } int main ( ) { COMPLEX c1(1,2) ; COMPLEX c2(2) ; COMPLEX c3(c1) ; c3.print ( ); c2.add(c1); c3. subs(c2); c3.print( ); return 0; } 给出输出结果,分析this的用途.在此程序的基础上实现运算符”+”和”-“的重载 this是自身的地址,但是*this就是自身了.是按值回返了.如果函数的回返值是带有号的,那么回返值就是引用了一种情况就是,在类的非静态成员函数中返回类对象本身的时候,直接使用 return *this;另外一种情况是当参数与成员变量名相同时,如this-n = n (不能写成n = n)。 ++的前置和后置重载,但实际上并没有实现。试分析原因,并作出修改,以实现此功能。 #include iostream.h class OperClass { int x; public: OperClass( ); OperClass operator ++( ); OperClass operator ++(int ); void display( ); }; OperClass:: OperClass( ) { x=0; return;} OperClass OperClass :: operator++( ) {OperClass A; ++x; A.x=x; return A;} OperClass OperClass :: operator++(int ) {OperClass A; x ++;

文档评论(0)

1亿VIP精品文档

相关文档