You are on page 1of 11

AIR UNIVERSITY

DEPARTMENT OF ELECTRICAL & COMPUTER


ENGINEERING
EXPERIMENT NO 05

Lab Title: Operator Overloading


Student Name: Hasnain Hameed Reg. No: 220645

Objective: To understand the concept of operator overloading with the help of


functions.

LAB ASSESSMENT:

Excellent Good Average Satisfactor Unsatisfacto


Attributes
(5) (4) (3) y (2) ry (1)
Ability to Conduct
Experiment
Ability to assimilate
the results
Effective use of lab
equipment and follows
the lab safety rules

Total Marks: Obtained Marks:

LAB REPORT ASSESSMENT:


Excellent Good Average Satisfactor Unsatisfacto
Attributes
(5) (4) (3) y (2) ry (1)

Data presentation

Experimental results

Conclusion

Total Marks: Obtained Marks:

Date: 25-10-2023 Signature:


EXPERIMENT 05
Operator Overloading

Objectives
In this lab students will learn to overload,

⮚ Arithmetic operators

⮚ Relational operators

⮚ Logical operators

⮚ Unary operators

Equipment required

⮚ Visual Studio/ Dev C++/Eclipse installed in Windows/PC

Operator Overloading
The process of defining additional meaning of operators is known as Operator Overloading. It
enables the operator to perform different operations depending on the type of operands. It also
enables the operators to process the user-defined data types.

In C++ the overloading principle applies not only to functions, but to operators too. That is, of
operators can be extended to work not just with built-in types but also classes. A programmer
can provide his own operator meaning to a class by overloading the built-in operator to
perform some specific computation when the operator is used on objects of that class. The
following set of operators is commonly overloaded for user-defined classes:

• = (assignment operator)
• +, -, *, \, (binary arithmetic operators)
• +=, -=, *=, (compound assignment operators)
• ==, !=, >, <, >=, <=, (comparison operators)

Operator overloading is a clear representation of operations performed, and is used because


it allows the developer to program using notation closer to the target domain and allows
user- defined types a similar level of syntactic support as types built into the language, for
example, consider variables a, b, c of some user-defined type, such as matrices:
a+b*c

In a language that supports operator overloading, and with the usual assumption that the '*'
operator has higher precedence than '+' operator, this is a concise way of writing:
add (a, multiply (b,c))

You cannot create new operators like *& and try to overload them; only existing
operators can be overloaded. The precedence of an operator cannot be changed by
overloading. The associativity (i.e., left-to-right, or right-to-left) of an operator cannot be
changed. The arity (i.e., no. of operands) of an operator cannot be changed. The meaning of
how an operator works on built-in types cannot be changed. Operator overloading works only
on objects of user defined types or with a mixture of user-defined and built-in types.
Home Lab Tasks

Q1. Write a class Time that has three data members hour, minutes and seconds. The class
has the following member functions:

⮚ A constructor to initialize the time

⮚ Show function to initialize the time

⮚ Overload ++ operator to increase the time by 1 minute.

⮚ Overload -- operator to increase the time by 1 minute.

C++ Code:

#include <iostream>

class Time
{
private:
int hour;
int minutes;
int seconds;

public:
Time(int h, int m, int s) {
hour = h;
minutes =
m; seconds
= s;
}
void show() {
cout << "Time: " << hour << ":" << minutes << ":" << seconds <<endl;
}
// Overload ++ operator
void operator++()
{
minutes++;
if (minutes == 60)
{
minutes =
0; hour++;
if (hour == 24)
{
hour = 0;
}
}
}
// Overload --
operator void
operator--()
{
minutes--;
if (minutes < 0)
{
minutes =
59; hour--;
if (hour < 0)
{
hour = 23;
}
}
}
};
int main()
{
Time t1(10, 30, 45);
t1.show();
++t1;
t1.show();

--t1;
t1.show()
; return 0;
}

