12378 - Tsai DS 20190919 Operator Overloading   

Description

Please consider the following c++ code. Implement a class Complex, which represents the Complex Number data type. 

#include<iostream>
using namespace std;
 
class Complex{
    private:
        int re;
        int im;
    public:
        Complex():re(0),im(0){}
        Complex(int re,int im):re(re),im(im){}
        int getReal(){
            return this->re;
        }
        int getImagine(){
            return this->im;
        }
        void setReal(int re){
            this->re=re;
        }
        void setImagine(int im){
            this->im=im;
        }
        Complex operator+(Complex c){
            // TODO (40%)
        }
};
 
ostream& operator<<(ostream& os, Complex& c){ 
       // Hint : "os" is used to output something, for example : os<<something       
       // TODO (30%)
       return os;
}
 
istream& operator>>(istream& is,Complex& c){
        // Hint : "is" is used to read something, for example : is>>something
        // TODO (30%)
        return is;
}
 
int main(){
 
    Complex c1, c2;

    cin>>c1;
    cin>>c2;

    Complex c3 = c1+c2;
    cout<<c3;
    return 0;

}

You should use operator>> to set the value of complex numbers first, and read out the complex numbers by operator<<. In the end, print out the addition result.

Input

Each test case's input consists of 4 numbers which represent two complex numbers.

Example :  

3 5 

4 -1  

means two complex numbers 3+5i and 4-1i

  

Output

The output is the addition result of the input

 

Sample Input  Download

Sample Output  Download

Tags

12378



Discuss