C++ : 复数运算“<<”函数重载和“+”、“—”重载
来源:互联网 发布:数组的默认长度是多少 编辑:程序博客网 时间:2024/06/05 18:18
自己最近写了下C++里面的"<<"和"+","-","*","/"运算符重载
#include<iostream>using namespace std;class Complex{public:Complex(double r=0, double i=0):real(r), image(i){}Complex operator+(Complex &c);Complex operator-(Complex &c);Complex operator*(Complex &c);Complex operator/(Complex &c);friend ostream & operator<<(ostream &, Complex &);private:double real;double image;};ostream & operator<<(ostream &output, Complex &c){ output<<"(" <<c.real << "+" <<c.image <<"i)" << endl; return output;}Complex Complex::operator +(Complex &c){return Complex(real+c.real, image+c.image);}Complex Complex::operator -(Complex &c){return Complex(real-c.real, image-c.image);}Complex Complex::operator *(Complex &c){return Complex(real*c.real-image*c.image, real*c.image+image*c.real);}Complex Complex::operator /(Complex &c){return Complex(real/c.real, image/c.image);}int main(){Complex c1(3, 4), c2(5, 10), c3, c4 , c5, c6;c3=c1+c2;c4=c2-c1;c5=c1*c2;c6=c2/c1; cout << c3 << c4 << c5 << c6;return 0;}
- C++ : 复数运算“<<”函数重载和“+”、“—”重载
- 【C++】重载运算符和重载函数
- 复数类中的运算符重载—友元函数
- 函数重载实现复数运算的加减乘除
- 复数的运算符重载----成员函数
- [c++]复数的运算符重载
- 【C++】复数的运算符重载
- 复数运算符重载
- 复数运算符重载
- 《C++》12 C++ 重载运算符和重载函数
- 运算符重载——重载+和-对复数类对象进行加减运算
- 运算符重载—复数的计算
- C++:复数类构造函数、拷贝构造、运算符重载、析构函数
- 用成员运算符重载函数进行复数运算
- C++ 重载运算符和重载函数
- C++ 重载运算符和重载函数
- C++:重载函数运算符
- C++实现输入输出运算符重载、友元函数和成员函数实现复数类Complex
- linux基础 第二篇
- 630路将更新欧舒特空调
- wpf image绑定bitmap( Binding Image.Source from download memory)
- firefox的经典调试工具
- x$bh
- C++ : 复数运算“<<”函数重载和“+”、“—”重载
- 【转】Object-C的函数调用机制详解--消息
- [行业观察]跨平台之争
- Hibernate1
- 8种Nosql数据库系统对比
- 使用js清空html表格的简单方法
- <?=$data ?>PHP快捷输出变量到HTML
- WPF中的换行符
- linux驱动开发理解