实验八 运算符重载
学号 20181101047 姓名 范少帅 班级 18 计科 3 班
一、实验目的
理解运算符重载的概念,掌握常用运算符重载。
二、实验内容
1、定义复数类,假定 c1,c2 代表复数对象,实现如下功能:
(1)两个复数相加 c1+c2
(2)复数和实数相加 c1+5.0 5.0+c1
(3)复数对象自增(规则:实部、虚部均自增) c1++ ++c1
(4)重载“<<”、”>>” 运算符实现复数对象输入输出。
2、定义一个数组类 Array(封装数组及对数组的操作,数组元素空间动态分配),
实现如下功能:
(1)数组复制
(2)数组赋值
(3)重载”[]”访问数组元素
(4)重载“<<”、”>>” 运算符实现数组对象输入输出。
三、实验步骤 (记录)
1、
#include <iostream>
using namespace std;
class complex
{
public:
double real,image;
public:
complex(double a=0,double b=0)
{
real=a;image=b;
}
friend complex add(complex &x,complex &y);
friend complex operator+(complex &c1,double &b);
complex operator++();
complex operator++(int);
friend istream & operator>>(istream & in, complex & A);
friend ostream & operator<<(ostream & out, complex & A);
void Show()
{
评论0