Oop Lab 5

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

AIR UNIVERSITY

DEPARTMENT OF ELECTRICAL & COMPUTER


ENGINEERING

EXPERIMENT NO 5

Lab Title: Operator Overloading


Student Name: Reg. No:

Objective:

LAB ASSESSMENT:
Attributes Excellent Good Average Satisfact Unsatisfact
(5) (4) (3) ory (2) ory (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:


Attributes Excellent Good Average Satisfact Unsatisfact
(5) (4) (3) ory (2) ory (1)

Data presentation

Experimental results

Conclusion

Total Marks: Obtained Marks: Date: Signature:

EXPERIMENT NO 5
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.
An operator can be overloaded by declaring a special member function in the class. The member
function uses the keyword operator with the symbol of operator to be overloaded

Syntax

Return_type operator op()


{
Function body
}

Return_type: It indicates the type of value returned by the member function Operator: It is a
keyword that indicates that the member function is used to overload an opertor Op: It is the
symbol of operator to be overloaded e.g +,-, ++ etc

Example: Write a program that overloads prefix increment operator to work with user-defined
objects
Overloading Unary Operators
Operator Overloading example

#include <iostream> int main()


#include<conio.h> { count obj;
using namespace std; obj.show();
++obj;
class count obj.show();
{ getch();
int n; }
public:

count()
{
n=0;
}

void show(){
cout<<"count is"<<n<<endl;
}
//operator overloading
void operator ++()
{
n=n+1;
}
};

Overloading Binary Operators


Operator Overloading example
#include <iostream> int main()
#include<conio.h> { Add x,y,z;
using namespace std; x.in();
class Add y.in();
{ z=x+y;
int a,b; //.show();
public: //y.show();
Add() z.show();
{ a=0; getch();
b=0;} }
void in()
{ cout<<"Enter a:";
cin>>a;
cout<<"Enter b:";
cin>>b;}

void show(){
cout<<"a="<<a<<endl;
cout<<"b="<<b<<endl;
}
Add operator +(Add p)
{
Add temp;
temp.a=p.a+a;
temp.b=p.b+b;
return temp;
}};

In Lab Tasks
Q 1. Write a program that overloads postfix increment operator to work with user-defined objects.
Q 2. Write a program that overloads arithmetic addition operator + for concatenating two string
values.
Q 3. Write a program that overloads addition operator to add two complex numbers
Q 4. Write a program that overloads the comparison operators == to work with String class. The
result of comparison must be 1 if two strings are of same length and 0 otherwise.

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.
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.

You might also like