Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Name: gimmpy224
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

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;
}

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

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.

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

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 temp12nd iteration:
a = temp1 - d;
this point to temp1 and d is the parameter
I will call the returned object temp23rd 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;
}

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

"Basic,..."
Erm...Basically* :S"...you can overload the + variable..."
Erm...+ operator* :S :S :SI really need a way to edit my posts on here...
Does anyone know who I'd bring that up to?

![]() |
![]() |
![]() |

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