Solved

Operator Overloading in C++

Posted on 2004-03-23
4
1,052 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
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 125 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

828 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