Question about overloading operators

Hello,
I have a question about overloading operators, what is the point? I tried a program that uses it, and it works... but when I comment out the overloaded operator functions, it still works the same... any help would be appreciated. Here is the source code for reference:

Original:
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
// Operator Overloading
#include <iostream>
using namespace std;

class ThreeD{
	int x, y, z; //3-D coordinates
public:
	ThreeD(){x = y = z = 0;}
	ThreeD(int i, int j, int k){x=i; y=j; z=k;}

	ThreeD operator+(ThreeD op2); // op1 is implied
	ThreeD operator=(ThreeD op2); // op1 is implied

	void show();
};

// Overload +.
ThreeD ThreeD::operator+(ThreeD op2)
{
	ThreeD temp;

	temp.x = x + op2.x; // These are integer addition
	temp.y = y + op2.y; // and the + retains its original
	temp.z = z + op2.z; // meaning relative to them
	return temp;
}

// Overload assignment.

ThreeD ThreeD::operator=(ThreeD op2)
{
	x = op2.x; //these are integer assignments
	y = op2.y; // and the = retains its original
	z = op2.z; // meaning relative to them
	return *this;
}

// Show X, Y, Z coordinates.
void ThreeD::show()
{
	cout << x << ", ";
	cout << y << ", ";
	cout << z << '\n';
}

int main()
{
	ThreeD a(1, 2, 3), b(10, 10, 10), c;

	cout << "Original value of a: ";
	a.show();
	cout << "Original value of b: ";
	b.show();

	cout << '\n';

	c = b = a; // demonstrate multiple assignment
	cout << "Value of c after c = b = a: ";
	c.show();
	cout << "Value of b after c = b = a: ";
	b.show()
	;

	return 0;
}


And here is the commented version:
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
// Operator Overloading
#include <iostream>
using namespace std;

class ThreeD{
	int x, y, z; //3-D coordinates
public:
	ThreeD(){x = y = z = 0;}
	ThreeD(int i, int j, int k){x=i; y=j; z=k;}

	//ThreeD operator+(ThreeD op2); // op1 is implied
	//ThreeD operator=(ThreeD op2); // op1 is implied

	void show();
};

// Overload +.
//ThreeD ThreeD::operator+(ThreeD op2)
//{
//	ThreeD temp;
//
//	temp.x = x + op2.x; // These are integer addition
//	temp.y = y + op2.y; // and the + retains its original
//	temp.z = z + op2.z; // meaning relative to them
//	return temp;
//}

// Overload assignment.

//ThreeD ThreeD::operator=(ThreeD op2)
//{
//	x = op2.x; //these are integer assignments
//	y = op2.y; // and the = retains its original
//	z = op2.z; // meaning relative to them
//	return *this;
//}

// Show X, Y, Z coordinates.
void ThreeD::show()
{
	cout << x << ", ";
	cout << y << ", ";
	cout << z << '\n';
}

int main()
{
	ThreeD a(1, 2, 3), b(10, 10, 10), c;

	cout << "Original value of a: ";
	a.show();
	cout << "Original value of b: ";
	b.show();

	cout << '\n';

	c = b = a; // demonstrate multiple assignment
	cout << "Value of c after c = b = a: ";
	c.show();
	cout << "Value of b after c = b = a: ";
	b.show()
	;

	return 0;
}


Thanks for taking a look,
enduser000
Your test is invalid, because you aren't doing anything that the default copy constructor and assignment operator don't already do.

Try placing a

1
2
3
cout << "a + b + c = ";
ThreeD sum = a + b + c;
sum.show();


in the commented and uncommented versions and see what happens.

The point of overloading operators is just to create a higher-level abstraction so that the programmer can better see what he is doing. So instead of writing

sum.assign( a.add( b.add( c ) ) );

he can just say:

sum = a + b + c;

...which is easier to read.

Hope this helps.
Alright, I missed this part before, the code is now:
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
// Operator Overloading
#include <iostream>
using namespace std;

