Lab 5
Lab 5
Operator Overloading
LAB OBJECTIVE:
1. To deal with the concept of different operators.
2. To give an importance of operator overloading and their types.
INTRODUCTION:
1. Operators:
Operators help the programmers to do some operation what they want to do. Operators
have different types. Operators can be relational, operational, unary & binary.
2. Types of Operators:
In this lab or in operator overloading we will deal with just two operators.
Binary Operators and Unary Operators.
Unary Operators:
This type of operators can execute on a single operand.
Binary Operators:
This type of operators based on two arguments or operands.
3. Syntax of Operator Overloading:
The syntax is very simple, and we can easily use it in function prototype or function
definition. The syntax is given below:
operator + ()
We will use operator keyword, then we will apply what operator we need.
4. Main purpose of Operator Overloading:
In C++, there is a useful benefit of operator overloading. It is a simplest way to write a
perfect, readable and efficient code. In previous cases, we used functions to add two
objects but with operator overloading, it can be handled very easily to add objects and
assign their values to other objects.
IN-LAB TASKS
Task#01
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.
Code:
#include <iostream>
#include <stdlib.h>
using namespace std;
class Distance /*defining of class*/
#include <string.h> //declaration of libraries
{
private: //declaring private members
int feet;
float inches;
public:
Distance() //default constructor
{
feet=0;
inches=0;
}
Distance(int F,float I) //2-argument constructor
{
feet = F; /*assigning of value*/
inches = I;
}
void get_data() //function for getting the values
{
cout <<"\n\t\t\t\t Please enter the feet = "; cin >> feet;
cout <<"\n\t\t\t\t Please enter the inches = "; cin >> inches;
}
void display() const //function for displaying the values
{
cout << feet <<"'\""<< inches << endl;
}
Distance operator += (Distance d2) //function for operator
overloading
{
Task#02
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;
Code:
#include <iostream> //declaring libraries
using namespace std;
#include<string.h>
#include<stdlib.h>
/*defining of class*/
class String
{
private: //declaration of private members
enum {SZ = 80};
char str[SZ];
public:
String() //default constructor
{
}
};
int main() /*starting with main function*/
{
/*declaring and initializing of objects*/
String s1 = "\nAbdul Wahab";
String s2 = " A lagend of EE-3";
String s3;
s1.disp(); //calling of functions
s2.disp();
s1+=s2; //calling of operator overloading
s1.disp();
cout << endl;
}
console window
Task#03
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.
Code:
#include <iostream>
using namespace std;
#include<string.h> /*declaration of libraries*/
#include<stdlib.h>
class String //defining of class
{
private:
enum {SZ = 80};
char str[SZ]; //character array
public:
String operator== (String ss1) //operator overloading function
{
if(!strcmp(str,ss1.str)) //checking the condition
{
cout <<"\n\t\t\t\t "<< str <<" equal to "<< ss1.str;
}
else
{
cout <<"\n\t\t\t\t "<< str <<" not equal to "<< ss1.str;
}
}
void get_data()
{
/*getting data*/
cout <<"\n\t\t\t\t Please enter the string = "; cin >> str;
}
};
int main() //starting with main function
{
String s1; /*declaration of object*/
String s2;
s1.get_data(); /*calling of function to get value from user*/
s2.get_data();
s1 == s2; /*operator calling*/
cout << endl;
}
console window
POST-LAB TASKS
Task# 01
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.
Code:
#include<iostream> //declaration of libraries
using namespace std;
class TIME //defining of class
{
private:
int Sec,Min,Hours; //declaration of data members
public:
TIME() //default constructor
{
Sec = 0; //initializing
Min = 0;
Hours = 0;
}
TIME(int S,int M,int H) //3-arguments constructors
{
Sec = S; //assigning the values
Min = M;
Hours = H;
}
void get_data() //function for getting the data
{
cout <<"\n\t\t\t\t Please enter the seconds = "; cin >> Sec;
cout <<"\t\t\t\t Please enter the minutes = "; cin >> Min;
cout <<"\t\t\t\t Please enter the Hours = "; cin >> Hours;
}
void Disp_data() //function for displaying the data
{
cout << Hours <<" : "<< Min <<" : "<< Sec;
}
TIME operator - (TIME T) //function for - operator overloading
{
TIME temp; //declaration of object
temp.Sec = Sec - T.Sec; //subtracting
if(temp.Sec >= 60) //checking of condition
{
temp.Sec -= 60; //decrementing & incrementation
if(temp.Min >= 60)
{
temp.Min -=60;
Hours++;
}
temp.Hours = Hours - T.Hours;
return temp; //returning the value
}
TIME operator * (float t) //overloading of operator *
{
TIME TEMP;
TEMP.Hours = Hours * t; //multiplying the time with float number
TEMP.Min = Min * t;
TEMP.Sec = Sec * t;
return TEMP; //returning of value
}
};
int main() //starting with main function
{
TIME t1,t2,t3; //declaration of objects
float NUM;
cout <<"\n\n\t\t\t\t Please enter the data of Time 1!";
t1.get_data(); //getting data from user
cout <<"\n\t\t\t\t Please enter the data of Time 1!";
t2.get_data();
cout <<"\n\t\t\t\t Time 1 is = ";
t1.Disp_data(); //displaying data on console
cout <<"\n\t\t\t\t Time 2 is = ";
Min++;
}
temp.Min = Min - T.Min;
t2.Disp_data();
t3 = t1-t2; //calling of function of operator overloading
cout <<"\n\t\t\t\t Time 1 - Time 2 = ";
t3.Disp_data();
cout <<"\n\n\t\t\t\t Enter a float number you want to multiply with Time
1 = ";
cin >> NUM; //getting of a float number
t3 = t1*NUM; //calling of function of operator overloading
cout <<"\t\t\t\t Time 1 multiplied by float number = ";
t3.Disp_data();
cout <<"\n\n\t\t\t\t Enter a float number you want to multiply with Time
2 = ";
cin >> NUM;
t3 = t2*NUM;
cout <<"\t\t\t\t Time 2 multiplied by float number = ";
t3.Disp_data(); //calling of function
cout << endl;
}
console window
Task#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”.
Code:
#include<iostream>
#include<math.h>
using namespace std;
class Cartesian
{
private:
int x,y,R;
public:
Cartesian()
{
x=y=0;
}
int result (){
R=sqrt((x*x)+(y*y));
return R;
}
void get(){
cout<<"\nplease enter the x coordinate";cin>>x;
cout<<"\nplease enter the y coordinate";cin>>y;
}
void display(){
cout<<"\n"<<x<<","<<y;
}
bool operator ==(Cartesian B)
{
if(this->result()==B.result()){
return 1;
}
else{
return 0;
}
}
};
int main()
{
Cartesian A[2];
cout <<"\nplease enter 1st coordinate";A[0].get();
cout <<"\nplease enter 2nd coordinate";A[1].get();
if(A[0]==A[1]){
cout <<"wrong";
}
else if(A[0].result()>A[1].result()){
cout<<"\nHead point";A[0].display();
cout<<"\ntail point";A[1].display();
}
else
{
cout<<"\nHead point";A[1].display();
cout<<"\ntail point";A[0].display();
}
console window
Conclusion:
i. After this lab, we can excess operator overloading and use it in our program.
ii. In lab tasks, we used different operators and overloaded them.
iii. In these tasks, we dealt with major concepts of how to overload different types of
operators.
iv. In post lab task, we dealt with minus operator to subtract the time and also multiplication
of float with time.