operator overloading & type conversion in cpp

Click here to load reader

download operator overloading & type conversion in cpp

of 42

Transcript of operator overloading & type conversion in cpp

Operator Overloading & Type Conversion

Chap 7Operator Overloading & Type Conversion1By:-Gourav Kottawar

Contents7.1 Defining operator Overloading 7.2 Overloading Unary Operator 7.3 Overloading Binary Operator 7.4 Overloading Binary Operator Using Friend function 7.5 Manipulating of String Using Operators 7.6 Type Conversion 7.7 Rules for Overloading Operators

2By:-Gourav Kottawar

IntroductionImportant technique allows C++ user defined data types behave in much the same way as built in types.C++ has ability to provide the operators with a special meanings for a data type.The mechanism of giving such special meaning to an operator is known as operator overloading. 3By:-Gourav Kottawar

By:-Gourav Kottawar4Why Operator Overloading?Readable codeExtension of language to include user-defined typesI.e., classesMake operators sensitive to context

Generalization of function overloading

4

By:-Gourav Kottawar5Simple Exampleclass complex {double real, imag;public:complex(double r, double i) :real(r), imag(i) {}}Would like to be able to write:complex a = complex(1, 3.0);complex b = complex(1.2, 2);complex c = b;

a = b + c;b = b+c*a;c = a*b + complex(1,2);I.e., would like to write ordinary arithmetic expressionson this user-defined class.

5

By:-Gourav Kottawar6With Operator Overloading, We Canclass complex {double real, imag;public:complex(double r, double i) :real(r), imag(i) {}

complex operator+(complex a, complex b);complex operator*(complex a, complex b);complex& operator=(complex a, complex b);...}

6

By:-Gourav Kottawar7General FormatreturnType operator*(parameters); any type keyword operator symbol

Return type may be whatever the operator returnsIncluding a reference to the object of the operandOperator symbol may be any overloadable operator from the list.

7

By:-Gourav Kottawar8C++ PhilosophyAll operators have contextEven the simple built-in operators of basic typesE.g., '+', '-', '*', '/' for numerical typesCompiler generators different code depending upon type of operands

Operator overloading is a generalization of this feature to non-built-in typesE.g., '' for bit-shift operations and also for stream operations

8

By:-Gourav Kottawar9C++ Philosophy (continued)Operators retain their precedence and associativity, even when overloaded

Operators retain their number of operands

Cannot redefine operators on built-in types

Not possible to define new operatorsOnly (a subset of) the built-in C++ operators can be overloaded

9

Restrictions on Operator Overloading

C++ operators that can be overloaded

C++ Operators that cannot be overloaded

10By:-Gourav Kottawar

7.2 Overloading Unary Operator

11By:-Gourav Kottawar

#include using namespace std; class temp { private: int count; public: temp():count(5){ } void operator ++() { count=count+1; } void Display() { cout