You are given a class - Complex.
class Complex
{
public:
int a,b;
};
Operators are overloaded by means of operator functions, which are regular functions with special names. Their name begins with the operator keyword followed by the operator sign that is overloaded. The syntax is:
type operator sign (parameters) { /*... body ...*/ }
You need to overload operators +
and <<
for the Complex class.
The operator +
should add complex numbers according to the rules of complex addition:
(a+ib)+(c+id) = (a+c) + i(b+d)
Overload the stream insertion operator <<
to add "" to the stream:
cout<<c<<endl;
The above statement should print "" followed by a newline where and .
Input Format
The overloaded operator +
should receive two complex numbers ( and ) as parameters. It must return a single complex number.
The overloaded operator <<
should add "" to the stream where is the real part and is the imaginary part of the complex number which is then passed as a parameter to the overloaded operator.
Sample Input
3+i4
5+i6
Sample Output
8+i10
이 문제는 앞의 연산자 오버로딩 문제를 풀고 난 후여서, operator+ 에 대해서는 쉽게 풀 수 있었다. 출력에 대한 오버로딩은 std::ostream을 이용하는 약간의 응용 방식을 확인한 후 작성할 수 있었다.
허나, operator+ 에 대해 여러 가지 방식을 이용하여 풀 수 있다는 것을 알게 되었고, 3 가지 방식에 대해 정리를 해보았다.
'Language Proficiency > C++' 카테고리의 다른 글
Attribute Parser (0) | 2018.08.01 |
---|---|
Attending Workshops (0) | 2018.07.31 |
Operator Overloading (0) | 2018.07.31 |
Deque-STL (0) | 2018.07.31 |
Print Pretty (0) | 2018.07.31 |