operator overloading

Apr 21, 2009 at 3:30am
Can anyone please explain me what is operator overloading.I read the tutorial here and I got that It can add, subtract different types of data for class or structure.I looked at the give example but I didn't get it.I will be thankful if someone gives me the most simple example of operator overloading and tells me some key points.
Apr 21, 2009 at 3:48am
Most simple example of operator overloading will be, overloading output operator. So for example, you have a class which has some data members, and you want to print it's data members. For ex.
1
2
3
4
5
6
7
8
9
10
11
12
13
class base {

public: 
int i;
char arr[10]; 

} ; 

int main() { 
base obj; 
cout<<obj; // for doing this you'll have to overload output operator 
return 0; 
} 


Just google overloading output operator and you'll know how to use it.

Hope this helps !
Apr 21, 2009 at 3:53am
but we didn't use
type operator key word and the sign of operation here ?
Apr 21, 2009 at 5:56am
Hi masiht,

That's what I said in my previous post, that google overloading output operator and you'll know how to use it.

Just give it a shot. If you still have any problems, we would be glad to help.
Apr 21, 2009 at 11:54am
overloaded operators are like other functions but they can have a different syntax when called

eg:

1
2
3
4
5
6
7
8
9
10
11
struct S
{
    int n;
    S(int i = 0):n(i){}
    S &operator ++ ()  { n++; return *this; }
};

S a = 5;
cout << ++a.n;//output: 6
a.operator ++();
cout << a.n; //output: 7 
Apr 27, 2009 at 10:17pm
I googled the operator over loading on the google but i find that every website present it in a different style.
Can someone use + as a - sign by the operator overloading.
like if this 3-5=8
Last edited on Apr 27, 2009 at 10:17pm
Apr 27, 2009 at 10:42pm
Here's a breakdown:

The built-in C++ operators normally only work on basic data types like 'int'. For example:

1
2
int myvar;
myvar = 3 + 5;  // <--- 


The marked line will use the '+' operator to add 3 and 5 together (which it can do because they're both of type 'int'), it will then use the '=' operator to assign the result (8) to 'myvar'.

This only works because 'int' is a fundamental type of the language. If you were to make a class called MyInt, it wouldn't work. Example:

1
2
3
4
5
6
7
8
9
10
11
class MyInt
{
public:
  int v;
};
//-------------
void func()
{
  MyInt myvar;
  myvar = 8;   //  error
}


Because myvar is a MyInt object and not a fundamental type, this code produces an error because the compiler can't assign the 8 (an 'int') to myvar (a 'MyInt').

Operator overloading lets you define how operators work with a specific class. So for example if we wanted the above to work, we could overload the = operator to accept an int:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class MyInt
{
public:
  int v;

  MyInt& operator = (int i)   // overload the = operator
  {
    v = i;
    return *this;
  }
};

//---------
void func()
{
  MyInt myvar;
  myvar = 8;   //  now this works okay!
}


A few things to note about operator overloading:

- You can only do it with user defined types (ie: classes).
- You don't have to be intuitive with the operators, but you should be. IE: it's legal to overload - so that it performs addition instead of subtraction, but you should never do that because it makes your code hard to follow.
- You can NOT overload operators for built in types. At least one of the operands must be a user defined type. Therefore:

1
2
3
4
5
int blah = 5 + 6; // CANNOT overload this + because '5' and '6' are both
     // built in types (int)

int blah = MyInt(5) + 6;  // CAN overload this + because 'MyInt' is a
     // user defined type 
Last edited on Apr 27, 2009 at 10:44pm
Topic archived. No new replies allowed.