[ create a new paste ] login | about

Link: http://codepad.org/xAwxjyPw    [ raw code | output | fork ]

C++, pasted on Jun 22:
#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;
}


Output:
1
please input the complex c1=(-8.91496e+303,4.89293e-270)please input the complex c2=(-1.13637e+267,-2.55184e+267)please input a number c1+c2=(-8.91496e+303,-2.55184e+267)c1+i=(-8.91496e+303,4.89293e-270)


Create a new paste based on this one


Comments: