Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming with ANSI C ++

Similar presentations


Presentation on theme: "Programming with ANSI C ++"— Presentation transcript:

1 Programming with ANSI C ++
A Step-by-Step Approach Prof. Bhushan Trivedi Director GLS Institute of Computer Technology

2 and User Defined Conversions
Chapter 6 Operator Overloading and User Defined Conversions

3 Introduction Give an additional meaning to an existing operator
A Meaningful meaning! Is important The operator functions

4 Operator Overloading and User Defined Conversions
Comparison of both the methods of conversion Which method is to be used? Conversion and OO should not be changing the semantics of use The stack example for + and – The employee example for + and -

5 Introduction Give an additional meaning to an existing operator
A Meaningful meaning! Is important The operator functions

6 Restrictions on use of OO
Cannot change the original meaning New operators cannot be devised Ops will not have additional precedence. cannot change number of arguments at least one argument as user defined type Except (), no other operator can have a default argument. same return type for new, delete and ->

7 Operators which can not be overloaded
The dot operator for member access The dereference member to class operator .* Scope resolution operator :: Size of operator sizeof Conditional ternary operator ?: Casting operators static_cast<>, dynamic_cast<>, reinterpret_cast<>, const_cast<> # and ## tokens for macro preprocessors

8 Operators which can not be overloaded as friends
Assignment Operator = Function call operator () Array Subscript operator [] Access to class member using pointer to object operator -> To ensure their first operand is lvalue! = has global meaning as well!

9 The operator function The need for word operator preceding the real operator like operator + No explicit call! Better as a library function! Users do not need to remember function names No ambiguity about parameters and call Readability increases No misspelled functions

10 Types of operators Unary Operators Binary Operators
The dummy arguments for postfix versions of increment and decrement operators Binary Operators Overloading shorthand operators while overloading arithmetic operators The single ternery operator is not available

11 Using Friends The lvalue problem for cases like Object + scaler and Scaler + Object calls to + The body for both the cases can be different! The operator << and >> case The need to return reference

12 Overloading other Operators
The = operator Normal Overloading Extending = in the derived class The [] operator The safearray class example The need to have reference as return type Can be extended using templates

13 Overloading other Operators
The () operator The function object vs. Function pointers Inlining is possible May have cache info and help The new and delete using malloc and free The New and delete using ::new and ::delete

14 Overloading new and delete
Before Insertion After First Insertion Old Pointer Table Space pointer Current Pointer Null After 8th Insertion Overloading new and delete

15 User Defined Conversions
Conversion from built-in data type to an object Object to a built-in data type The need for such operator: the wrapper class example Object to Object Conversion using constructors using conversion functions

16 Operator Overloading and User Defined Conversions
Comparison of both the methods of conversion Which method is to be used? Conversion and OO should not be changing the semantics of use The stack example for + and – The employee example for + and -


Download ppt "Programming with ANSI C ++"

Similar presentations


Ads by Google