实验3 多态性
实验课程名:面向对象程序设计(C++)
专业班级: 学号: 姓名: 实验时间: 实验地点: 指导教师:
3.1实验目的和要求 (1) 了解多态性的概念。 (2) 掌握运算符重载的基本方法。 (3) 掌握虚函数的定义和使用方法。 (4) 掌握纯虚函数和抽象类的概念和用法。
二、实验内容 一、构建一个复数类Complex,试对下列几个运算符进行重载:++,=,!=,+,-,==,其中要求要有成员重载形式和友元重载形式,而且,++运算符要求实现先加和后加两种形式。 该类定义原型说明: class complex { public: complex(double r=0,double i=0); complex &operator +(complex &c); complex operator -(complex &c); complex operator *(complex &c); friend complex operator /(complex &c1,complex &c2); friend int operator ==(complex &c1,complex &c2); friend int operator !=(complex &c1,complex &c2); //friend complex operator++(complex &c); complex operator++(); complex operator++(int); void disp(); private: double real; double imag; }; 实验代码如下: #include
imag=i; } complex operator +(complex &c); complex operator -(complex &c); complex operator *(complex &c); friend complex operator /(complex &c1,complex &c2); friend int operator ==(complex &c1,complex &c2); friend int operator !=(complex &c1,complex &c2); //friend complex operator++(complex &c); complex operator++(); complex operator++(int); void display(); double real; double imag; }; complex complex::operator +(complex &c) { complex c1; c1.real=real+c.real; c1.imag=imag+c.imag; return c1;