Docsity

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

Toggle sidebar
bg1
4/13/2011
1
Ob
j
ect Oriented
j
Programming using C++
Lecture – 10
Operator Overloading
Operator Overloading
Operator overloading is one of the most exciting
features of object oriented programming
features of object oriented programming
It is a mechanism to re-define the built-in
operations of the language for user defined data
types
It provides a flexibility to enhance the normal
operations of the language, e.g,
Usman Younis
Arithmetic operations could be used to process the strings
Comparison operations could be performed on shapes
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

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 Orientedj

Programming using C++

Lecture – 10

Operator Overloading

Operator Overloading

„ Operator overloading is one of the most exciting

features of object oriented programmingfeatures of object oriented programming

„ It is a mechanism to re-define the built-in

operations of the language for user defined data

types

„ It provides a flexibility to enhance the normal

operations of the language, e.g,

Usman Younis

‰ Arithmetic operations could be used to process the strings

‰ Comparison operations could be performed on shapes

Operator Overloading (contd..)

„ Each operator must be overloaded individually for

use with the user defined typesuse w t t e use de ed types

‰ i.e., overloading the assignment operator and the

subtraction operator does not overload the −= operator

„ Operator overloading should be used to perform

the same or similar function on the class objects as

it is performed for the built-in data types

„ Operator functions may be defined as either

Usman Younis

p y

member functions or as non-member functions

‰ Non-member functions are usually made friends

‰ Member functions usually use the this pointer implicitly

Operator Overloading (contd..)

„ Overloading an operator does not change:

‰ thth e operator precedence,t d

‰ the arity of the operator, or

‰ the meaning of how the operator works on

objects and how it works on the built-in types

Usman Younis

Why Operator Overloading?

„ It enables the required operations on the user defined data types to be more readable „ E.g, band-gap energy of any material ‰ E = hv / λ (Electron Volts) class E { private: double Energy; float wave_length; //units in microns double Plank_Cnst;

Usman Younis

public: E(float wav_len): wave_length(wav_len), Plank_Cnst(4.135E-15) { E = Plank_Cnst * 3E8 / ((double) wave_length * 1E-6); } //A function which adds energy double get_energy() { return Energy; } } ;

Why Operator Overloading?

void main() { E Silicon(1.107F); E Germanium(1.85F);

E Unobtanium;

//Some Addition Options Unobtanium.add(Silicon, Germanium); Unobtanium = Silicon.add(Germanium);

Usman Younis

Unobtanium = Silicon + Germanium; }

//Obviously the last option is most natural and easy to understand

Operator Overloading

„ Operator Overloading enables to apply standard operators

(such as +,-,*,<, and so on) to objects of the programmer

defined type.

a = b + c;

Calling Party L-Value

Function name operator+

Returns an object Argument to function

Usman Younis

Argument to function R-Value , Called Party

Returns an object Stored in aa

Overloading Unary Operators

Increment Operator (contd..)

Prefix Form of the operator „ Prototype inside the class definition: ‰ E operator ++ (); „ Call in the main() ‰ E Silicon(1.107F); ‰ ++Silicon; ‰ Similarly ‰ E Temp = ++Silicon; //As the operator returns an object

Usman Younis

„ Compiler interpret it as: ‰ Silicon.operator++(); „ The basic idea is that compiler calls member function operator++.

Increment Operator (contd..)

„ Operator function definition:

E E::operator ++ () { wave_length += 0.01F; Energy = Plank_Cnst * 3E8 / ((double) wave_length * 1E-6);

return *this;

Usman Younis

Increment Operator (contd..)

Postfix Form of the operator „ Prototype inside the class definition: ‰ E operator ++ (int); „ Call in the main() ‰ E Silicon(1.107F); ‰ Silicon++; ‰ Similarly ‰ E Temp = Silicon++; //As the operator returns an object

Usman Younis

„ Compiler interpret it as: ‰ Silicon.operator++(0); „ The integer parameter is a dummy parameter and it is used only to differentiate between prefix and postfix form

Increment Operator (contd..)

„ Operator function definition:

E E::operator ++ (int) { E Temp = *this;

wave_length += 0.01F; Energy = Plank_Cnst * 3E8 / ((double) wave_length * 1E-6);

Usman Younis

return Temp; }

Binary Operators - Addition

„ Consider a class complex_number class complex_number { private: double real_part; double imag_part;

public: l b (d bl d bl b) l t( ) i g t(b)

Usman Younis

complex_number(double a, double b): real_part(a), imag_part(b) { } complex_number operator + (const complex_number& number); };

Addition (contd..)

„ Definition

complex_number complex_number :: operator + (const complex_number& number) { double a = real_part + number.real_part;

void main() { complex_number x(1.4, 2.5), y(3.78, 4.98); complex_number z = x + y; //compiler interpret it as

Usman Younis

double b = imag_part + number.imag_part;

return complex_number(a, b); }

//compiler interpret it as // x.operator+(y) }

Binary Operators

„ Similarly, we can implement the arithmetic operations for our complex number class, using operator overloading ‰ Division / ‰ Multiplication * ‰ Subtraction − ‰ Etc.

Usman Younis

„ Remember the operator overloading follows the normal precedence

Binary Operators - Relations

„ Operator overloading can be used to implement the

relational functions on user defined typesrelational functions on user defined types

‰ E.g.,

if (rect1<rect2)

//Perform some actions

‰ Instead of

Usman Younis

rect1.compare_area(rect2);

Assignment Operator

„ Assignment operator (=) is defined for all the

objects by defaultobjects by default, generated by the compiler generated by the compiler

„ The default assignment does not meet the

requirements when objects allocate dynamic

memory using their data members

„ Consider the example of a my_string class, which

allocates dynamic memory using a char* pointer,

Usman Younis

when a string is passed in its constructor

Assignment Operator (contd..)

class my_string

~my_string() { delete[] ptr; }

private:

char* ptr;

public:

my_string(): ptr(NULL) { }

my_string(char* str)

void set(char* str) { strcpy(ptr, str); }

void print() { cout<<ptr<<endl; }

void operator = (my string& strg);

Usman Younis

ptr = new char[strlen(str)+1];

strcpy(ptr, str);

void operator (my_string& strg);

};

Assignment Operator (contd..)

void main()

h ll

my_string string4;

my_string string1("hello

there");

string1.print();

my_string string2 = string1;

string2 print();

string4 = string3;

string4.set("inner braces");

string4.print();

string3.print();

“hello there”

“hello there”

“Inner braces”

“Inner braces”

Usman Younis

string2.print();

my_string string3("outer

braces");

string3.print();

hello there

Error, string is

undefined

Error at the End, Double deletion in

case of string1&2, and string 3&

Assignment Operator (contd..)

„ In situations where dynamic memory has been allocated, it is

mandatory to overload the default assignment operator

„ In our example:

void my_string:: operator = (const my_string& p)

if (this != &p) // to avoid self assignment

delete[] ptr;

Usman Younis

ptr = new char[strlen(p.ptr) + 1];

strcpy(ptr, p.ptr);