13
Operator Overloading

Operator Overloading. Introduction It is one of the important features of C++ language Compile time polymorphism. Using overloading feature, we can

Embed Size (px)

Citation preview

Page 1: Operator Overloading. Introduction It is one of the important features of C++ language  Compile time polymorphism. Using overloading feature, we can

Operator Overloading

Page 2: Operator Overloading. Introduction It is one of the important features of C++ language  Compile time polymorphism. Using overloading feature, we can

Introduction It is one of the important features of C++

language Compile time polymorphism. Using overloading feature, we can add two user

defined data types such as, objects, with the same syntax, just as basic data types.

We can overload almost all the C++ operators except the following:

Class member access operators( ., .*) Scope resolution operator (::) Size operator (sizeof) Conditional operator (?:)

Page 3: Operator Overloading. Introduction It is one of the important features of C++ language  Compile time polymorphism. Using overloading feature, we can

Defining Operator Overloading It is done with the help of a special function,

called operator function, which describes the special task to an operator.

General form:

return type classname :: operator op (argulist)

{

function body //task defined

}where return type type of value returned by the specified

operation & op operator being overloaded.

operator op function name

Page 4: Operator Overloading. Introduction It is one of the important features of C++ language  Compile time polymorphism. Using overloading feature, we can

Steps :

Create a class that defines the data type i.e. to be used in the overloading operation.

Declare the operator function operator op() in the public part of the class.

Note: It may be either a member function or a friend function.

Define the operator function to implement the required operations.

Page 5: Operator Overloading. Introduction It is one of the important features of C++ language  Compile time polymorphism. Using overloading feature, we can

Rules for overloading operators Only existing operators can be overloaded. New

operators cannot be created. At least one operand (user defined type). Cannot change the basic meaning of the operator. Follow the syntax rules of the original operators. Some operators cannot be overloaded. Cannot use friend function to overload certain operators. When using binary operators overloaded through a

member function, the left hand operand must be an object of the relevant class.

Binary arithmetic operators must explicitly return a value.

Page 6: Operator Overloading. Introduction It is one of the important features of C++ language  Compile time polymorphism. Using overloading feature, we can

Not used / overloaded

Operators that cannot Where a friend

be overloaded function can’t be used

Sizeof = (assignment)

. (membership operator) ( ) (function call)

.* (pointer to member operator) [ ] (subscripting)

:: (class member access)

?:

Page 7: Operator Overloading. Introduction It is one of the important features of C++ language  Compile time polymorphism. Using overloading feature, we can

Overloading of Unary Operators

++ & -- Operator Unary – Operator >> & << Operator

Note: - already discussed in the lab.

Page 8: Operator Overloading. Introduction It is one of the important features of C++ language  Compile time polymorphism. Using overloading feature, we can

Overloading of Binary Operators

Arithmetic operators (+, - , *, /)

Relational Operators (<, >, ==, <= ,>=, !=)

Page 9: Operator Overloading. Introduction It is one of the important features of C++ language  Compile time polymorphism. Using overloading feature, we can

Program: - Arithmetic Operatorclass complex

{ float x,y;

public:

complex() { } int main()

complex(float real, float img) {

{ x = real; y = img;} complex c1, c2, c3;

complex operator + (complex); c1 = complex(2.5, 3.5);

void display(void); } ; c2 = complex(1.5, 2.5);

complex complex :: operator + (complex c) c3 = c1 + c2;

{ complex temp; cout<< “c1”; c1.display();

temp.x = x + c.x; cout<< “c2”; c2.display();

temp.y = y + c.y; cout<< “c3”; c3.display();

return (temp);} return 0 ;

void complex :: display (void) }

{ cout << x << “ +I ” << y << endl; }

Page 10: Operator Overloading. Introduction It is one of the important features of C++ language  Compile time polymorphism. Using overloading feature, we can

Explain coding

complex complex :: operator + (complex c)

{ complex temp;

temp.x = x + c.x;

temp.y = y + c.y;

return (temp);}

Features: - 1. it receives only one complex type argument explicitly

2. It returns a complex type value.

3. It is a member function of complex.

Page 11: Operator Overloading. Introduction It is one of the important features of C++ language  Compile time polymorphism. Using overloading feature, we can

Overloading Binary Operators Using Friends

Friend functions may be used in the place of member functions for overloading a binary operator.

Difference : - a friend function requires two arguments to be explicitly passed to it, while a member function requires only one.

Page 12: Operator Overloading. Introduction It is one of the important features of C++ language  Compile time polymorphism. Using overloading feature, we can

Using Friend Function

Replace the member function declaration by the friend function declaration. friend complex operator + (complex, complex);

Redefine the operator function as follows: complex operator + (complex a, complex b)

{

return complex ((a.x + b.x), (a.y + b.y));

}

In this case, the statement c3 = c1 + c2; is equivalent to

c3 = operator + (c1, c2);

Page 13: Operator Overloading. Introduction It is one of the important features of C++ language  Compile time polymorphism. Using overloading feature, we can

Thank You!!!