Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Operator Overloading - Object Oriented Programming - Lecture Slides, Slides of Object Oriented Programming

Operator overloading, Class, Function implementation, Single mathematical statement, Alternative way, Mathematical expression, C pp code, Predefined types, Low level function for addition are the points you can learn in this object oriented programming subject.

Typology: Slides

2011/2012

Uploaded on 11/09/2012

bacha
bacha 🇮🇳

4.3

(41)

215 documents

1 / 32

Toggle sidebar

Related documents


Partial preview of the text

Download Operator Overloading - Object Oriented Programming - Lecture Slides and more Slides Object Oriented Programming in PDF only on Docsity!

Object-Oriented Programming

(OOP)

Lecture No. 16

  • Consider the following class:

class Complex{ private: double real, img; public: Complex Add(const Complex &); Complex Subtract(const Complex &); Complex Multiply(const Complex &); … } Docsity.com

  • Function implementation:

Complex Complex::Add(

const Complex & c1){

Complex t;

t.real = real + c1.real;

t.img = img + c1.img;

return t;

}

  • The following statement:

Complex c3 = c1.Add(c2);

Adds the contents of c2 to c1 and assigns it to c3 (copy constructor)

  • To perform operations in a single

mathematical statement e.g:

c1+c2+c3+c

  • We have to explicitly write:

c1.Add(c2.Add(c3.Add(c4)))

  • Alternative way is:

t1 = c3.Add(c4);

t2 = c2.Add(t1);

t3 = c1.Add(t2);

  • If the mathematical expression is big:
    • Converting it to C++ code will involve complicated mixture of function calls
    • Less readable
    • Chances of human mistakes are very high
    • Code produced is very hard to maintain
  • C++ provides a very elegant solution:

Operator overloading

  • C++ allows you to overload common operators like + , - or ***** etc…
  • Mathematical statements don’t have to be explicitly converted into function calls
  • Assume that operator + has been overloaded
  • Actual C++ code becomes:

c1+c2+c3+c

  • The resultant code is very easy

to read, write and maintain

  • C++ automatically overloads operators for pre-defined types
  • Example of predefined types:

int float double char long

  • Example:

float x;

int y;

x = 102.02 + 0.09;

Y = 50 + 47;

The compiler probably calls the correct

overloaded low level function for addition

i.e:

// for integer addition: Add(int a, int b)

// for float addition: Add(float a, float b)

  • Operator functions are not usually

called directly

  • They are automatically invoked to

evaluate the operations they

implement

  • List of operators that can be overloaded in C++:
  • List of operators that can’t be overloaded:
  • Reason: They take name, rather than

value in their argument except for ?:

  • ?: is the only ternary operator in

C++ and can’t be overloaded

  • The precedence of an operator is

NOT affected due to overloading

  • Example:

c1c2+c*

c3+c2c*

both yield the same answer

  • Associativity is NOT changed due

to overloading

  • Following arithmetic expression

always is evaluated from left to

right:

c1 + c2 + c3 + c

  • Unary operators and assignment

operator are right associative, e.g:

a=b=c is same as a=(b=c)

  • All other operators are left

associative:

c1+c2+c3 is same as

(c1+c2)+c

  • Always write code representing

the operator

  • Example:

Adding subtraction code inside the

  • operator will create chaos
  • Creating a new operator is a syntax

error (whether unary, binary or ternary)

  • You cannot create $
  • Arity of an operator is NOT affected by

overloading

  • Example:

Division operator will take exactly two operands in any case:

b = c / d

Binary operators

  • Binary operators act on two quantities
  • Binary operators:

Binary operators

  • General syntax:

Member function:

TYPE 1 CLASS::operator B_OP(

TYPE 2 rhs){

...

}

Binary operators

  • General syntax:

Non-member function:

TYPE 1 operator B_OP(TYPE 2 lhs, TYPE 3 rhs){

...

}

Binary operators

  • The “ operator OP ” must have at

least one formal parameter of type

class (user defined type)

  • Following is an error:

int operator + (int, int);