-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab8Q1.cpp
More file actions
41 lines (30 loc) · 892 Bytes
/
Copy pathLab8Q1.cpp
File metadata and controls
41 lines (30 loc) · 892 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
class Fraction {
private:
int numerator;
int denominator;
public:
Fraction(int num, int denom) : numerator(num), denominator(denom) {
if (denominator == 0) {
std::cerr << "Error: Denominator cannot be zero." << std::endl;
exit(1);
}
}
Fraction operator+(const Fraction& other) const {
int newNumerator = (numerator * other.denominator) + (other.numerator * denominator);
int newDenominator = denominator * other.denominator;
return Fraction(newNumerator, newDenominator);
}
void print() const {
std::cout << numerator << "/" << denominator;
}
};
int main() {
Fraction fraction1(1, 2);
Fraction fraction2(1, 4);
Fraction result = fraction1 + fraction2;
std::cout << "Result: ";
result.print();
std::cout << std::endl;
return 0;
}