Professional Documents
Culture Documents
Lab # 05
Lab Title: Operator Overloading (Contd.)
Submitted by:
Name Registration #
MUHAMMAD MAJID FA22-BEE-005
MALIK SHARAF FA22-BEE-002
Rubrics to follow
Rubrics # R2 R5 R7 R8 R9
In –Lab
Post- Lab
Lab 05 – Operator Overloading (Contd.)
1. Objectives
The objective of this lab is to teach the students, the use of special binary operators i.e.
comparison and compound assignment 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 of special binary 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.
The following set of operators is commonly overloaded for user-defined classes:
4. Examples
Unary
++ -- (Increment and Decrement Operators) Operators
= (Assignment Operator)
1
In our first example we’ll overload the less than operator (<) in the Distance class so
that we can compare two distance
2
4.2 Example of Concatenating Strings:
3
5. In-Lab Tasks
5.1 Use the Arithmetic Assignment operator (+=) for a Distance class to add one distance
to a second, leaving the result in the first. This is similar to example shown earlier, but
there is a subtle difference.
#include <iostream>
using namespace std;
class dist
{
private:
int feet;
int inches;
public:
void set_value()
{
cout<<"enter value of feet : ";
cin>>feet;
cout<<"enter value of inches : ";
cin>>inches;
}
void get_value()
{
cout<<"feet : "<<feet<<"\t";
cout<<"inches : "<<inches<<endl;
}
dist operator +(dist d2)
{
dist temp;
4
temp.feet=feet+d2.feet;
temp.inches=inches+d2.inches;
return dist(temp);
}
};
int main()
{
dist d1,d2;
cout<<"obj 1"<<endl;
d1.set_value();
cout<<"obj 2"<<endl;
d2.set_value();
d1=d1+d2;
cout<<"result d1+=d2 "<<endl;
d1.get_value();
return 0;
}
5.2 Write a program that substitutes an overloaded += operator for the overloaded +
operator in the example given above. This operator should allow statements like s1 += s2;
where s2 is added (concatenated) to s1 and the result is left in s1. The operator should
also permit the results of the operation to be used in other calculations, as in s3 = s1 +=
s2;
5
#include <iostream>
#include <string>
using namespace std;
class dist
{
private:
string value;
public:
void set_value()
{
cout<<"enter value : ";
cin>>value;
}
void get_value()
{
cout<<"value : "<<value<<endl;
}
dist operator +(dist d2)
{
dist temp;
temp.value=value+d2.value;
return dist(temp);
}
};
int main()
{
dist s1,s2;
cout<<"obj 1"<<endl;
s1.set_value();
6
cout<<"obj 2"<<endl;
s2.set_value();
s1=s1+s2;
cout<<"resul s1 += s2;"<<endl; s2;"<<endl;
s1.get_value();
return 0;
}
5.3 Write a program for class String that uses an overloaded == operator for comparing two
strings together. This operator should allow statements like s1 == s2; and display the results
showing whether the strings entered by user are same or not.
#include <iostream>
#include <string>
class dist
private:
string value;
public:
void set_value()
cin>>value;
void get_value()
7
{
cout<<"value : "<<value<<endl;
if(value==d2.value)
else
};
int main()
dist d1,d2;
cout<<"obj 1"<<endl;
d1.set_value();
cout<<"obj 2"<<endl;
d2.set_value();
d1==d2;
return 0;
8
6. Post Lab Tasks:
6.1. Write a program for class time which has the ability to subtract two time values using the
overloaded (- ) operator, and to multiply a time value by a number of type float, using the
overloaded (*) operator.
#include <iostream>
class time
private:
int hh,mm,ss;
public:
void set_value()
cin>>hh;
cin>>mm;
cin>>ss;
void get_value()
cout<<"hours : "<<hh;
cout<<"\tminutes : "<<mm;
cout<<"\tseconds : "<<ss;
9
{
time temp;
temp.hh=hh-t2.hh;
temp.mm=mm-t2.mm;
temp.ss=ss-t2.ss;
return time(temp);
time temp;
temp.hh=hh*f;
temp.mm=mm*f;
temp.ss=ss*f;
return time(temp);
};
int main()
time t1,t2,t3;
float flt;
cout<<"time 1"<<endl;
t1.set_value();
cout<<"time 2"<<endl;
t2.set_value();
t3=t1-t2;
t3.get_value();
cin>>flt;
t3=t1*flt;
cout<<"\nresult t1*"<<flt<<endl;
10
t3.get_value();
return 0;
6.2. Create a class Cartesian such that it compares the x and y co-ordinates of two points in a
plain using an overloaded operator and after comparison print out which one of the point will be
the head of the vector and which one will be tail if a vector would be formed between these two
points keeping that a vector will go from smallest to largest point. Also check if both points are at
the same place using overloaded operator then print out an error message “Both points are same…
Resultant vector can’t be created”.
#include <iostream>
class Cartesian
private:
int x;
int y;
11
public:
void set_value()
cin>>x;
cin>>y;
void get_value()
cout<<"("<<x<<","<<y<<")"<<"\t";
if(x>c2.x || y>c2.y)
cout<<"head is ";
cout<<"("<<x<<","<<y<<")"<<endl;
cout<<"tail is ";
cout<<"("<<c2.x<<","<<c2.y<<")"<<endl;
if(c2.x>x || c2.y>y)
cout<<"head is ";
cout<<"("<<c2.x<<","<<c2.y<<")"<<endl;
cout<<"tail is ";
cout<<"("<<x<<","<<y<<")"<<endl;
12
}
};
int main()
Cartesian c1,c2;
cout<<"point 1"<<endl;
c1.set_value();
cout<<"point 2"<<endl;
c2.set_value();
c1.get_value();
c2.get_value();
cout<<endl;
cout<<endl;
c1<=c2;
return 0;
13
14