
- Forum
- Programming Talk
- C and C++
- operator overloading???
operator overloading???
This is a discussion on operator overloading??? within the C and C++ forums, part of the Programming Talk category; what is the difference between overloading '=' operator and using = in normal...... what is he significance of operator overloading???...
-
12-25-2010, 07:16 AM #1
- Join Date
- Dec 2010
- Answers
- 32
operator overloading???
what is the difference between overloading '=' operator and using = in normal......
what is he significance of operator overloading???
-
Hello guys....!
Operator overload allows you to define a mathematical operator to operate on types other than numeric.Can someone tell me in very simple code that what exactly is operator overloading and what is it mainly used for...i read a couple of articles and all explained it using multidimensional arrays and vectors and i am beginner so really could'nt get it very clearly .
-
hi halbert...operator overloading is widely used to perform evaluations the way you want to use it...
simplest example is the use of left shift (<<) operator and right shift (>>) operator by cout (cout<<..) and in (in>>...) for standard IO operations...
another example is the use of + operator by string to concenate two strings...
likewise, you can have your own meaning defined for the operator...
HTH!!!
-
04-24-2011, 01:06 AM #4
- Join Date
- Apr 2011
- Answers
- 1
What is overloadimg of binary operator
-
In C++ the overloading principle applies not only to functions, but to operators too. That is, the meaning of operators can be extended from built-in types to user-defined types. In this way a programmer can provide his or her own operator to a class by overloading the built-in operator to perform some specific computation when the operator is used with objects of that class. One question may arise here: is this really useful in real world implementations? Some programmers consider that overloading is not useful most of the time. This and the fact that overloading makes the language more complicated is the main reason why operator overloading is banned in Java.
Even if overloading adds complexity to the language it can provide a lot of syntactic sugar, and code written by a programmer using operator overloading can be easy, but sometimes misleading, to read. We can use operator overloading easily without knowing all the implementation's complexities.
-
05-06-2011, 08:52 AM #6
Operator overloading is a great feature of Object Oriented Programing Language. Operator overloading enables us to use operator not only with numeric but also on alphabets.
-
05-16-2011, 07:25 AM #7
- Join Date
- May 2011
- Answers
- 5
hi guys, first let me tel u what is an operator..
OPERATOR is a symbol that performs operations(eg addition,substraction, etc..) only on variables and numbers.
Eg: int a,b,c;
c=a+b;
Allowing the operators to perform on objects(instance of a class) also, such feature is allowed in c++ and it is called operator overloading.
Eg: WITHOUT USING OPERATOR OVERLOADING
class complex //a combination of real and imaginary number eg: 2+3i
{
int real,img;
public:
complex add(complex x,complex y)
{
complex result;
result.real=x.real+y.real;
result.img=x.img+y.img;
return result;
}
void display()
{
cout<<endl<<"The complex number is"<<real<<img<0?"+i":"-i"<<img;
}
};
void main()
{
complex x,y,r;
x.real=x.img=2;
y.real=y.img=3;
r.real=add(x,y);
r.display();
getch();
}
Eg: WITH USING OPERATOR OVERLOADING
class complex //a combination of real and imaginary number eg: 2+3i
{
int real,img;
public:
complex operator+(complex x,complex y) //here + is overloaded to complex number addition
{
complex result;
result.real=x.real+y.real;
result.img=x.img+y.img;
return result;
}
void display()
{
cout<<endl<<"The complex number is"<<real<<img<0?"+i":"-i"<<img;
}
};
void main()
{
complex x,y,r;
x.real=x.img=2;
y.real=y.img=3;
r.real=x+y; // u can simply cal like this no need of function name. we canot aply a operator on object but we achieved with the help of
operator overloading.
r.display();
getch();
}
Finaly , u noticed the difference, there is no change in the function code the only difference is function name declaration and calling the function.
another important feature of operator overloading is we can apply the function(which adds two objects) to any number of objects.
eg:
complex x,y,z,r;
r=add(x,y); //correct
r=add(x,y,z); //incorrect because we wrote the code for only two object addition
r=x+y; //correct
r=x+y+z; //is also correct. although we wrote the code for two objects only.
yours.
sudheer
-
05-17-2011, 06:35 AM #8
Sudher thanks for such a detailed explanation
-
05-18-2011, 03:26 AM #9
what is the difference between urinary and binary operated over loading
-
05-19-2011, 08:42 AM #10
any one please answer the question
-
Sponsored Ads

Reply With Quote





