Professional Documents
Culture Documents
PDPU
INTRODUCTION…
Can we use them for user defined data types like array,
structure or class?
2
INTRODUCTION…
EXAMPLE
char name1[30] = “Pandit Dindayal ” ;
char name2[30] = “Petroleum University”;
char fullname[60];
strcpy (fullname,name1);
strcat (fullname, name2);
In C, No.
In C++, Yes.
It is known as operator overloading, or compile time
polymorphism.
Operator overloading teaches a normal operator to act on a
user-defined operand.
It means giving additional meaning to operators like +, *, - ,
++ , -- .
User-defined data types behave in much the same way as
the standard data types. 4
DEFINING OPERATOR
OVERLOADING…
5
GENERAL FORM OF OPERATOR
FUNCTION…
// definition…
complex operator + ( complex c)
{
complex t;
t.real = real + c.real;
t.imag = imag + c.imag;
return t;
}
7
// Call…
c3 = c1 + c2;
When the operator +() function is called,
The object c2 is passed to it and collected in the
object c.
The object c1 gets passed to it automatically.
8
EXAMPLE…
9
PROCESS OF OVERLOADING…
10
FEW POINTS…
11
FEW POINTS…
12
FEW POINTS…
3. Subscripting Operator : []
16
SUMMARY…
18