Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Operator Overloading in C++

Posted on 2004-03-23
4
Medium Priority
?
1,079 Views
Last Modified: 2008-03-17
Its Really Urgent : Can u please write for me a simple easy program how to overload "+" and "++" operator in C++. I will be really grateful to u,Thanx
Blessed_smmz
0
Comment
Question by:blessed_smmz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 10656032
Seems to be a homework
Sorry, we do not do homework assignments
However, we can surely help you do the same
So, post the problems u  are facing and we would help u

Amit
0
 
LVL 12

Accepted Solution

by:
Salte earned 500 total points
ID: 10656408
well, I can give you the syntax it is usually public enough. Consult any C++ book for more info on this.

class foo {
public:
      foo & operator ++ () { ....some code here...; return *this; }
};

inline const foo operator + (const foo & a, const foo & b)
{ ...some code here...; }

Notes:

The operator + does not have to be inline. Especially if it is large you should rather just have the prototype in the headerfil and put the body in a .cxx or .cpp file as you do for other functions.

The operator ++ as written is also inline. Again, if the code is large you should put the body in a .cxx or .cpp file and just have the prototype in the headerfile.

The operator ++ as written above is a prefix operator. The syntax for declaring a postfix operator is this:

foo operator ++ (int)
{
    ......body here.....
}

Note that the return type in this case is a foo and not a foo reference. This is because the postfix return the old value and so have to create a new object to return. Prefix ++ return the present value and so can simply return a reference to the object it already has. This is one of the reaosns why you should normally always prefer prefix operations before postfix operations unless you really need that postfix functionality. Specifically code like this is an abomination:

for (foo k = initial_value; k < end_value; k++) ....body here....;

In this case you really just want to increase k. It is not important that it is postfix or prefix and so therefore prefix should be used. True, some compilers can in some cases figure out that the prefix one is good enough in this situation and so will pretend you did write prefix ++ but you shouldn't rely on such optimizations.

People who write loops like that are clueless period.

Also, note the int argument to the postfix ++ operator. This argument is not used it is purely a syntactic convention to tell the compiler that "this is the postfix version". Specifically it is a bad idea to declare a name for that argument and reference it in the code.

Also, note that the + operator is declared outside of the class. This is usually GOOD STYLE. The reason for this is that you often want to write other overloads of this. For example you might want to allow people to add an int and a foo together:

const foo operator + (int a, const foo & b)  { ....code here...; }

and such a function cannot be declared in the class. Putting that function outside the class and other other + overloads inside the class is messy and should be avoided. Therefore you should usually always write all + operators and similar operators outside the class - it is a good habit. Note that all operators that assigns values must be declared inside the class so += etc are always declared inside the class and never outside.

Also, note that the return type is const foo and not just foo. The reason is simple: We want to disallow code like this:

(a + b) = c;

The result of a + b should not be modified or assigned to by anyone. You should assign it to some variable or use it in a function call or as a subexpression of a larger expression and so it should only be read not modified. Therefore the operator + return a const object.

Hope this is of help.

Alf
0

Featured Post

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question