C++ : 复数运算“<<”函数重载和“+”、“—”重载
2013-05-15 22:53
465 查看
自己最近写了下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++ 重载赋值运算函数 operator =
- 【C++】重载运算符号时的函数返回值类型问题
- C++复数运算符重载(+与<<)
- C++中输入输出<< 和>>重载,以便适应输出输入一个对象
- [C++基础]队列<queue>中的常用函数
- 《C++第九周实验报告1-1》--------接第8周任务1,定义Complex类中的<<和>>运算符的重载,实现输入和输出
- Java笔记1 Java编程基础<2>循环语句、函数(重载)、变量和数组的内存结构
- C++的流插入运算符“<<”和流提取运算符“>>”的重载
- C++集合运算函数总结 & 需要有序集合的操作
- <C++学习笔记>函数模板 template
- C++复数运算 重载
- C++复数运算符重载(+与<<)
- 以复数运算为实例的类实现、函数重载、模板类操作
- 重载插入运算表福“<<”
- C++语言笔试题目 C++中为什么用模板类&& 类中如何使用const &&函数重载,我们靠什么来区分调用的那个函数?靠返回值判断可以不可以
- C++输出运算重载为成员函数
- 读书笔记-Thinking in C++-第7章 函数重载和默认参数Function Overloading &Default Arguments
- <<c++ primer>> 函数指针的笔记
- C++学习笔记-----在重载的赋值运算函数中调用拷贝构造函数