运算符的重载

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
class Complex
{
public:
Complex(double _real, double _image);    //构造函数
Complex(const Complex & p);           //拷贝构造函数
Complex operator+(const Complex &);      //加法运算符的重载
Complex operator-(const Complex &);      //减法运算符的重载
Complex operator*(const Complex &);      //乘法运算符的重载
Complex operator/(const Complex &);      //除法运算符的重载
void ShowNumber();                  //输出函数
~Complex();                       //析构函数
private:
double real;
double image;
};
Complex::Complex(double _real, double _image)    //构造函数
{
real = _real;
image = _image;
}
Complex::Complex(const Complex & p)      //拷贝构造函数
{
real = p.real;
image = p.image;
}
Complex::~Complex()         //析构函数
{
cout << "已调用析构函数" << endl;
}
Complex Complex::operator + (const Complex &op1)     //加法运算符的重载
{
return Complex(real + op1.real, image + op1.image);
}
Complex Complex::operator-(const Complex &op1)   //减法运算符的重载
{
return Complex(real - op1.real, image + op1.image);
}
Complex Complex::operator*(const Complex &op1)    //乘法运算符的重载
{
return Complex((real * op1.real) - (image * op1.image), (real * op1.image) + (op1.real * image));
}
void Complex::ShowNumber()      //输出函数
{
cout << real << "+" << image << "i" << endl;
}
int main()
{
system("color e");
Complex op1(1.0, 2.0), op2(3.0, 4.0), op3(5.0, 6.0);
Complex op4(op3);
op1.ShowNumber();
op2.ShowNumber();
op3.ShowNumber();
op4.ShowNumber();
op1 = op2 + op3;
op2 = op3 - op4;
op3 = op3*op4;
op1.ShowNumber();
op2.ShowNumber();
op3.ShowNumber();
system("pause");
return 0;
}


本文出自 “零点时光” 博客,请务必保留此出处http://10741764.blog.51cto.com/10731764/1743317

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值