Operator Overloading in C++: Friend Functions and I/O Operators, Slides of Object Oriented Programming

Advanced topics in c++ programming, specifically operator overloading using friend functions and the overloading of input and output operators. The limitations of using member functions for operator overloading and introduces friend functions as a solution. It also provides examples of overloading the insertion and extraction operators for a complex number data type.

Typology: Slides

2011/2012

Uploaded on 07/31/2012

saqqi
saqqi 🇵🇰

4

(33)

40 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
4/14/2011
1
Ob
j
ect Oriented
j
Programming using C++
Lecture – 11
Operator Overloading Continued
Operator Overloading
So far, we have used operator overloading to re-define
the built
-
in operations of the language for user defined
the built
in operations of the language for user defined
data types
For this, the overloaded operators are made member
functions of a class
E.g.,
class complex_number
{
Usman Younis
private:
//private data
public:
complex_number operator + (const complex_number& number);
};
pf3
pf4
pf5

Partial preview of the text

Download Operator Overloading in C++: Friend Functions and I/O Operators and more Slides Object Oriented Programming in PDF only on Docsity!

Object Orientedj

Programming using C++

Lecture – 11

Operator Overloading Continued

Operator Overloading

„ So far, we have used operator overloading to re-define

the built-in operations of the language for user definedthe built in operations of the language for user defined

data types

„ For this, the overloaded operators are made member

functions of a class

E.g., class complex_number {

Usman Younis

private: //private data public: complex_number operator + (const complex_number& number); };

Operator Overloading (contd..)

„ This, however, may not be applicable when the object

its self has to be passed as an argument to the operatorits self has to be passed as an argument to the operator

function

E.g., complex_number N1; cout<<N1; The compiler will interpret it as cout.operator<<(N1)

Usman Younis

cout is an object of the class ostream in C++.

Therefore, to print complex_number in our case the

insertion operator (<<) in class ostream has to be

overloaded. Bit cumbersome!

Overloading using Friends

„ Solution: Friend function

(We’ll come to the insertion operator overloading later!)(We ll come to the insertion operator overloading later!)

E.g.,

class complex_number { private: //private data

Usman Younis

public: friend complex_number operator + (complex_number& number1, complex_number& number2 ); };

Overloading I/O

„ Extraction:

int a;int a;

cin>>a;

//compiler’s interpretation

//cin.operator>>(a);

„ Insertion:

double b;

Usman Younis

double b;

cout<<b;

//compiler’s interpretation

//cout.operator<<(b);

Overloading I/O (contd..)

„ Example: class complex numberp _ { private: double real_part; double imag_part;

public: complex number(double a double b): real part(a) imag part(b)

Usman Younis

complex_number(double a, double b): real_part(a), imag_part(b) { } friend void operator>>(istream& in, complex_number& N); friend void operator<<(ostream& out, complex_number& N); };

Overloading I/O (contd..)

void operator>>(istream& in, complex_number& N) { cout<<“Enter a complex number (real part, imaginary part) : ”; cout<<endl;

in>>N.real_part>>N.imag_part; }

void operator<<(ostream& out, complex_number& N)

Usman Younis

p ( , p _ ) { out<<“Complex number is : ”<<endl <<N.real_part<<“ + i”<<N.imag_part<<endl; }

Overloading I/O (contd..)

void main() {{ complex_number cNumber;

cin>>cNumber; //compiler’s interpretation is //operator>>(cin, cNumber);

Usman Younis

cout<<cNumber; //compiler’s interpretation is //operator<<(cout, cNumber); }