2. OPERATOR OVERLOADING AND
TYPE CONVERSION
INTRODUCTION
DEFINING OPERATOR OVERLOADING
OVERLOADING UNARY OPERATORS
OVERLOADI NG BINARY OPERATORS
OVERLOADING BINARY OPERATORS USING
FRIENDS
SOME OTHER OPERATOR OVERLOADING
EXAMPLES
RULES FOR OVERLOADING OPERATORS
TYPE CONVERSION
3. INTRODUCTION
Operator overloading is one of the many
exciting features of the C++ language.
Operator overloading provides a flexible option
for the creation of new definition for most of the
C++ operators.
Class member access operator(.,.*)
Scope resolution operator(::)
Size operator(size of)
Conditional operator(?:)
4. DEFINNING OPERATOR
OVERLOADING
We must specify what it means in relation to the class
to which the operator is applied.
Syntax:
Return type classname::operator op(arglist)
{
function body//task defined
}
Operator functions must be either member functions or friend
functions:
vector operator+(vector);//vectoraddition
Vector operator-(vector)://unary minus
5. CONTINUE….
Friend vector operator+(vector;vector); //vector addition
Friend vector operator-(vector); //unary minus
Vector operator-(vector&a); //subtraction
Int operator==(vector,vector); //comparison
The process of overloading involves the following steps:
Create a class that defines the datatype that is to be used in the
overloading operation.
Declare the operator functions operator op() in the public part of the
class.
UNARY OPERATOR=X OP Y
BINARY OPERATOR=OPERATOR OP(X)
6. OVERLOADING UNARY
OPERATORS
A minus operator when use as a unary takes just one
operand.
Remember a statement like
It is possible to overload a unary minus operator using a friend
function as follows:
Friend void operator-(space&s);
void operator-(space&s)
S2=-S1;
S.X=-S.X;
S.Y=-S.Y;
S.Z=-S.Z;
8. OVERLOADING BINARY
OPERATORS
The same mechanism can be used to overload a binary
operator.
C1 takes the responsibility of invoking the function
and c2 plays the role of an argument that is passed to
the function.
Both the objects are available for the function.
C=SUM(A,B);
C3=C1.OPERATOR+(C2);
TEMP.X=X+C.X;
11. OVERLOADING BINARY
OPERATORS USING FRIENDS
Friend functions may be used in the place of member functions
for overloading a binary operator.
1) Replace the member function declartion by the friend
function declaration
2) [friend complex operation+(complex,complex);
3) Redefine the operator function as followa:
[complex opearor+(complex a,complex b)
{
return complex((a.x+b.x),(a.y+b.y));
}
In this case the statement
C3=C1+C2;
C3=OPERATOR+(C1+C2);
12. MANIPULATION OF STRINGS
USING OPERATORS
ANSI C implements strings using character arrays,pointers and string
functions.
EXAMPLE:
Thus we must create string objects that we can hold these two pieces of
information.
Class string
{
Char*p;
Int len;
Public:
}
String3=string1+string2;
If(string 1>=string2)string=string1;
14. SUB TOPICS
OVERLOADING THE SUBSCRIPT OPERATOR
o The subscript operator is normally used to access and
modify a specific element in an array.
OVERLOADING THE POINTER TO MEMBER -
>OPERATOR:
o The pointer to member operator is normally used to
conjunction with an object pointer to access any of the
objects member.
15. RULES FOR OVERLOADING
OPERATOR
There are certain restrictions and limitations in
overloading them,some of them are listed below
Only existing operators can be overloaded
Overloaded operators follow the syntax rules of the
original operators.
There are some operators that cannot be overloaded.
SIZE OF SIZE OF OPERATOR
. Membership operator
.* pointer to member operator
:: scope resolution operator
?: conditional operator
16. TYPE CONVERSION
The type of data to the right of an assignment
operator is automatically converted to the type of
the variable on the left.
BASIC TO CLASS TYPE:
1) It may be recalled that the use of constructor was
illustrated in a number of examples to initialize
objects.
int m;
float x=3.14159;
m=x;