0% found this document useful (0 votes)
53 views

LAB 4 Object Oriented Programming

The document discusses operator overloading in C++. It provides two tasks as examples of operator overloading - one that overloads the subtraction operator (-) for a Distance class, and another that overloads the addition operator (+) for a Time class to add two times. It also provides two post-lab tasks - one to overload the four basic arithmetic operators (+, -, *, /) for an Int class and check for overflow, and another involving overloading operators for matrix operations.

Uploaded by

Hamna Baig
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

LAB 4 Object Oriented Programming

The document discusses operator overloading in C++. It provides two tasks as examples of operator overloading - one that overloads the subtraction operator (-) for a Distance class, and another that overloads the addition operator (+) for a Time class to add two times. It also provides two post-lab tasks - one to overload the four basic arithmetic operators (+, -, *, /) for an Int class and check for overflow, and another involving overloading operators for matrix operations.

Uploaded by

Hamna Baig
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

LAB-04

OPERATOR OVERLOADING

1. OBJECTIVES :
 Introduction to operator overloading operator which is basically an operator
function whose functionality has been overloaded.
 To make operator overloaded function for both unary and binary operators.
 Performing simple arithmetic operations on objects of class using overloaded
operators.

2. INTRODUCTION.:
 A  Operator overloading is used to overload or redefines most of the operators
available in C++. It is used to perform the operation on the user-defined data type.
For example, C++ provides the ability to add the variables of the user-defined data
type that is applied to the built-in data types. We have unary and binary
overloading operators.
This can be done by declaring the function, its syntax will be if we are defining it
outside the class,
Return_Type classname :: operator op(Argument list)
{ Function Body }
To declare it in the class we write return type and then operator keyword; used to
overload an operator and then the argument type.
 The binary operators take two arguments .For example ; addition (+) operator,
subtraction (-) operator and division (/) operator. The unary operators act only on
one operand. For example; increment and decrement operator ++ and --.. 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--.

3. IN LAB TASKS

i. TASK # 1

To the Distance class in the above given example in this chapter, add an overloaded -
operator that subtracts two distances. It should allow statements like dist3= dist1-
dist2;. Assume that the operator will never be used to subtract a larger number from a
smaller one (that is, negative distances are not allowed).

OBJECT ORIENTED PROGRAMMING (CSC-241)Page 1


 TASK CODE :

1 #include<stdlib.h>
2 #include <iostream>
3 using namespace std;
4 class Distance //Declaration of class
5 {
6 private: //Not accessible from outside the class
7 int feet; //Member data
8 float inches;
9 public: //Accessible from outside the class
10 Distance() : feet(0), inches(0) //Default constructor
11 {} //Empty body
12 void getdist() //declaration of member function
13 {
14 cout << "\n\tEnter feet: "; cin >> feet; //taking feet
15 cout << "\tEnter inches: "; cin >> inches; //taking inches
16 cout<<"\tDistance entered :"<<feet<<"'"<<inches<<"''"<<endl; //to display entered data
17 }
18
19 Distance operator - (Distance x) const //Operator Overloading function declaration
20 { //Operator Overloading function definition
21 Distance temp; //Declaration of object of class
22 if(feet>=x.feet) //Executes only when distance 1 feet is greater then distance 2 feet
23 {
24 temp.inches = inches + x.inches; //to add inches and storing result in temporary object
25 if(temp.inches>12)
26 {
27 temp.feet++; //because 12 inches make 1 feet
28 temp.inches =temp.inches-12; //12 inches are now added to feet
29 }
30 temp.feet = feet - x.feet - temp.feet; ;
31 }
32 else //executes when if condition is not satisfied
33 {
34 cout<<"\n\tDistance 2 is greater subtraction is not possible\n\t";
35 exit(1); //To exit from program
36 }
37 return temp; //Returning object
38 }
39 void show_distance() const //declaration of constant member function
40 {
41 cout<<"\n\t "<<feet<<"'"<<inches<<"''"; //displays of data members of objects when called
42 }
43 };
44 int main()
45 {
46 Distance dist1, dist3,dist2; //declaration of objects of class
47 cout<<"\n\tEnter distance 1 ";dist1.getdist();
48
49 cout<<"\n\n\tEnter distance 2 ";dist2.getdist();

OBJECT ORIENTED PROGRAMMING (CSC-241)Page 2


50 dist3 = dist1 - dist2; //calls operator overloaded function
51 cout << "\n\tDistance 1 = "; dist1.show_distance(); cout << endl; //To display
52 cout << "\tDistance 2 = "; dist2.show_distance(); cout << endl;
53 cout << "\tDistance 3 = "; dist3.show_distance(); cout << endl;
54 return 0;
55 }

 OUTPUT :

ii. TASK # 2 :
Write a program for class time which overloads the operator + to add two times
instead of using function. The class time should contain hours, minutes and seconds
as parameters.

 TASK CODE :

