运算符的重载
2013-04-22 22:58
155 查看
#include<iostream> using namespace std; class Complex { public: Complex() { real=0; imag=0; } Complex(double r,double i) { real=r; imag=i; } Complex complex_add(Complex &c2); void display(); private: double real; double imag; }; Complex Complex::complex_add(Complex &c2) { Complex c; c.real=real+c2.real; c.imag=imag+c2.imag; return c; } void Complex::display() { cout<<"("<<real<<","<<imag<<"i)"<<endl; } int main() { Complex c1(3,4),c2(5,-10),c3; c3=c1.complex_add(c2); cout<<"c1="; c1.display(); cout<<"c2="; c2.display(); cout<<"c1+c2="; c3.display(); return 0; }
运行结果:
相关文章推荐
- C#运算符的重载
- c++ 运算符的重载
- C++中那些运算符不能重载
- 详解C++中的函数调用和下标以及成员访问运算符的重载
- 重载算术运算符
- 设计一个string类,通过重载“+=”运算符,实现对两个string类对象m1,m2的操作。
- C++通过重载()运算符来仿造函数
- 自增、自减运算符的重载
- 第八周项目一(2)友元函数对运算符的重载
- 继承与派生、运算符的重载、虚函数的应用
- 8-1-(1)-友元函数的运算符的重载
- 函数调用运算符与重载、类型转换
- 第八周任务二——关于时间函数运算符的重载
- 第9周任务3(实现分数类中<<和>>运算符的重载)
- 第九周任务二(接第八周任务二--实现Complex类中的"<<"和">>"运算符的重载)
- 北大 C++ 4.6 自加自减运算符的重载
- C++日期类(运算符的重载)
- 不可以重载的运算符
- C++入门 (十) 重载双目运算符
- C++: 重载前缀运算符与重载后缀运算符(以++为例)