-
#include <iostream.h>
-
class Complex {
-
double re, im;
-
public:
-
Complex(double r=0, double i=0); // ① Complex객체 초기화
-
~Complex(){};
-
Complex operator+ (Complex); // ② 복소수의 합
-
friend Complex operator* (double, Complex); // ③ 실수와 복소수의 곱
-
Complex operator++(int); // ④ 실수부 1증가
-
Complex operator++();
-
bool operator<(Complex); // ⑤ 실수부 크기비교
-
Complex* address(); // ⑥ 객체의 주소변환
-
friend ostream &operator<<(ostream &os, const Complex &comObj); //화면출력을 위한 <<연산자 구현
-
};
-
-
int main()
-
{
-
double num = 3.14159;
-
-
cout << "\n # 복소수의 객체 생성 \n\n";
-
Complex a(1,1);
-
Complex b(2);
-
Complex c;
-
Complex d;
-
Complex e;
-
cout << " a = " << a << endl;
-
cout << " b = " << b << endl;
-
cout << " c = " << c << endl;
-
cout << " d = " << d << endl;
-
cout << " e = " << e << endl;
-
-
cout << "\n\n # 복소수의 합 \n\n";
-
c = a + b;
-
cout << " c = a + b"<<endl;
-
cout << " => "<< c << " = " << a << " + " << b <<endl;
-
-
cout << "\n\n # 실수와 복소수의 곱 \n\n";
-
d = num * a;
-
cout << " d = "<<num<<" * a "<<endl;
-
cout << " => "<< d << " = " << a << " * " << num <<endl;
-
-
cout << "\n\n # 복소수 1증가 (증감연산자) \n\n";
-
cout << " b = " << b << endl;
-
cout << " 선행 증감연산 \n";
-
cout << " e = ++b => e = " << ++b << " , b = " << b << endl;
-
cout << " 후행 증감연산 \n";
-
cout << " e = b++ => e = " << b++ << " , b = " << b << endl;
-
-
cout << "\n\n # 실수부의 크기비교 \n\n";
-
cout << " a < b => ";
-
if(a<b)
-
cout<<"True ";
-
else
-
cout<<"False ";
-
cout << a << " < " << b << endl;
-
-
-
cout << "\n\n # 객체의 주소를 반환 \n\n";
-
cout << " a의 주소 = " << a.address()<<endl;
-
cout << " b의 주소 = " << b.address()<<endl;
-
cout << " c의 주소 = " << c.address()<<endl;
-
cout << " d의 주소 = " << d.address()<<endl;
-
cout << " e의 주소 = " << e.address()<<endl;
-
-
return 0;
-
}
-
-
// ① Complex객체 초기화
-
Complex::Complex(double r, double i)
-
{
-
re=r, im=i;
-
}
-
-
// ② 복소수의 합
-
Complex Complex::operator+ (Complex data)
-
{
-
Complex res;
-
res.re = this->re+data.re;
-
res.im = this->im+data.im;
-
return res;
-
}
-
-
// ③ 실수와 복소수의 곱
-
Complex operator* (double n, Complex data)
-
{
-
Complex res;
-
res.re = n * data.re;
-
res.im = n * data.im;
-
return res;
-
}
-
-
// ④ 실수부 ++증가 (후행증가)
-
Complex Complex::operator++(int)
-
{
-
Complex res=*this;
-
(this->im)++;
-
(this->re)++;
-
return res;
-
}
-
-
// 실수부 ++ 증가 (선행증가)
-
Complex Complex::operator++()
-
{
-
(this->im)++;
-
(this->re)++;
-
return *this;
-
}
-
-
// ⑤ 실수부 크기비교
-
bool Complex::operator<(Complex data)
-
{
-
return (this->re < data.re);
-
}
-
-
// ⑥ 객체의 주소변환
-
Complex* Complex::address()
-
{
-
return this;
-
}
-
-
//화면출력을 위한 <<연산자 구현
-
ostream &operator<<(ostream &os, const Complex &comObj)
-
{
-
os<< "( " << comObj.re << " + " << comObj.im << "i )";
-
return os;
-
}