









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










Usman Younis
Operator Overloading (contd..)
Usman Younis
Operator Overloading (contd..)
Overloading an operator does not change:
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
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
Usman Younis
Assignment Operator
Usman Younis
Assignment Operator (contd..)
~my_string() { delete[] ptr; }
void set(char* str) { strcpy(ptr, str); }
void print() { cout<<ptr<<endl; }
void operator = (my string& strg);
Usman Younis
void operator (my_string& strg);
};
Assignment Operator (contd..)
Usman Younis
Assignment Operator (contd..)
Usman Younis