Lab Report 07
Lab Report 07
4 Operator
Overloading
4.1 Objectives of the lab:
Introducing the concepts of
operator overloading and
overloading different operators
such as
14 Arithmetic operators
15 Relational operators
16 Logical operators
17 Unary operators
Lab No.4 Operator
Overloading
4.1 Objectives of the lab:
Introducing the concepts of
operator overloading and
overloading different operators
such as
14 Arithmetic operators
15 Relational operators
16 Logical operators
17 Unary operators
Lab No.4 Operator
Overloading
4.1 Objectives of the lab:
Introducing the concepts of
operator overloading and
overloading different operators
such as
14 Arithmetic operators
15 Relational operators
16 Logical operators
17 Unary operLa
DEPARTMENT OF COMPUTER & SOFTWARE
ENGINEERING
COLLEGE OF E&ME, NUST, RAWALPINDI
Total 10
Lab Tasks:
1. Create a C++ class called Time to represent time values, with attributes for hours and
minutes only. Overload the (+) operator within the (Time) class to add two (Time) objects.
This operator should add the hours and minutes separately. In the main function:
Create two Time objects, (time1) and (time2).
Input the values of hours and minutes for both (time1) and (time2).
Use the overloaded (+) operator to add (time1) and (time2) together and create a new
(Time) object called (resultTime).
Display the values of (resultTime) to show the addition of the times.
UML Diagram:
Source Code:
#include <iostream>
using namespace std;
class Time {
private:
int hours;
int minutes;
public:
// Constructor
Time(int h = 0, int m = 0) {
sethours(h);
setminutes(m);
}
// Setters
void sethours(int h) {
hours = h;
}
void setminutes(int m) {
minutes = m;
}
// Getters
int gethours() const {
return hours;
}
int main() {
Time time1, time2;
// Displaying result
cout << "Resulting time after addition: " << resultTime << endl;
return 0;
}
Output:
2. Create a C++ class called `Int` to represent a custom integer data type. Overload the `+`
operator to add two `Int` objects. If the result exceeds the normal range of integers, print a
warning and terminate the program. Overload the `-` operator to subtract one `Int` object
from another. Handle overflow similarly. Overload the `*` operator to multiply two `Int`
objects. Handle overflow as well. Overload the `/` operator to divide one `Int` object by
another. Print a warning and terminate the program if division by zero occurs. Implement the
class `Int` to handle overflow by checking the result of arithmetic operations and comparing
it to the valid range for integers. In the `main` function, create `Int` objects and perform
various arithmetic operations, testing the overloads for correctness and handling overflow.
UML Diagram:
Source Code:
#include <iostream>
#include <limits>
#include <cstdlib>
class Int {
private:
int num;
public:
Int(int num = 0) {
this->num = num;
}
Int(const Int& h) {
this->num = h.num;
}
~Int() {}
int main() {
// Test addition within range
Int obj1(100);
Int obj2(200);
Int sum = obj1 + obj2;
cout << "Sum within range: "; sum.Display();
return 0;
}
Output:
3. Create a C++ class called Polynomial that uses a dynamic array to store coefficients, with
each index representing the power of the variable. Implement a constructor for initializing the
polynomial and a display() method for printing it in a readable format. Overload the +, -, and
* operators for adding, subtracting, and multiplying polynomial objects, ensuring results are
simplified by removing any zero-coefficient terms. In the main() function, create polynomial
objects, perform arithmetic operations, and display the results.
UML Diagram:
Source Code:
#include <iostream>
#include <cmath>
#include <vector>
class Polynomial {
private:
vector<int> coeff; // Vector to store coefficients
vector<int> degree; // Vector to store degrees
public:
// Default and Parameterized Constructor
Polynomial(int inputs = 3, const int* coeffi = nullptr, const int*
degre = nullptr) {
if (inputs < 3) {
inputs = 3;
}
if (coeffi != nullptr && degre != nullptr) {
for (int i = 0; i < inputs; ++i) {
coeff.push_back(coeffi[i]);
degree.push_back(degre[i]);
}
}
else {
for (int i = 0; i < inputs; ++i) {
degree.push_back(i);
coeff.push_back(1); // Default coefficients
}
}
}
int x = 2;
cout << "Evaluating Polynomial 1 at x = " << x << ": " <<
poly1.evaluate(x) << endl; // Evaluating polynomial 1 at x
return 0;
}
Output:
complex(double r, double
i):re(r), im(i)
{}
void show()
{
cout<<"complex number:
"<<re<<"+"<<im<<"i"<<endl;
}
complex operator +
(complex rhs)
{
complex temp;
temp.re=re + rhs.re;
temp.im=im + rhs.im;
return temp;
}
};
void main()
{
complex c1(3, 4.3), c2(2, 4);
c1.show();
c2.show();
complex c3;
c3=c1+ c2; //Invocation
of "+" Operator -- direct
//or
//c3=c1.operator+ (c2);
//Invocation of "+" Operator --
Function
c3.show();
}
Lab No.4 Operator
Overloading
4.1 Objectives of the lab:
Introducing the concepts of
operator overloading and
overloading different operators
such as
14 Arithmetic operators
15 Relational operators
16 Logical operators
17 Unary operators