Presentation is loading. Please wait.

Presentation is loading. Please wait.

Operator Overloading www.lpuians.com.

Similar presentations


Presentation on theme: "Operator Overloading www.lpuians.com."— Presentation transcript:

1 Operator Overloading

2 Introduction Mechanism of giving special meaning to an operator is known as operator overloading. Gives option to give new definitions to operators. Syntax can’t be changed; but semantics can be.

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

4 Defining operator overloading
To define additional task to operator, define its meaning. Special function called operator function is used. General form of operator function is:

5 Operator functions Operator functions must be either member or friend functions. Friend Function Member Function Unary Operator 1 Binary Operator 2

6 Operator invocation Overloaded operator functions can be invoked (for unary operator) as: op x or x op Can be interpreted as: operator op (x) Example: S or S - Object

7 Operator invocation Overloaded operator functions can be invoked (for binary operator) as: x op y Can be interpreted as: x . operator op (y)  for member function operator op (x , y)  for friend function

8 Overloading Unary operators

9 Overloading Unary operators
Using Friend function, it works as:

10 Overloading Binary operators
Consider following example to add 2 complex numbers using friend function as:

11

12 Overloading + operator

13 + operator overloaded

14 The invocation: C3 = C1 + C2 is equivalent to:
Data members of C1 are accessed directly & that of C2 are accessed using dot operator

15

16 Overloading Binary operators using Friends
Friend functions can be used instead of member functions for overloading binary operator. It requires 2 arguments to be passed explicitly; whereas member function requires only one.

17 Previous example can be modified as:
Complex operator + (complex a, complex b) { complex temp; temp.x = a.x + b.x; temp.y = a.y + b.y; return (temp); }

18 Difference between Friend & Member function
Consider situation where 2 different data types are added as: A = B + 2; works for member function, but statement: A = 2 + B; will not work. 1st argument used to invoke function

19 Difference between Friend & Member function
However, friend function allows both approaches as: X op Y is interpreted as: operator op (X , Y) And for member function, it can be interpreted as: X . operator op (Y)

20 Declaration of Operator function
Example Declaration of Operator function

21 Definition of operator * functions
Example (cont.) Definition of operator * functions

22 Example (cont.)

23 Point to be noted

24 Type Conversion The statements:
converts x to integer before assigning to m. Type conversions are automatic for built-in data-types.

25 Consider following statement:
v3 = v1 + v2 //v1, v2, v3 are objects When objects are of same class, compiler doesn’t complain. Compiler doesn’t support type conversion for user-defined data-types. Therefore, conversion routines have to be developed.

26 Hence, 3 situations arrive:

27 Basic  Class type Built-in variable (duration) assigned to member variables Constructor does the job

28 Class  Basic type C++ allows us to define overloaded casting operator to convert class-type to basic-type. It converts class-type data to typename. Example: int num = obj; //object to int

29 Returning “double” value
Example Returning “double” value

30

31 One Class  other Class type
Destination Class object Source Class object Conversion can be carried out using: Constructor function (or) Conversion function

32 (a) Conversion function (used in source class)
Recall that, Casting operator function: operator typename() converts: class object  typename() typename() can be built-in or user-defined type.

33 (b) Constructor function (used in destination class)
Here, argument belongs to source class and passed to destination class for conversion. Hence, conversion constructor is placed in destination class.

34 Example

35 Conversion between objects

36 Conversion summary


Download ppt "Operator Overloading www.lpuians.com."

Similar presentations


Ads by Google