LAB 4 Object Oriented Programming
LAB 4 Object Oriented Programming
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).
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();
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 }
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 :
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 .
------------------------------------------------------------------------------------