-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathrational-4.cpp
More file actions
123 lines (101 loc) · 3.07 KB
/
Copy pathrational-4.cpp
File metadata and controls
123 lines (101 loc) · 3.07 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
/**
* A simple Rational number class. It prints messages to show which constructor (ctor) is being called.
* This version have move constructor, showing its use using std::move.
* we have implemented "copy and swap algorithm" for assignment operator,
* So there is one common function for assignment and move assignment operator.
*/
#include <cstdio>
#include <string>
void message( const char * msg ) {
puts(msg);
fflush(stdout);
}
class Rational {
int _n;
int _d;
static const int _bufsize = 128;
mutable char * _buf = nullptr;
public:
Rational() { reset(); message("default constructor"); }
Rational( int n ) : _n{ n }, _d{ 1 } { message("int constructor"); }
Rational( int n, int d ) : _n{ n }, _d{ d } { message("int, int constructor"); }
Rational( const Rational & other )
: _n{ other._n }, _d{ other._d } { message("copy constructor"); }
Rational( Rational && );
~Rational();
void reset();
inline int numerator() const { return _n; }
inline int denominator() const { return _d; }
void swap( Rational & b );
Rational & operator = ( const Rational );
Rational operator + ( const Rational & ) const;
Rational operator - ( const Rational & ) const;
Rational operator * ( const Rational & ) const;
Rational operator / ( const Rational & ) const;
operator std::string() const;
std::string string() const;
const char * c_str() const;
};
Rational::Rational( Rational && other ) {
_n = std::move( other.numerator() );
_d = std::move( other.denominator() );
other.reset();
message("move constructor");
}
Rational::~Rational() {
reset();
message("destructor");
}
void Rational::reset() {
_n = 0;
_d = 1;
delete[] _buf;
_buf = nullptr;
}
void Rational::swap( Rational & other ) {
std::swap( _n, other._n );
std::swap( _d, other._d );
}
Rational & Rational::operator = ( Rational rhs ) {
message("copy and swap");
swap(rhs);
return *this;
}
Rational Rational::operator + ( const Rational & rhs ) const {
return Rational( (_n * rhs._d) + (_d * rhs._n), _d * rhs._d );
}
Rational Rational::operator - ( const Rational & rhs ) const {
return Rational( (_n * rhs._d) - (_d * rhs._n), _d * rhs._d );
}
Rational Rational::operator * ( const Rational & rhs ) const {
return Rational( _n * rhs._n, _d * rhs._d );
}
Rational Rational::operator / ( const Rational & rhs ) const {
return Rational( _n * rhs._d, _d * rhs._n );
}
Rational::operator std::string() const {
return string();
}
std::string Rational::string() const {
return std::string{ c_str() };
}
const char * Rational::c_str() const {
if (!_buf) {
_buf = new char[_bufsize];
}
snprintf(_buf, _bufsize, "%d/%d", _n, _d);
return _buf;
}
int main( int argc, char ** argv ) {
Rational a = 7; // 7/1
Rational b(5, 3); // 5/3
Rational c = b; // copy ctor
Rational d = std::move(c);
d = b;
printf("a is: %s\n", a.c_str());
printf("b is: %s\n", b.c_str());
printf("c is: %s\n", c.c_str());
printf("d is: %s\n", d.c_str());
printf("%s + %s is %s\n", a.c_str(), b.c_str(), Rational(a + b).c_str());
return 0;
}