class ThreeD{
	int x, y, z; //3-D coordinates
public:
	ThreeD(){x = y = z = 0;}
	ThreeD(int i, int j, int k){x=i; y=j; z=k;}

	ThreeD operator+(ThreeD op2); // op1 is implied
	//ThreeD operator=(ThreeD op2); // op1 is implied

	void show();									 
};

//Overload +.
ThreeD ThreeD::operator+(ThreeD op2)
{
	ThreeD temp;

	temp.x = x + op2.x; // These are integer addition
	temp.y = y + op2.y; // and the + retains its original
	temp.z = z + op2.z; // meaning relative to them
	return temp;
}

// Overload assignment.

//ThreeD ThreeD::operator=(ThreeD op2)
//{
//	x = op2.x; //these are integer assignments
//	y = op2.y; // and the = retains its original
//	z = op2.z; // meaning relative to them
//	return *this;
//}

// Show X, Y, Z coordinates.
void ThreeD::show()
{
	cout << x << ", ";
	cout << y << ", ";
	cout << z << '\n';
}

int main()
{
	ThreeD a(1, 2, 3), b(10, 10, 10), c;

	cout << "Original value of a: ";
	a.show();
	cout << "Original value of b: ";
	b.show();

	cout << '\n';

	c = a + b + c; // add a, b, and c together
	cout << "Value of c after c = a + b + c: ";
	c.show();

	cout << '\n';

	c = b = a; // demonstrate multiple assignment
	cout << "Value of c after c = b = a: ";
	c.show();
	cout << "Value of b after c = b = a: ";
	b.show()
	;

	return 0;
}


And it didn't like when I removed the overloaded + operator so I put that back in... = still dosen't seem nessecary to overload...
Last edited on
Perhaps you should spend some time over at the C++FAQ-Lite.
http://www.parashift.com/c++-faq-lite/operator-overloading.html

Good luck!
I think I'm starting to get it, if you need to reuse your classes, it gets easier with operator overloading. Same thing if others need to use your classes, I have another question: Where are plain x, y and z coming from in this block, when op2 is an object passed to the function and temp is created by the function?
1
2
3
4
5
6
7
8
9
ThreeD ThreeD::operator+(ThreeD op2)
{
	ThreeD temp;

	temp.x = x + op2.x; // These are integer addition
	temp.y = y + op2.y; // and the + retains its original
	temp.z = z + op2.z; // meaning relative to them
	return temp;
}


would it b the same to put...?
1
2
3
4
5
6
7
8
9
ThreeD ThreeD::operator+(ThreeD op2)
{
	ThreeD temp;

	temp.x += op2.x; // These are integer addition
	temp.y += op2.y; // and the + retains its original
	temp.z += op2.z; // meaning relative to them
	return temp;
}


enduser000
Last edited on
The plain x, y, and z are implied to be this->x, etc. and would not be the same as temp.x += op2.x.

The reason is so that you add the current classes values to the other classes values and return them in the resulting class. With the bottom method, you create a new class with 0 is all the variables, then add the second classes values and effectively discard the first classes variables.
One final thing to clarify: so if you ise a member overloaded operator, the 'this' pointer is implicitly passed to it, and the argument you put in there (for binary operations) is the value on the right..?

For example:
1
2
3
4
5
6
7
8
9
ThreeD ThreeD::operator+(ThreeD op2)
{
	ThreeD temp;

	temp.x = x + op2.x;
	temp.y = y + op2.y;
	temp.z = z + op2.z;
	return temp;
}


When used in main()
1
2
3
4
5
6
int main()
{
//...
	Object obj1, obj2, val;

	val = obj1 + obj2;


ob1s' 'this' pointer is passed to operator+(), correct? and val is retuned the temp value, and ob2 is actually passed to operator+();
?
enduser000
Yep. :-)
Topic archived. No new replies allowed.