Computing.Net > Forums > Programming > C++ Overloading Operators

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

C++ Overloading Operators

Reply to Message Icon

Name: gimmpy224
Date: August 28, 2004 at 12:17:31 Pacific
OS: windows XP pro
CPU/Ram: athlon XP 512 mb
Comment:

Im reading the book "Mastering Visual C++ 6" and it just had a section on overloading operators that made NO sense at all.
I was hoping that someone (Don, Blue, IR) could once again pull me out of the dark.
The book totaly confused me, perhaps the code is wrong in it.
Also i dont understand why you would have the need to overload an operator.

Thanks guys :)

GIMPS



Sponsored Link
Ads by Google

Response Number 1
Name: egkenny
Date: August 28, 2004 at 16:22:32 Pacific
Reply:

Operators have predefined meanings for normal data types such as integers and floating point numbers. When you overload an operator you define what the operator is to do for a class.

Suppose we have the following class:
class cPos
{
public:
int getx() {return xpos;}
int gety() {return ypos;}
void setx(int xp) {xpos = xp;}
void sety(int yp) {ypos = yp;}
private:
int xpos;
int ypos;
};

Suppose you define three instances of the class: cPos a, b, c;
The computer would not know what to do with this statement: c = a + b;

You overload the + operator to handle adding two instances of the class cPos.

The computer would not know how to handle this either:
cout << c << endl;

You overload the << operator to handle using C++ iostreams to ouput the class cPos.

Here is the complete example:

#include <iostream.h>
class cPos
{
public:
int getx() {return xpos;}
int gety() {return ypos;}
void setx(int xp) {xpos = xp;}
void sety(int yp) {ypos = yp;}
cPos operator+(cPos & p);
friend ostream& operator << (ostream& out, cPos & pos);
private:
int xpos;
int ypos;
};

cPos cPos::operator+(cPos & p)
{
cPos q;
q.setx(this->getx() + p.getx());
q.sety(this->gety() + p.gety());
return q;
}

ostream& operator<< (ostream& out, cPos & pos)
{
out << "(" << pos.getx() << "," << pos.gety() << ")";
return out;
}

int main()
{
cPos a, b, c;
a.setx(2);
a.sety(7);
b.setx(15);
b.sety(27);
c = a + b;
cout << c << endl;
return 0;
}



0

Response Number 2
Name: gimmpy224
Date: August 28, 2004 at 19:28:30 Pacific
Reply:

Also are templates the same thing as an overloaded operator, like do they perform the same task???

And another thing, Im very anxious to start windows programming in Visual c++ 6 with MFC.

Is it manditory that i learn about overloaded operator and the this statement before i start with MFC, cause everytime i read something about them it just throws me in the dark even more.

GIMPS


0

Response Number 3
Name: egkenny
Date: August 28, 2004 at 22:13:35 Pacific
Reply:

If you really want to learn what is going on try to learn C first and write some simple programs with it. You can create simple "console applications" for this. These applications work in a DOS-like environment an open a DOS Window like when you run the CMD.exe program. Once you are confortable with Visual C++ 6.0 this way you can start using Visual ++ 6.0 with MFC and Windows.
For DOS-like applications create a new project of type "Win32 Console Application".
For MFC Windows applications create a new project of type "MFC AppWizard (exe)".

Of course if you do not understand C++ classes then MFC Windows applications will be hard to understand.

The Standard Template Library (STL) can be thought of as a toolkit of things people used to have to write from scratch all the time. Some of the templates take overloading to the extreme and allow you to overload user defined data types and classes. It is a whole book in itself. You should not worry about it until you get a good understanding of C++ first.

I also would not worry about overloading operators until you get a good grasp of C++ and classes. You should also understand the other type of overloading first the overloading of functions. That is where the same function name takes on different uses depending upon the number and type of parameters used.


0

Response Number 4
Name: gimmpy224
Date: August 28, 2004 at 22:46:18 Pacific
Reply:

I already understand overloading of functions, and have a good grasp on c++ classes, its just the book dosnt answer some of my questions that come up.
But i went back and re read the chapter 3 times and i think i finaly found an answer.

In your example:
cPos cPos::operator+(cPos & p)
{
cPos q;
q.setx(this->getx() + p.getx());
q.sety(this->gety() + p.gety());
return q;
}

The q is a temporary cPos object, because you need an object from the class to work with the ints.
The thing im a little confused on is why there is also a need for the member object p to be there.

Is it because you need another object , that isnt defined in the function, so you can add the different ints together???

so instead of having q.getx() + q.getx()
youll have q.getx() + p.getx()???

GIMPS


0

Response Number 5
Name: egkenny
Date: August 29, 2004 at 17:50:49 Pacific
Reply:

this points to the current object which is to the left of the + operator.
p is to object to the right of the + operator.
q is result object to be returned.

You always want to return on object. This allows you to string together a number of calculations. Remember +, -, *,/, etc operators can only work on two objects at a time. With more than two objects you return the result to a temporary variable and use this as the object to the left of the next operator in the sequence. Here is an example:

Suppose you have this expresion:
a = (b + c) - d;

1st iteration:
this point to b and c is the parameter
I will call the returned object temp1

2nd iteration:
a = temp1 - d;
this point to temp1 and d is the parameter
I will call the returned object temp2

3rd iteration:
a = temp2;
Now we have to actually invoke the = operator to finish the calculation. There actually is a default operator built into classes for this. For more complex classes you have to define this action yourself. If I had done this myself it would have looked something like this:

cPos cPos::operator=(cPos & p)
{
this->setx(p.getx());
this->sety(p.gety());
return *this;
}


0

Related Posts

See More



Response Number 6
Name: BlueRaja
Date: August 29, 2004 at 21:32:20 Pacific
Reply:

...Awe man, I showed up late :(


0

Response Number 7
Name: gimmpy224
Date: August 30, 2004 at 16:41:46 Pacific
Reply:

its ok blue, im still kinda lost on this whole thing.

GIMPS


0

Response Number 8
Name: BlueRaja
Date: August 30, 2004 at 20:37:23 Pacific
Reply:

lol okay - basic, you overload operators to make programming easier.

Instead of using the following (imaginary) function on the following (hypothetical) variables:
string3 = concatenate_two_strings(string1,string2);
you can overload the + variable, and do it like this:
string3 = string1 + string2;

And in fact, you can actually do that when using the standard string library - try it! ;P


0

Response Number 9
Name: BlueRaja
Date: August 30, 2004 at 20:41:58 Pacific
Reply:

"Basic,..."
Erm...Basically* :S

"...you can overload the + variable..."
Erm...+ operator* :S :S :S

I really need a way to edit my posts on here...
Does anyone know who I'd bring that up to?


0

Response Number 10
Name: gimmpy224
Date: August 31, 2004 at 16:48:40 Pacific
Reply:

good god..... I UNDERSTAND!!! thanks Raja :)

you always come through for me.

GIMPS


0

Response Number 11
Name: BlueRaja
Date: August 31, 2004 at 20:48:29 Pacific
Reply:

Anytime ;)


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: C++ Overloading Operators

c++ operator>> overloads www.computing.net/answers/programming/c-operator-overloads/4556.html

C++ operator== overload www.computing.net/answers/programming/c-operator-overload/4636.html

Overloading operators in C++ www.computing.net/answers/programming/overloading-operators-in-c/6243.html