0% found this document useful (0 votes)
72 views6 pages

EEE272 Lecture 12 PDF

This document discusses operator overloading in C++. Specifically, it covers: 1) Overloading the insertion (<<) and extraction (>>) operators for a Point class to allow printing and reading points from streams. 2) The prototype and definition for overloading operator<< to print points, returning the stream to allow chaining calls. 3) Similarly overloading operator>> to extract point data from a stream, returning the stream. 4) A reading assignment on overloading unary operators like ++, -- and !.

Uploaded by

Baphelele Mnisi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views6 pages

EEE272 Lecture 12 PDF

This document discusses operator overloading in C++. Specifically, it covers: 1) Overloading the insertion (<<) and extraction (>>) operators for a Point class to allow printing and reading points from streams. 2) The prototype and definition for overloading operator<< to print points, returning the stream to allow chaining calls. 3) Similarly overloading operator>> to extract point data from a stream, returning the stream. 4) A reading assignment on overloading unary operators like ++, -- and !.

Uploaded by

Baphelele Mnisi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

UNIVERSITY OF ESWATINI

FACULTY OF SCIENCE & ENGINEERING


DEPARTMENT OF ELECTRICAL AND ELECTRONIC
ENGINEERING

LECTURE NOTES
ON PROGRAMMING TECHNIQUES 2

LECTURE 12
Operator Overloading Part 2

2
Input and Output Operators
- We have been using stream extraction(input) and stream insertion (output) operators
to input data from basic data types, where variables, and strings are operands
- C++ allows the programmer to overload these operators for class objects
- Overloading the stream extraction and stream insertion operators, which will be
referred to as operator<< and operator>> respectively, makes it easier to work with
objects when writing programs
- Imagine you wanted to print a sequence of items from an object as shown below ,

Example
class Point
{
private:
double x, y, z;

public:
Point(double x1, double y1, double z1) : x(x1), y(y1), z(1)
{

double getX()
{
return x;
}

double getY()
{
return y;
}

double getZ()
{
return z;
}

};

- This could be done by adding a display member function in Point, then calling the
member function on an object as shown below

3
- But function display() cant be called in the middle of a statement e.g.
cout << "My point is: " << point1.display() << "in the cartesian plane. \n";

- This can only be achieved thus;


cout << "My point is: ";
point1.print();
cout<< in cartesian plane.\n";

- What other method can we use to display the coordinates?


- It turns out we can overload operator<< for class Point objects, then the coordinates
can be displayed in a single statement as shown below,
cout << "My point is: " << point1<< "in the cartesian plane. \n";

4
Overloading Operator<<
- Operator<< is a binary operator which normally has the cout object as an operand on
the left handside and basic data type operand on the right i.e.
cout<<operand of any basic datatype>

Example:
cout<<3000;, cout<<”my point: \n”;

- We have already mentioned that operator<< can be overloaded with a class object
such that cout<<point1 is realized.
- Note that cout is an object of type ostream, ostream is a class for which cout is an
object.
- An overloaded function to be used by the operator function call, cout<<point1, will
have the prototype shown below;

friend ostream& operator<<(ostream& out, const Point& point);

- The assumption is that it has been overloaded as a friend function of class Point
- The function definition will be as shown below,
class Point
{
friend ostream& operator<<(ostream& out, const Point& point);
private:
double x, y, z;

public:
Point(double x1, double y1, double z1) : x(x1), y(y1), z(1)
{

double getX()
{
return x;
}

double getY()
{
return y;
}

double getZ()
{
return z;
}
void display() const
{
cout << "point(" << x << "," << y << "," << z << "\n";

}
};

ostream& operator<<(ostream& out, const Point& point)


{

5
out << "Point(" << point.x << "," << point.y << "," << point.z <<
")";
return out;
}

- The parameter out is a reference to cout


- Object out is returned to allow for chaining of calls to operator<<

Example
int main()
{
Point point1(3, 5, 8), point2(6, 1, 5);
//print the coordinates of the points
cout << point1 << point2 << "\n";
return 0;
}

- The execution of the statement to print the coordinates of each point will start from
the left, starting with cout<<point1, which returns object out,
- Then out<<point2 will follow which will also return out,
- Finally it will execute out<<”\n”;

Overloading Operator>>
- Overloading of operator>> can be done in a similar manner
- The overloading differs in that the left operand cin is an object of type istream
- Class istream is part of the C++ standard library

Example

class Point
{
friend ostream& operator<<(ostream& out, const Point& point);
friend istream& operator>>(istream& in, Const Point& point);
private:
double x, y, z;

public:
Point(double x1, double y1, double z1) : x(x1), y(y1), z(1)
{

double getX()
{
return x;
}

double getY()
{
return y;
}

double getZ()
{
return z;
}
void display() const

6
{
cout << "point(" << x << "," << y << "," << z << "\n";

}
};

ostream& operator<<(ostream& out, const Point& point)


{
out << "Point(" << point.x << "," << point.y << "," << point.z << ")";
return out;
}

istream& operator>>(istream& in, Const Point& point)


{
in << point.x;
in << point.y;
in >> point.z;
return in;
}

- Similarly here, the object in is returned to allow for consecutive calls to operator>> in
a single statement.

Reading Assignment
- Read about unary operator overloading of operator++, operator--, and operator! from
the reference textbook
- As a reward for your reading and understanding you will be given 10% bonus marks
in the test, which you will ‘claim’ by answering basic questions related to the
overloading of these operators

You might also like