Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Operator Overloading. Introduction It is one of the important features of C++ language  Compile time polymorphism. Using overloading feature, we can."— Presentation transcript:

1 Operator Overloading

2 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 (?:)

3 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

4 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.

5 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.

6 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) ?:

7 Overloading of Unary Operators ++ & -- Operator Unary – Operator >> & << Operator Note: - already discussed in the lab.

8 Overloading of Binary Operators Arithmetic operators (+, -, *, /) Relational Operators (, ==, =, !=)

9 Program: - Arithmetic Operator class 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; }

10 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.

11 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.

12 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);

13 Thank You!!!


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

Similar presentations


Ads by Google