0% found this document useful (0 votes)
5 views10 pages

Operator Overloading

The document outlines four programming problems involving operator overloading in C++. The first problem requires implementing a Fraction class to add and simplify fractions, the second involves calculating final velocity using an Acceleration class, the third focuses on a Time class to increment time by one minute, and the fourth is about managing student records with a Student class that tracks weight. Each problem includes input and output specifications along with sample inputs and outputs.

Uploaded by

Anup Doddamani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views10 pages

Operator Overloading

The document outlines four programming problems involving operator overloading in C++. The first problem requires implementing a Fraction class to add and simplify fractions, the second involves calculating final velocity using an Acceleration class, the third focuses on a Time class to increment time by one minute, and the fourth is about managing student records with a Student class that tracks weight. Each problem includes input and output specifications along with sample inputs and outputs.

Uploaded by

Anup Doddamani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Problem Statement

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.

Function Specifications: Fraction operator+(const Fraction& other) const

Input Format

The input consists of two lines.

Each line contains two integers separated by a space.

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.

Sample Input Sample Output

12

34

5/4

Sample Input Sample Output

56

13

7/6

Time Limit: - ms Memory Limit: - kb Code Size: - kb

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

The input consists of three space-separated float values:

1. Initial velocity (in m/s)

2. Acceleration (in m/s2)

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.

Refer to the sample output for formatting specifications.

Constraints

In this scenario, the test cases fall under the following constraints:

1.0 ≤ Initial velocity ≤ 500.0

1.0 ≤ Acceleration ≤ 50.0

1.0 ≤ Time ≤ 50.0

Sample Input Sample Output

10.3 5.0 2.1

20.8 m/s

Sample Input Sample Output

410.2 9.8 4.5

454.3 m/s

Time Limit: - ms Memory Limit: - kb Code Size: - kb

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.

Note: The time is represented in 24-hour format.

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.

The time is printed in the format: [hours]h [minutes]m.

Refer to the sample output for formatting specifications.

Constraints

0 ≤ h ≤ 23

0 ≤ m ≤ 59

Sample Input Sample Output

11 57

11h 57m

11h 58m

Sample Input Sample Output

13 5

13h 5m

13h 6m

Sample Input Sample Output

23 59

23h 59m

0h 0m

Sample Input Sample Output

0 59

0h 59m

1h 0m

Time Limit: - ms Memory Limit: - kb Code Size: - kb

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.

Assist her in completing the program.

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.

Round off the weights to two decimal places.

Refer to the sample output for formatting specifications.

Constraints

In this scenario, the test cases fall under the following constraints:

1 ≤ Student ID ≤ 1000

1.0 ≤ initial Weight ≤ 100.0

Sample Input Sample Output

101

34.8

101 34.80

101 35.80

Sample Input Sample Output

103

78.987

103 78.99

103 79.99
Q1

#include <iostream>

class Fraction {

private:

int numerator;

int denominator;

public:

Fraction(int num = 0, int denom = 1) : numerator(num), denominator(denom) {}

Fraction operator+(const Fraction& other) const {

int num = numerator * other.denominator + other.numerator * denominator;

int denom = denominator * other.denominator;

return Fraction(num, denom);

Fraction simplify() const {

int gcd = computeGCD(numerator, denominator);

return Fraction(numerator / gcd, denominator / gcd);

int computeGCD(int a, int b) const {

if (b == 0)

return a;

return computeGCD(b, a % b);

friend std::ostream& operator<<(std::ostream& os, const Fraction& fraction) {

os << fraction.numerator << "/" << fraction.denominator;


return os;

};

int main() {

int num1, denom1, num2, denom2;

std::cin >> num1 >> denom1;

std::cin >> num2 >> denom2;

Fraction f1(num1, denom1);

Fraction f2(num2, denom2);

Fraction sum = f1 + f2;

Fraction simplifiedSum = sum.simplify();

std::cout << simplifiedSum << std::endl;

return 0;

Q2

#include <iostream>

#include <iomanip>

using namespace std;

class Acceleration {

public:

float acceleration;

Acceleration(float val) {

acceleration = val;
}

Acceleration operator*(float t) {

Acceleration result(acceleration * t);

return result;

};

int main() {

float iv, av, t;

cin >> iv >> av >> t;

Acceleration obj(av);

Acceleration m = obj * t;

float a = m.acceleration;

float fv = iv + a;

cout << fixed << setprecision(1) << fv << " m/s";

return 0;

Q3

#include <iostream>

using namespace std;

class Time {

public:

int hours;

int minutes;

Time(int h, int m){


hours = h;

minutes = m;

void display() {

cout << hours << "h " << minutes << "m";

Time operator++() {

minutes++;

if (minutes == 60) {

minutes = 0;

hours++;

if (hours == 24) {

hours = 0;

return Time(hours, minutes);

};

int main() {

int hr, min;

cin >> hr >> min;

Time t(hr, min);

t.display();

cout << endl;


t = ++t;

t.display();

return 0;

Q4

#include <iostream>

#include <iomanip>

using namespace std;

class Student {

private:

int studentID;

double weight;

public:

Student(int id, double w) {

studentID = id;

weight = w;

void displayStudent() {

cout << studentID << " " << fixed << setprecision(2) << weight << endl;

friend void operator++(Student& student);

};

void operator++(Student& student) {

student.weight++;

}
int main() {

int studentID;

double initialWeight;

cin >> studentID;

cin >> initialWeight;

Student student(studentID, initialWeight);

student.displayStudent();

++student;

student.displayStudent();

return 0;

You might also like