Skip to content

Commit 69ecc9a

Browse files
author
XuhuaHuang
committed
Refactor operator overload examples
1 parent c35abe3 commit 69ecc9a

4 files changed

Lines changed: 94 additions & 55 deletions

File tree

OperatorOverload/assignment.cpp

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
*********************************************************************/
99
// clang-format on
1010

11-
#include <iostream>
12-
1311
/**
1412
* @class point
1513
* @brief A class representing a point in 2D space.
@@ -21,26 +19,26 @@
2119
*/
2220
class point {
2321
public:
24-
constexpr point() = default;
25-
constexpr point(const point& rhs) = default;
26-
virtual ~point() = default;
22+
point() = default;
23+
constexpr point(const point& rhs) = default;
24+
virtual ~point() = default;
2725

28-
int _x;
29-
int _y;
26+
int _x;
27+
int _y;
3028

31-
point& operator=(const point&);
29+
point& operator=(const point&);
3230
};
3331

3432
point& point::operator=(const point& otherPoint) {
35-
_x = otherPoint._x;
36-
_y = otherPoint._y;
33+
_x = otherPoint._x;
34+
_y = otherPoint._y;
3735

38-
return *this;
36+
return *this;
3937
}
4038

4139
int main(void) {
42-
point ptr1, ptr2;
43-
ptr1 = ptr2;
40+
point p1, p2;
41+
p1 = p2;
4442

45-
return 0;
43+
return 0;
4644
}

OperatorOverload/complex.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <iostream>
2+
3+
class complex_t {
4+
private:
5+
double real{0.0};
6+
double imag{0.0};
7+
8+
public:
9+
complex_t() = default;
10+
11+
complex_t(double re, double im) noexcept
12+
: real(re)
13+
, imag(im) {}
14+
15+
[[nodiscard]]
16+
inline double get_real() const noexcept {
17+
return real;
18+
}
19+
[[nodiscard]]
20+
inline double get_imag() const noexcept {
21+
return imag;
22+
}
23+
24+
[[nodiscard]]
25+
inline complex_t operator+(const complex_t& other) const noexcept {
26+
return complex_t(real + other.real, imag + other.imag);
27+
}
28+
};
29+
30+
int main() {
31+
complex_t c1(1.0, 1.0);
32+
complex_t c2(2.0, 2.0);
33+
34+
complex_t c3 = c1 + c2;
35+
36+
std::cout << "Result: (" << c3.get_real() << ", " << c3.get_imag() << ")\n";
37+
38+
return 0;
39+
}

OperatorOverload/conversion.cpp

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// clang-format off
12
/*****************************************************************//**
23
* \file conversion.cpp
34
* \brief Overloaded conversion operator float()
@@ -7,36 +8,37 @@
78
* \author Xuhua Huang
89
* \date November 2020
910
*********************************************************************/
11+
// clang-format on
1012

1113
#include <iostream>
1214

1315
class Fraction {
1416
private:
15-
int num;
16-
int den;
17+
int num;
18+
int den;
1719

1820
public:
19-
constexpr Fraction() = default;
21+
inline Fraction() = default;
2022

21-
constexpr Fraction(int n, int d)
22-
: num(n)
23-
, den(d) {}
23+
constexpr Fraction(int n, int d)
24+
: num(n)
25+
, den(d) {}
2426

25-
constexpr Fraction(const Fraction& rhs) = default;
26-
virtual ~Fraction() = default;
27+
constexpr Fraction(const Fraction& rhs) = default;
28+
virtual ~Fraction() = default;
2729

28-
constexpr const int getnum() const { return this->num; }
29-
constexpr const int getden() const { return this->den; }
30+
constexpr int getnum() const { return this->num; }
31+
constexpr int getden() const { return this->den; }
3032

31-
// conversion operator: return float value of fraction
32-
constexpr operator float() const { return float(getnum()) / float(getden()); }
33+
// conversion operator: return float value of fraction
34+
constexpr operator float() const { return float(getnum()) / float(getden()); }
3335
};
3436

3537
int main(void) {
36-
Fraction f(2, 5); // numerator = 2, denominator = 5
38+
Fraction f(2, 5); // numerator = 2, denominator = 5
3739

38-
float val = f; // float(val) is overloaded
39-
std::cout << val;
40+
float val = f; // float(val) is overloaded
41+
std::cout << val;
4042

41-
return EXIT_SUCCESS;
43+
return 0;
4244
}

OperatorOverload/parenthesis_fn_obj.cpp

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,38 +13,38 @@
1313

1414
class Compare {
1515
public:
16-
constexpr inline Compare() noexcept = default;
17-
constexpr inline Compare(const Compare& rhs) noexcept = default;
18-
inline virtual constexpr ~Compare() noexcept = default;
16+
constexpr inline Compare() noexcept = default;
17+
constexpr inline Compare(const Compare& rhs) noexcept = default;
18+
inline virtual ~Compare() noexcept = default;
1919

20-
// overloading the "()" operator to compare two int
21-
// return either 'true/1' or 'false/0'
22-
inline const std::string operator()(int a, int b) const {
23-
std::string result = "false";
24-
if (a < b) {
25-
result = "true";
26-
}
27-
return result;
20+
// overloading the "()" operator to compare two int
21+
// return either 'true/1' or 'false/0'
22+
inline const std::string operator()(int a, int b) const {
23+
std::string result = "false";
24+
if (a < b) {
25+
result = "true";
2826
}
27+
return result;
28+
}
2929
};
3030

3131
int main(void) {
32-
Compare compare; // create a function object
33-
int a = 5;
34-
int b = 7;
35-
std::string ans = compare(a, b);
36-
// using the overloaded () operator
37-
// of the "Compare" class
32+
Compare compare; // create a function object
33+
int a = 5;
34+
int b = 7;
35+
std::string ans = compare(a, b);
36+
// using the overloaded () operator
37+
// of the "Compare" class
3838

39-
std::cout << ans << "\n";
39+
std::cout << ans << "\n";
4040

41-
// fun with nested for loops
42-
for (int i = 0; i < 100; i++) {
43-
for (int j = 0; j < 100; j++) {
44-
std::string ans = compare(i, j);
45-
std::cout << "\nFor " << i << " < " << j << " is " << ans << "\n";
46-
}
41+
// fun with nested for loops
42+
for (int i = 0; i < 100; i++) {
43+
for (int j = 0; j < 100; j++) {
44+
std::string ans = compare(i, j);
45+
std::cout << "\nFor " << i << " < " << j << " is " << ans << "\n";
4746
}
47+
}
4848

49-
return 0;
49+
return 0;
5050
}

0 commit comments

Comments
 (0)