Thursday, July 9, 2009

What is meant by Operator Overloading in C++?

I need a detailed explanation in easy words. Please help me !!!!

What is meant by Operator Overloading in C++?
Operator Overloading provides the ability to use the same operator to perform different actions


In C++ the satament


c = a + b


will compile successfully if a, b and c are of int and float types


And if we attempt to compile the statement when a,b and c are the objects of user-defined classes,the compiler will generate error message but with operator overloading, this can happen.


Take an example


#include%26lt;iostream.h%26gt;


class student


{


int roll_numb;


int age;


public :


student(int rn,int ag) //constructor definition


{


roll_numb = rn


age = ag


}


void operator ++() //overloading unary operator


{


age = age + 1;


}


}


void main()


{


student ram(1200,19)


++ram; //this will increase the age of ram to 20)


}


This is the example of unary operator where only one operand is required








Now the example binary operator overloading where two operands are required


#include%26lt;iostream.h%26gt;


class student


{


int roll_numb;


int age;


public :


student(int rn,int ag) //constructor definition


{


roll_numb = rn;


age = ag;


}


student operator +(student obj)


{


student temp(0,0); //roll_numb and age initialized to 0


temp.age = age + obj.age;


return temp;


}


};


void main()


{


student ram(1200,19) , mohan(1201,20);


student ramesh(0,0);


ramesh = ram + mohan; //add the age of ram and mohan


and assign the sum to the


age of ramesh


}





student operator +(student obj)-%26gt;


this operator function has student class return type and has one


student class type argument


calling of this function-%26gt;


ramesh = ram + mohan


ramesh - student class type return type


ram - the object which called the function


mohan - the student class type argument
Reply:Operator overloading just means the same thing as overloading any other thing in C++. Overloading in C++ means you are replacing something with another function which might be similar but does things a little different. In other words operator overloading is just like function overloading just applied to an operator. You can basically write a function in C++ that will overload the ++ operator and instead will do a subtraction operation. The compiler will know to use your overloaded function rather then the original function usually based on the type of variable you pass to the function or the type it returns. I would have put in some code samples but yahoo doesn't really like it when i do that.
Reply:Operator overloading means, an operator which is doing more than one jobs.


for ex: %26gt;%26gt; symbol is for getting value from keyboard (like cin %26gt;%26gt; a;)


%26gt;%26gt; symbol is also used for right shift .so this is an operator overloading.
Reply:your computer processor is not compatible with the c++ programming language you have to use another programming language
Reply:A symbol like '+' '-' '/' is called an 'operator' because is signifies an operation (+ for addition). However, the actual operation depends on the data type of the operands (5+6 makes sense but a+b does not, if the operation is additon). Operator overloading refers to the tchnique where in the same operator signifies different operation depending upon the data type. For example '+' can mean addition or concatenation according as the operands are numbers or characters. So a program using operator over loading on the '+' symbol would result in 11 if the operands are 5 %26amp; 6 and 'ab' if they are 'a' %26amp; 'b'. It would use the same line of code to give different results depending upon the operand type. Since the same operator is being used to signify two different operations, the operator is loaded with more than one operation hence the term operator overloading.
Reply:Best and easy expaination along with examples available at:
Reply:hang on ill look for ya, no thats not it, is there any codes with it?


No comments:

Post a Comment