EEE272 Lecture 12 PDF
EEE272 Lecture 12 PDF
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";
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;
- 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";
}
};
5
out << "Point(" << point.x << "," << point.y << "," << point.z <<
")";
return out;
}
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";
}
};
- 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