Operator Overloading
Operator Overloading
Implement a Fraction class that represents a fraction with a numerator and a denominator. Overload
the '+' operator to add two fractions and return the result as a simplified fraction.
Input Format
The first line represents the numerator and denominator of the first fraction.
The second line represents the numerator and denominator of the second fraction.
Output Format
The output displays the sum of the fractions of the given input as simplified values.
12
34
5/4
56
13
7/6
Q2.
Problem Statement
John is driving a car with an initial velocity (in m/s) that suddenly accelerates at a constant rate (in
m/s2) for a certain time (in seconds). He wants to write a program that calculates and displays the
final velocity of the car.
Help John calculate the final velocity by overloading the * operator in the Acceleration class.
Formula: Final velocity = Initial velocity + (Acceleration * time)
Input Format
3. Time (in s)
Output Format
The output displays a float value representing the final velocity followed by " m/s", rounded off to
one decimal place.
Constraints
In this scenario, the test cases fall under the following constraints:
20.8 m/s
454.3 m/s
Q3.
Problem Statement
Create a class Time to represent the departure time of a train, initialized with hours and minutes.
Overload the pre-increment operator ++ to advance the departure time by one minute. Display the
original and updated departure times.
Input Format
The input consists of two space-separated integers, hours (h) and minutes (m) representing the
departure time of the train.
Output Format
The first line of output prints the original departure time and the second line prints the updated
departure time.
Constraints
0 ≤ h ≤ 23
0 ≤ m ≤ 59
11 57
11h 57m
11h 58m
13 5
13h 5m
13h 6m
23 59
23h 59m
0h 0m
0 59
0h 59m
1h 0m
Q4.
Problem Statement
Maria is developing a program to manage student records, with a specific focus on weight tracking.
She has created a class named Student that represents student information and allows for the
incrementing (++) of a student's weight by 1 kg through operator overloading with the friend
function named operator.
Input Format
The first line of input consists of an integer, representing the student's ID.
The second line consists of a double-point value, representing the initial weight of the student.
Output Format
The first line of output prints the student ID and initial weight of the student.
The second line prints the student ID and the weight of the student after incrementing.
Constraints
In this scenario, the test cases fall under the following constraints:
1 ≤ Student ID ≤ 1000
101
34.8
101 34.80
101 35.80
103
78.987
103 78.99
103 79.99
Q1
#include <iostream>
class Fraction {
private:
int numerator;
int denominator;
public:
if (b == 0)
return a;
};
int main() {
return 0;
Q2
#include <iostream>
#include <iomanip>
class Acceleration {
public:
float acceleration;
Acceleration(float val) {
acceleration = val;
}
Acceleration operator*(float t) {
return result;
};
int main() {
Acceleration obj(av);
Acceleration m = obj * t;
float a = m.acceleration;
float fv = iv + a;
return 0;
Q3
#include <iostream>
class Time {
public:
int hours;
int minutes;
minutes = m;
void display() {
cout << hours << "h " << minutes << "m";
Time operator++() {
minutes++;
if (minutes == 60) {
minutes = 0;
hours++;
if (hours == 24) {
hours = 0;
};
int main() {
t.display();
t.display();
return 0;
Q4
#include <iostream>
#include <iomanip>
class Student {
private:
int studentID;
double weight;
public:
studentID = id;
weight = w;
void displayStudent() {
cout << studentID << " " << fixed << setprecision(2) << weight << endl;
};
student.weight++;
}
int main() {
int studentID;
double initialWeight;
student.displayStudent();
++student;
student.displayStudent();
return 0;