How to overload operators in existing code

Hello! I need to re-write this program to overload the following operators: ">>, <<, +, -, *, ==, !=, ++ (for square)" ...I have read about the basics of operator overloading, but I haven't been able to make even my sum (+) operator overload properly. If someone could give me an example of how to merely change my sum (+) method to overload the operator, that would be plenty to get me started. Thanks so much, and you guys are the best!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include<iostream>
#include<iomanip>
using namespace std;

class ComplexNum
{
public:
    ComplexNum(float = 0.0, float = 0.0); //default constructor that uses default arg. in case no init. are in main
    void getComplexNum(); //get real and imaginary numbers from keyboard
    void sum(ComplexNum a, ComplexNum b); //method to add two ComplexNum numbers together
    void diff(ComplexNum a, ComplexNum b); //method to find the difference of two complex numbers
	void prod(ComplexNum a, ComplexNum b); //method to find the product of two complex numbers
	void squarea(ComplexNum a); //method to change each complex number to its square
	void squareb(ComplexNum b); //method to change each complex number to its square
	void printComplexNum(); //print sum, diff, prod, square 
	void formComplexNum(); //and "a+bi" form


private: 
    float real; //float data member for real number (to be entered in by user)
    float imaginary; //float data member for imaginary number (to be entered in by user)
};

ComplexNum::ComplexNum(float a, float b)
{   
    real = a;
    imaginary = b;
}

void ComplexNum::getComplexNum()
{
    cout << "Enter real part of complex number: ";
    cin >> real;
	
    cout << "Enter imaginary part of complex number: ";
    cin >> imaginary;
}

void ComplexNum::sum(ComplexNum a, ComplexNum b)
{
    this->real = a.real + b.real;
    this->imaginary = a.imaginary + b.imaginary;
	cout << "The addition of the two Complex Numbers is: ";
}

void ComplexNum::diff(ComplexNum a, ComplexNum b)
{
	this->real = a.real - b.real;
	this->imaginary = a.imaginary - b.imaginary;
	cout << "The difference of the two Complex Numbers is: ";
}

void ComplexNum::prod(ComplexNum a, ComplexNum b)
{
	this->real = (a.real * b.real) - (a.imaginary * b.imaginary);
	this->imaginary = (a.real * b.imaginary) + (b.real * a.imaginary);
	cout << "The product of the two Complex Numbers is: ";
}

void ComplexNum::squarea(ComplexNum a)
{
	this->real = (a.real * a.real) - (a.imaginary * a.imaginary);
	this->imaginary = (2 * (a.real * a.imaginary));
	cout << "The square of Complex Number 1 is: ";
}

void ComplexNum::squareb(ComplexNum b)
{
	this->real = (b.real * b.real) - (b.imaginary * b.imaginary);
	this->imaginary = (2 * (b.real * b.imaginary));
	cout << "The square of Complex Number 2 is: ";
}

void ComplexNum::printComplexNum()
{
	cout << "(" << real << "(+)" << imaginary << "i" << ")" << endl;
}

void ComplexNum::formComplexNum()
{
	cout << "Form '(a+bi)': " << "(" <<real << "(+)" << imaginary << "i" << ")" << endl;
}


int main()
{
    ComplexNum a, b, c, d, e, f, g;
    cout << "First Complex Number:" << endl;
	a.getComplexNum();
	a.formComplexNum();
	cout << endl;

	cout << "Second Complex Number:" << endl;
	b.getComplexNum();
	b.formComplexNum();
	cout << endl;
	
	c.sum(a, b);  
	c.printComplexNum();
	
	d.diff(a, b);
	d.printComplexNum();
	
	e.prod(a, b);
	e.printComplexNum();
	
	f.squarea(a);
	f.printComplexNum();
	
	g.squareb(b);
	g.printComplexNum();
	cout << endl;

    return 0;
}
Last edited on
Here's what I've tried, and has failed on me. And yes, unfortunately, I need to refactor the existing code. I currently get redlines under the second "operator" saying "declaration is incompatible" (with the "operator" in public)...here are the snippets I've tried ---

(what I added inside inside public to start overloading):

ComplexNum operator+(ComplexNum)

(and my attempt at refactoring sum(+)):

1
2
3
4
5
6
7
8
9
ComplexNum ComplexNum::operator+(ComplexNum a, ComplexNum b)
{
    ComplexNum result;
    result.real = a.real + b.real;
    result.imaginary = a.imaginary + b.imaginary;
    cout << "The addition of the two Complex Numbers is: ";

    return result;
}


Hope this helps clarify what I've done to try to re-write my code...
Last edited on
Your sum/div/prod/etc methods should not be producing output. They should be doing the operation required.

In methods which are non-static members of class the method receives a hidden (this) argument as it's first parameter. So, Complex::sum(ComplexNum a, ComplexNum b) involves three objects. a, b and the object referred to by this.

Likewise with operator+. But only two objects can be input for an expression involving a single +. In a+b there are only two objects, so your overloaded operator should only use 2 objects. One of them will be an explicit parameter in the overloaded operator function and one of them will be the implicitly provided this.

