-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperator_overloading_new.cpp
More file actions
171 lines (142 loc) · 4.99 KB
/
operator_overloading_new.cpp
File metadata and controls
171 lines (142 loc) · 4.99 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <iostream>
#include <cassert>
#include <cmath>
#include <numeric> // std::gcd() for reduce the fractions to the lowest terms
#include <utility>
class Fraction
{
private:
int m_numerator{};
int m_denominator{};
void reduce() {
const int f_gcd{std::gcd(m_numerator, m_denominator)};
if(f_gcd) // make sure no divide by 0
{
m_numerator /= f_gcd;
m_denominator /= f_gcd;
}
}
public:
explicit Fraction(const int numerator = 0, const int denominator = 1) // we set default parameters here for default initialization
: m_numerator{numerator}, m_denominator{denominator}
{
assert(m_denominator != 0); // make sure all the fraction which is math semantics correct
reduce(); // and to the lowest term
}
void print() const {
if(m_denominator != 1 && m_numerator != 0)
std::cout << m_numerator << "/" << m_denominator << '\n';
else
std::cout << m_numerator << '\n';
}
friend Fraction operator*(const Fraction &f1, const Fraction &f2);
friend Fraction operator*(const int value, const Fraction &f2); // if we remove two of these (This line and the below one And `explicit` ahead of the constructor)
friend Fraction operator*(const Fraction &f1, const int value); // the program will still work
friend std::istream &operator>>(std::istream &in, Fraction &fraction);
friend std::ostream &operator<<(std::ostream &out, const Fraction &fraction);
};
Fraction operator*(const Fraction &f1, const Fraction &f2)
{
return Fraction{f1.m_numerator * f2.m_numerator, f1.m_denominator * f2.m_denominator};
}
Fraction operator*(const int value, const Fraction &f2)
{
return Fraction{value * f2.m_numerator, f2.m_denominator};
}
Fraction operator*(const Fraction &f1, const int value)
{
return value * f1; // We delegate this to another operator function.
}
int gcd(int a, int b) // the greatest common divisor (GCD) (Traditional)
{
return (b == 0) ? std::abs(a) : gcd(b, a % b);
}
std::istream &operator>>(std::istream &in, Fraction &fraction)
{
int numerator{};
char divide{};
int denominator{};
if(in>>numerator>>divide>>denominator && denominator != 0)
fraction = Fraction{numerator, denominator};
else
{
in.setstate(std::ios_base::failbit);
fraction = Fraction{1, 1};
}
return in;
}
std::ostream &operator<<(std::ostream &out, const Fraction &fraction)
{
if(fraction.m_denominator != 1 && fraction.m_numerator != 0)
return out << fraction.m_numerator << '/' << fraction.m_denominator;
else
return out << fraction.m_numerator;
}
class Point
{
private:
double m_x {};
double m_y {};
double m_z {};
public:
Point(double x=0.0, double y=0.0, double z=0.0):
m_x{x}, m_y{y}, m_z{z}
{
}
// Convert a Point into its negative equivalent
Point operator- () const;
Point operator+ () const;
// Return true if the point is set at the origin
bool operator! () const;
double getX() const { return m_x; }
double getY() const { return m_y; }
double getZ() const { return m_z; }
};
bool Point::operator!() const
{ // if(!value) --> if(value <= 0) (non-positive Int)
return (m_x == 0 && m_y == 0 && m_z == 0); // if(!point) --> if(point.m_x == 0 && ..m_y == 0 && ..m_z == 0)
}
// Point Point::operator+() const
// {
// return {m_x, m_y, m_z};
// }
Point Point::operator+() const
{
return *this; // we just do nothing, return the implicit object
}
Point Point::operator-() const
{
return {-m_x, -m_y, -m_z};
}
int main()
{
Fraction f1{1, 4};
f1.print();
Fraction f2{2, 3};
Fraction f3{f1 * f2};
f3.print();
Fraction f4{2 * f1};
Fraction f5{f2 * 3};
f4.print();
f5.print();
Fraction f6{ Fraction{1, 2} * Fraction{2, 3} * Fraction{3, 4} }; // if we remove `const` on the first operator* overloading, this statement will not work
// because non-const reference can not bind to a temporary object
Fraction f7{0, 6};
f7.print();
Fraction f11{};
std::cout << "Enter fraction 1: ";
std::cin >> f11;
Fraction f12{};
std::cout << "Enter fraction 2: ";
std::cin >> f12;
std::cout << f11 << " * " << f12 << " is " << f11 * f12 << '\n'; // note: The result of f1 * f2 is an r-value
Point point{}; // use default constructor to set to (0.0, 0.0, 0.0)
if (!point)
std::cout << "point is set at the origin.\n";
else
std::cout << "point is not set at the origin.\n";
point = {1, 2, -3};
std::cout << "Point:(" << -point.getX() << ',' << -point.getY() << ',' << -point.getZ() << ")\n";
std::cout << "Point:(" << +point.getX() << ',' << +point.getY() << ',' << +point.getZ() << ")\n";
return 0;
}