0
what is operator overloading?
3 Answers
+ 1
The meaning of operators are already defined and fixed for basic types like: int, float, double etc in C++ language. For example: If you want to add two integers then, + operator is used. But, for user-defined types(like: objects), you can define the meaning of operator, i.e, you can redefine the way that operator works. For example: If there are two objects of a class that contain string as its data member, you can use + operator to concatenate two strings. Suppose, instead of strings if that class contains integer data member, then you can use + operator to add integers. This feature in C++ programming that allows programmer to redefine the meaning of operator when they operate on class objects is known as operator overloading.
0
#include<iostream. h>
#include<conio. h>
class test
{
int a, b;
public:
test(void)
{
a=10;
b=20;
}
void print(void)
{
cout<<"\na="<<a<<"\nb="<<b;
}
test operator +(test);
};
test test:: operator +(test t)
{
TEST TT;
TT. A=A+T. A;
TT. B=B+T. B;
RETURN (TT);
}
void main()
{
clrscr();
test result, t1, t2;
result=t1+t2;
t1. print();
t2. print();
result. print();
getch();
}
in this code some part are written in capital word... please explain me this area.... i can not understand what actually happening there.......
0
You create a new Test object, assign its data values and return it.
So in main if I made two test objects
Test A
Test B
Test C
it would be called like C.operator+(A,B) or what most people prefer C = A + B