La 1
La 1
Overloading
March 04, 2024, 08:30 am to 11:30 am
Instructor: Dr. M. Umar Khan
Instructions:
1. Lab Reports must be submitted in both hard and soft forms before the deadline.
2. The cover page of the assignment must clearly state the Name and Registration of the
student along with the title of the Lab.
3. For online submission use MS Team group of section BCE 2B.
4. All lab reports are assessed as per rubric defined in the course description file (CDF).
1. Objectives
The objective of this lab is to teach the students, the use of unary, binary and stream
operators for user defined classes i.e. operator overloading.
2. Outcome
2.1. At the end of this lab student will know the purpose of Operator Overloading.
2.2. Student will be able to use unary, binary and stream operators for
user defined classes.
3. Introduction
One of the nice features of C++ is that you can give special meanings to operators, when
they are used with user-defined classes. This is called operator overloading. You can
implement C++ operator overloads by providing special member-functions on your
classes that follow a particular naming convention. For example, to overload the +
operator for your class, you would provide a member-function named operator+ on your
class.
4. Examples
= (Assignment Operator)
Page 1 of 5
4.1. Example of Unary Operators Overloading
The unary operators operate on a single operand and following are the examples
of Unary operators:
The unary operators operate on the object for which they were called and normally,
this operator appears on the left side of the object, as in !obj, -obj, and ++obj but
sometime they can be used as postfix as well like obj++ or obj--.
Following example explain how increment (++) operator can be overloaded for
prefix as well as postfix usage. Similar way you can overload operator (--).
4.2. Example of Binary Operators Overloading
The binary operators take two arguments and following are the examples of Binary
operators. You use binary operators very frequently like addition (+) operator,
subtraction (-) operator and division (/) operator.
Following example explain how addition (+) operator can be overloaded. Similar
way you can overload subtraction (-) division (/) or any other binary operators.
#include
<iostream> using
namespace std;
class Box
{
double length; // Length of a
box double breadth; // Breadth of a
box double height; // Height of a
box
public:
double getVolume(void)
{
return length * breadth * height;
}
void setLength( double len )
{
length = len;
}
Page 2 of 5
{
height = hei;
}
// Overload + operator to add two Box
objects. Box operator+(const Box& b)
{
Box box;
box.length = this->length + b.length;
box.breadth = this->breadth +
b.breadth; box.height = this->height
+ b.height; return box;
}
};
// box 1 specification
Box1.setLength(6.0);
Box1.setBreadth(7.0);
Box1.setHeight(5.0);
// box 2 specification
Box2.setLength(12.0);
Box2.setBreadth(13.0);
Box2.setHeight(10.0);
// volume of box 1
volume =
Box1.getVolume(); cout
<< "Volume of Box1 : ";
cout << volume <<endl;
// volume of box 2
volume =
Box2.getVolume(); cout
<< "Volume of Box2 : ";
cout << volume <<endl;
// volume of box 3
volume =
Box3.getVolume(); cout
<< "Volume of Box3 : ";
cout << volume <<endl;
system("pause");
}
Page 3 of 5
4.3. Example of Binary Operators Overloading
A date is an ideal candidate for a C++ class in which the data members (month,
day, and year) are hidden from view. An output stream is the logical destination
for displaying such a structure. This code displays a date using the cout object:
Date dt( 1, 2, 92 );
To get cout to accept a Date object after the insertion operator, overload the
insertion operator to recognize an ostream object on the left and a Date on the
right. The overloaded << operator function must then be declared as a friend of
class Date so it can access the private data within a Date object.
#include
<iostream> using
namespace std;
class Date
{
int mo, da, yr; //Variable
Declaration public:
//Constructor
Date(int m, int d, int y)
{
mo = m; da = d; yr = y;
}
friend ostream& operator<<(ostream& os, const Date& dt);
};
/////////////main
function////////// void main()
{
Date dt(5, 6,
92); cout<<dt;
system("pause");
}
Page 4 of 5
5. Lab Tasks
5.1. Create a counter class, overload ++ operator for counter post and pre
increment, use the object of counter class as a loop counter for printing a
table in main function.
5.2. A complex number is a number which can be put in the form a + bi. Create a
class for complex numbers, which handle real and imaginary part
separately. Class should consist minimum required constructors, get and
show methods also Overload the + operator for this class which work like
this formula.
5.3. Create a class of Distance including feet and inches. Class should consist
minimum required constructors, get and show methods also overload the %
operator for this class.
6. Home Tasks
6.1. Create a calculator for the complex number by creating a class of complex
number with overloading all operators in it.(Operators: ++,--,+,-,/,*, >>, <<).
Page 5 of 5