Output:
Q2. Create a class RationalNumber that stores a fraction in its original form (i.e., without
finding the equivalent floating pointing result). This class models a fraction by using two data
members: an integer for numerator and an integer for denominator. For this class, provide
the following functions:

• A no-argument constructor that initializes the numerator and denominator of a


fraction to some fixed values.
• A two-argument constructor that initializes the numerator and denominator to the
values sent from calling function. This constructor should prevent a 0 denominator
in a fraction, reduce or simplify fractions that are not in reduced form, and avoid
negative denominators.
• A display function to display a fraction in the format a/b.
• An overloaded pre and post increment operator to increment the data members.
• An overloaded operator - for subtraction of two rational numbers. Two fractions a/b
and c/d are subtracted from each other as:

• An overloaded operator / for division of two rational numbers. If fraction a/b is


divided by the fraction c/d, the result is

• An overloaded relational operator >= should return a variable of type bool to


indicate whether 1st fraction is greater than or equal to 2nd or not
• Overloaded equality operator == should return a variable of type bool to indicate
whether 1st fraction is equal to the 2nd fraction or not.

C++ Code:
#include <iostream>

class RationalNumber
{
private:
int numerator;
int
denominator;

int gcd(int a, int b)


{
return b == 0 ; gcd(b, a % b);

void simplify()
{

int common = gcd(abs(numerator),


denominator); numerator /= common;
denominator /= common;
}
public:
RationalNumber() : numerator(1), denominator(1) {}

RationalNumber(int num, int denom)


{
if (denom == 0) {
cout << "Denominator cannot be zero. Setting to default (1/1)." <<endl;
numerator = 1;
denominator = 1;
} else {
numerator = num;
denominator =
denom; simplify();
if (denominator < 0)
{
numerator *= -1;
denominator *= -1;
}
}
}
void display() {
cout << numerator << "/" << denominator;
}
RationalNumber& operator++() { // Pre-increment
numerator += denominator;
simplify();
return *this;
}
RationalNumber operator++(int) { // Post-increment
RationalNumber temp = *this;
++(*this);
return temp;
}

RationalNumber operator-(const RationalNumber& other) {


int common_denominator = denominator * other.denominator;
int result_numerator = (numerator * other.denominator) - (other.numerator * denominator);
return RationalNumber(result_numerator, common_denominator);
}

RationalNumber operator/(const RationalNumber& other) {


if (other.numerator == 0) {
std::cout << "Division by zero is not allowed. Returning default (1/1)." <<
std::endl; return RationalNumber();
}
int new_numerator = numerator * other.denominator;
int new_denominator = denominator *
other.numerator;
return RationalNumber(new_numerator, new_denominator);
}

bool operator>=(const RationalNumber& other) {


int common_denominator = denominator * other.denominator;
int this_numerator = numerator * other.denominator;
int other_numerator = other.numerator * denominator;
return this_numerator >= other_numerator;
}
bool operator==(const RationalNumber& other) {
return numerator == other.numerator && denominator == other.denominator;
}
};

int main() {
RationalNumber a(3, 4);
RationalNumber b(1, 2);

cout << "a =


"; a.display();
cout << endl;

stcout << "b = ";


b.display();
cout << endl;

RationalNumber c = a - b;
cout << "a - b = ";
c.display();
cout <<endl;

RationalNumber d = a / b;
cout << "a / b = ";
d.display();
cout << std::endl;

cout << "a >= b: " << (a >= b) <<endl;


cout << "a == b: " << (a == b) <<endl;

cout << "Incrementing a. " <<endl;


a++;
cout << "a =
"; a.display();
cout <<endl;

return 0;
}
Output:

Conclusion:
In this lab we learned about operator overloading in C++. Operator
overloading can be done by implementing a function which can be a: Member
Function. Non-Member Function. Friend Function.Moreover we performed lab
tasks to understand what actually operator overloading is.Also we saw the
concept of unary operator, pre increment and decrement .In the post increment
and decrement we use a special indicator known as int which is given in the
function parameter.

**************************************

You might also like