运算符重载的实现代码
来源:互联网 发布:环太平洋配乐 知乎 编辑:程序博客网 时间:2024/06/10 11:56
# include <iostream>
using namespace std;
class Complex
{
friend ostream& operator<<(ostream& _cout, const Complex &c1);
public:
Complex(const double real, const double image)
:_real(real)
, _image(image)
{}
Complex(const Complex & complex)
{
_real = complex._real;
_image = complex._image;
}
Complex operator+(const Complex&c)
{
Complex C(0.0, 0.0);
C._real = _real + c._real;
C._image = _image + c._image;
return C;
}
Complex operator-(const Complex&c)
{
Complex C(0.0, 0.0);
C._real = _real - c._real;
C._image = _image - c._image;
return C;
}
Complex operator*(const Complex&c)
{
Complex C(0.0, 0.0);
C._real = _real * c._real;
C._image = _image * c._image;
return C;
}
Complex operator/(const Complex&c)
{
Complex C(0.0, 0.0);
C._real = _real / c._real;
C._image = _image / c._image;
return C;
}
Complex& operator+=(const Complex&c)
{
_real = _real + c._real;
_image = _image + c._image;
return *this ;
}
Complex& operator-=(const Complex&c)
{
_real = _real - c._real;
_image = _image - c._image;
return *this;
}
Complex& operator*=(const Complex&c)
{
_real = _real * c._real;
_image = _image * c._image;
return *this;
}
Complex& operator/=(const Complex&c)
{
_real = _real / c._real;
_image = _image / c._image;
return *this;
}
Complex operator&(const Complex &c)
{
Complex C(0.0, 0.0);
C._real=(int)_real & (int)(c._real);
C._image=(int)_image & (int)(c._image);
return C;
}
bool operator>(const Complex&c)
{
if(_real > c._real)
return 1;
else
return 0;
}
bool operator<(const Complex&c)
{
return _real < c._real ? 1 : 0;
}
bool operator==(const Complex&c)
{
return _real == c._real ? 1 : 0;
}
bool operator!=(const Complex&c)
{
return _real != c._real ? 1 : 0;
}
private:
double _real;
double _image;
};
ostream& operator<<(ostream& _cout, const Complex &c1)
{
_cout <<"("<<c1. _real <<","<< c1. _image<<")";
return _cout;
}
int main()
{
Complex c1(2.0, 3.0);
cout << c1 << endl;
Complex c2(c1);
cout << c2 << endl;
cout<<c1.operator+(c2)<<endl;
cout << c1.operator-(c2) << endl;
cout << c1.operator*(c2) << endl;
cout << c1.operator/(c2) << endl;
cout << c1.operator+=(c2) << endl;
cout << c1 << endl;
cout << c1.operator/=(c2) << endl;
cout << c1.operator-=(c2) << endl;
cout << c1.operator*(c2) << endl;
cout << c1<<c2 << endl;
cout << c1.operator&(c2) << endl;
cout << c1.operator>(c2) << endl;
cout << c1.operator<(c2) << endl;
cout << c1.operator==(c2) << endl;
cout << c1.operator!=(c2) << endl;
system("pause");
return 0;
}
using namespace std;
class Complex
{
friend ostream& operator<<(ostream& _cout, const Complex &c1);
public:
Complex(const double real, const double image)
:_real(real)
, _image(image)
{}
Complex(const Complex & complex)
{
_real = complex._real;
_image = complex._image;
}
Complex operator+(const Complex&c)
{
Complex C(0.0, 0.0);
C._real = _real + c._real;
C._image = _image + c._image;
return C;
}
Complex operator-(const Complex&c)
{
Complex C(0.0, 0.0);
C._real = _real - c._real;
C._image = _image - c._image;
return C;
}
Complex operator*(const Complex&c)
{
Complex C(0.0, 0.0);
C._real = _real * c._real;
C._image = _image * c._image;
return C;
}
Complex operator/(const Complex&c)
{
Complex C(0.0, 0.0);
C._real = _real / c._real;
C._image = _image / c._image;
return C;
}
Complex& operator+=(const Complex&c)
{
_real = _real + c._real;
_image = _image + c._image;
return *this ;
}
Complex& operator-=(const Complex&c)
{
_real = _real - c._real;
_image = _image - c._image;
return *this;
}
Complex& operator*=(const Complex&c)
{
_real = _real * c._real;
_image = _image * c._image;
return *this;
}
Complex& operator/=(const Complex&c)
{
_real = _real / c._real;
_image = _image / c._image;
return *this;
}
Complex operator&(const Complex &c)
{
Complex C(0.0, 0.0);
C._real=(int)_real & (int)(c._real);
C._image=(int)_image & (int)(c._image);
return C;
}
bool operator>(const Complex&c)
{
if(_real > c._real)
return 1;
else
return 0;
}
bool operator<(const Complex&c)
{
return _real < c._real ? 1 : 0;
}
bool operator==(const Complex&c)
{
return _real == c._real ? 1 : 0;
}
bool operator!=(const Complex&c)
{
return _real != c._real ? 1 : 0;
}
private:
double _real;
double _image;
};
ostream& operator<<(ostream& _cout, const Complex &c1)
{
_cout <<"("<<c1. _real <<","<< c1. _image<<")";
return _cout;
}
int main()
{
Complex c1(2.0, 3.0);
cout << c1 << endl;
Complex c2(c1);
cout << c2 << endl;
cout<<c1.operator+(c2)<<endl;
cout << c1.operator-(c2) << endl;
cout << c1.operator*(c2) << endl;
cout << c1.operator/(c2) << endl;
cout << c1.operator+=(c2) << endl;
cout << c1 << endl;
cout << c1.operator/=(c2) << endl;
cout << c1.operator-=(c2) << endl;
cout << c1.operator*(c2) << endl;
cout << c1<<c2 << endl;
cout << c1.operator&(c2) << endl;
cout << c1.operator>(c2) << endl;
cout << c1.operator<(c2) << endl;
cout << c1.operator==(c2) << endl;
cout << c1.operator!=(c2) << endl;
system("pause");
return 0;
}
0 0
- 运算符重载的实现代码
- 运算符+-*/的重载代码
- 运算符重载的分析及代码实现
- 十九、运算符重载(三)String类的改进实现、[]运算符重载、+运算符重载、+=运算符重载、<<运算符重载、>>运算符重载
- 高精度重载运算符的C++代码
- 矩阵类的实现,运算符重载
- C++实现的复数运算符重载
- 实现复数类的运算符重载
- 实现负数中的运算符的重载
- 实现Time类的运算符重载
- 实现复数类的运算符重载
- 实现TIME类的运算符重载
- 【c++】实现运算符的重载
- C++运算符重载的实现
- 重载运算符的实现与应用
- C++之运算符的重载实现大数的运算
- c++语言 运算符重载 使用重载运算符实现类的加法运算
- 运算符重载相关代码
- Symmetric Tree问题及解法
- mate标签
- 浅谈自定义View之自定义布局FlowLayout
- Java垃圾回收学习笔记
- JAVA POI导入技术之EXCEL模板中制作下拉列表
- 运算符重载的实现代码
- MySQL中的存储引擎讲解(InnoDB,MyISAM,Memory等各存储引擎对比)
- codeforces 785 E. Anton and Permutation(分块)
- 使用Python实现每日一句英语发送到手机
- eclispe使用maven创建web项目后更改java和web版本
- Loop指令
- React学习之列表运用(七)
- java并发包-ReentrantLock(三):如何解锁
- Tensorflow一些常用基本概念与函数4