Professional Documents
Culture Documents
LAB ASSESSMENT:
Data presentation
Experimental results
Conclusion
Objectives
In this lab students will learn to overload,
⮚ Arithmetic operators
⮚ Relational operators
⮚ Logical operators
⮚ Unary operators
Equipment required
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)
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:
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:
C++ Code:
#include <iostream>
class RationalNumber
{
private:
int numerator;
int
denominator;
void simplify()
{
int main() {
RationalNumber a(3, 4);
RationalNumber b(1, 2);
RationalNumber c = a - b;
cout << "a - b = ";
c.display();
cout <<endl;
RationalNumber d = a / b;
cout << "a / b = ";
d.display();
cout << std::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.
**************************************