1 #include<iostream>
2 using namespace std;
3
4 class Time //Declaration of class
5 {
6 private: //Not accessible from outside the class
7 int hours; int minutes; int seconds; //declaring data member
8 static int data;
9 int id;
10 public: //accessible from outside the class
11 Time() : hours(0), minutes(0),seconds(0) //default constructor
12 {
13 data++; //data is same for all objects of class
14 id=data; //id has different value for all object
15 }

OBJECT ORIENTED PROGRAMMING (CSC-241)Page 3


16 void getdist() //member function to get time from user
17 {
18 cout<<"\n\tEnter Time "<<id; //To show which object called getdist() functiom
19 cout << "\n\tEnter Hours : "; cin >> hours;
20 cout << "\tEnter Minutes : "; cin >> minutes;
21 cout << "\tEnter Seconds : "; cin >> seconds;
22 cout<<"\tTime entered : "<<hours<<":"<<minutes<<":"<<seconds<<endl; //to display
23 }
24 Time operator + (Time x) const //declaration of overloaded operator function
25 {
26 Time temp; //declaration of temporary object
27 temp.seconds=seconds+x.seconds;//adding secs of 2 object & storing it in secs of temp object
28 if(temp.seconds>60) //executes when temporary object seconds greater than 60
29 {
30 temp.minutes++; //because 60 seconds make 1 minute
31 temp.seconds=temp.seconds-60;
32 }
33 //adding minutes of 2 object & storing it in minutes of temporary object
34 temp.minutes=minutes+x.minutes+temp.minutes;
35 if((temp.minutes)>60) //executes when temporary object minutes greater than 60
36 {
37 temp.minutes=temp.minutes-60;
38 temp.hours++; //because 60 minutes make 1 hour
39 }
40 temp.hours=hours+x.hours+temp.hours;
41 if(temp.hours>12) //time will be displayed in 12 hour format
42 {
43 temp.hours=temp.hours-12;
44 }
45 return temp;
46 }
47 void show_distance() //member function to show values in data members of objects
48 {
49 cout<<"\n\tTime "<<id<<" =\n\t "<<hours<<":"<<minutes<<":"<<seconds; //to display time
50 }
51 };
52 int Time::data(0); //initialization of static data member
53 int main()
54 {
55 Time time_1,time_2,time_3; //declaration of class
56 time_1.getdist();
57 time_2.getdist();
58 time_3=time_1+time_2; //calls overloaded operator function
59 //to display time
60 time_1.show_distance(); cout << endl;
61 time_2.show_distance(); cout << endl;
62 time_3.show_distance(); cout << endl;
63 return 0;
64 }
 OUTPUT

OBJECT ORIENTED PROGRAMMING (CSC-241)Page 4


4. POST LAB TASKS

i. TASK # 1 :
Create a class Int. Overload four integer arithmetic operators (+, -, *, and /) so that
they operate on objects of type Int. If the result of any such arithmetic operation
exceeds the normal range of ints (in a 32-bit environment)— from 2,147,483,648 to –
2,147,483,647—have the operator print a warning and terminate the program. Such a
data type might be useful where mistakes caused by arithmetic overflow are
unacceptable.
Hint: To facilitate checking for overflow, perform the calculations using type long
double. Write a program to test this class.

 TASK CODE :

1 #include<stdlib.h> //used for exit(1) to exit from program


2 #include "iostream" //Standard library for C++
3 using namespace std;
4
5 class Int //Declaration of class
6 {
7 private: //Not accessible from outside the class
8 long double number;
9 public:
10 Int( int n=0)//1 argument constructor if any parameter passed else works as default constructor
11 {

OBJECT ORIENTED PROGRAMMING (CSC-241)Page 5


12 number=n; //private data member assigned with value passed as parameter from main
13 }
14 void get_data() //member function to get number
15 {
16 cout<<"\tEnter integer "<<" :";cin>>number;
17 }
18 void display( Int x ) const //constant member function to display
19 {//This function does not changes private data member
20 cout<<"\t"<<x.number<<endl;
21 }
22 Int operator + (Int temp) //Declaration of operator + overloading function
23 {//Definition of operator overloading function
24 long double n=number+temp.number; //declaring and defining of private data member
25 cout<<"\tSum is";
26 return overflow(n);
27 }
28 Int operator - (Int temp) //Declaration of operator - overloading function
29 { //Definition of operator overloading function
30 if(number>temp.number)
31 {
32 long double n=number-temp.number; //declaring and defining of private data member
33 cout<<"\tDifference is";
34 return overflow(n);
35 }
36 }
37 Int operator * (Int temp) //Declaration of operator * overloading function
38 { //Definition of operator overloading function
39 long double n=number*temp.number; //declaring and defining of private data member
40 cout<<"\tProduct is";
41 return overflow(n);
42 }
43 Int operator / (Int temp) //Declaration of operator / overloading function
44 { //Definition of operator overloading function
45 long double n=number/temp.number; //declaring and defining of private data member
46 cout<<"\tDivision is";
47 return overflow(n);
48 }
49 Int overflow(long double num) //check for overflow of a
50 {
51 if( num>2147483647.0L || num<-2147483647.0L )//executes when value is overflowed above
52 the ranges
53 {
54 cout << "\tOverflow Error\n\n\n"; exit(1);
55 }
56 return Int(num); //Return nameless temporary object of class Int
57 }
58 };
59 int main( )
60 {
61 Int num_1, num_2(673784); //declaration of objects of class Int
62 Int num_3;

OBJECT ORIENTED PROGRAMMING (CSC-241)Page 6


63 num_3.get_data(); //Calling of member function to get data
64 num_1 = num_2+num_3; //calls operator + overloading member function
65 num_1.display( num_1); //call display() member function
66 num_1 = num_2-num_3; //calls operator - overloading member function
67 num_1.display( num_1);
68 num_1 = num_2*num_3; //calls operator * overloading member function
69 num_1.display( num_1);
70 num_1 = num_2/num_3; //calls operator / overloading member function
71 num_1.display( num_1);
72 }

 OUTPUT :

5. LAB CONCLUSION :
 Understanding why operator overloading is needed and write statements to
overload operators for class.
 How to overload an operator so we define the operator overloading function inside
the class whose objects/variables we want the overloaded operator to work with.
 Overloading of unary and binary operators .
 Operator functions are same as normal functions. The only differences are, name
of an operator function is always operator keyword followed by symbol of
operator and operator functions are called when the corresponding operator is
used.
 Performing simple arithmetic operations on objects of class .

------------------------------------------------------------------------------------

OBJECT ORIENTED PROGRAMMING (CSC-241)Page 7

You might also like