1
2
3
4
5
6
ComplexNum ComplexNum::operator+(ComplexNum other) const
{
    other.real += real;  // Short for:  other.real += this->real
    other.imaginary += imaginary;
    return other;
}
Last edited on
Binary operators are typically implemented as non-members to maintain symmetry (for example, when adding a complex number and an integer, if operator+ is a member function of the complex type, then only complex+integer would compile, and not integer+complex). Since for every binary arithmetic operator there exists a corresponding compound assignment operator, canonical forms of binary operators are implemented in terms of their compound assignments
http://en.cppreference.com/w/cpp/language/operators


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include<iostream>
#include<iomanip>

class ComplexNum
{
    public:
        ComplexNum(double = 0.0, double = 0.0); 

        // *** modified to return the result 
        ComplexNum& sum(ComplexNum a, ComplexNum b); //method to add two ComplexNum numbers together
        ComplexNum& diff(ComplexNum a, ComplexNum b); //method to find the difference of two complex numbers
        ComplexNum& prod(ComplexNum a, ComplexNum b); //method to find the product of two complex numbers

          // ...

        ////////////// overloaded operators (members)  //////////////
        ComplexNum& operator= ( const ComplexNum& that ) = default ;
        ComplexNum& operator += ( const ComplexNum& that ) { return sum( *this, that ) ;}
        ComplexNum& operator -= ( const ComplexNum& that ) { return diff( *this, that ) ; }
        ComplexNum& operator *= ( const ComplexNum& that ) { return prod( *this, that ) ; }
        // ...

        std::ostream& print( std::ostream& stm = std::cout ) const ; // const-correct


    private:
        double real; // double is the default floating point type
        double imaginary; 

    ////////////// overloaded operators (non-members)  //////////////
    // canonical forms of binary operators are implemented
    // in terms of their compound assignments:
    // see: http://en.cppreference.com/w/cpp/language/operators
    ///////////////////////////////////////////////////////////

    // note: a is passed by value
    friend ComplexNum operator+ ( ComplexNum a, const ComplexNum& b ) { return a += b ; }
    friend ComplexNum operator- ( ComplexNum a, const ComplexNum& b ) { return a -= b ; }
    friend ComplexNum operator* ( ComplexNum a, const ComplexNum& b ) { return a *= b ; }

    friend std::ostream& operator<< ( std::ostream& stm, const ComplexNum& c ) { return c.print(stm) ; }
};

ComplexNum::ComplexNum(double a, double b)
{
    real = a;
    imaginary = b;
}

ComplexNum& ComplexNum::sum(ComplexNum a, ComplexNum b)
{
    this->real = a.real + b.real;
    this->imaginary = a.imaginary + b.imaginary;
	return *this ;
}

ComplexNum& ComplexNum::diff(ComplexNum a, ComplexNum b)
{
	this->real = a.real - b.real;
	this->imaginary = a.imaginary - b.imaginary;
	return *this ;
}

ComplexNum& ComplexNum::prod(ComplexNum a, ComplexNum b)
{
	this->real = (a.real * b.real) - (a.imaginary * b.imaginary);
	this->imaginary = (a.real * b.imaginary) + (b.real * a.imaginary);
	return *this ;
}


std::ostream& ComplexNum::print( std::ostream& stm ) const
{ return stm << "(" << std::noshowpos << real << std::showpos << imaginary << "i)" ; }

int main()
{
    ComplexNum a( 2.0, 3.1 ), b( 4.5, -1.3 );
    std::cout << std::fixed << std::setprecision(2)
              << "a == " << a << '\n'
              << "b == " << b << '\n'
              << "a+b == " << a+b << '\n'
              << "a-b == " << a-b << '\n'
              << "a*b == " << a*b << '\n'
              
              // note: there is a converting constructor for double => ComplexNum
              // see: http://en.cppreference.com/w/cpp/language/converting_constructor
              << "a + 2.1 == " << a + 2.1 << '\n'
              << "7.8 - b == " << 7.8 - b << '\n'
              << "a*4 == " << a*4 << '\n' ;
}

http://coliru.stacked-crooked.com/a/20d78eee2c0de960
Thank you so much for the detailed reply! I am getting an error at =default in the public, it says that it's a "badly formed pure specifier" ...not sure what that means. I tried setting it to zero, but that also caused errors as well. I believe it may have an issue with the IDE that I'm using (VS 2010) but I could be wrong. Haven' been able to find a solution, though I can get it to run in C++ shell, so there is definitely a problem with the IDE. Thanks so much again!
Last edited on
I got it to work, just needed to return a value in my getComplexNum method. It works excellently now. I learned a thing or two about friends as well. Thanks so much there.
> I'm using (VS 2010)

Upgrade to a more current version of the compiler/library

For instance: https://www.visualstudio.com/vs/visual-studio-2017-rc/
(For the installation, choose 'Windows desktop development with C++' )
Topic archived. No new replies allowed.