codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
#include<iostream> using namespace std; class Complex { public: Complex() { } Complex(double a, double b) { real = a, imag = b; } friend Complex operator +(int &i, Complex &c); friend Complex operator +(Complex &c1, Complex &c2); void input(); void display(); private: double real; double imag; }; Complex operator +(int &i, Complex &c) { return Complex(i + c.real, c.imag); } Complex operator +(Complex &c1, Complex &c2) { return Complex(c1.real + c2.real, c1.imag + c2.imag); } void Complex::input() { cout << "please input the complex "; cin >> real >> imag; } void Complex::display() { cout << "(" << real << "," << imag << ")"; } int main() { Complex c1, c2, c3, c4; int i; c1.input(); cout << "c1="; c1.display(); c2.input(); cout << "c2="; c2.display(); c3 = c1 + c2; cout << "please input a number "; cin >> i; c4 = i + c1; cout << "c1+c2="; c3.display(); cout << "c1+i="; c4.display(); return 0; }
Private
[
?
]
Run code
Submit