-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass_creation_skills.cpp
More file actions
71 lines (56 loc) · 1.11 KB
/
class_creation_skills.cpp
File metadata and controls
71 lines (56 loc) · 1.11 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
/*
6 kyu
Train your skills in creation of classes
https://www.codewars.com/kata/5ab4f002379d20e82500008c
*/
#include <iostream>
#include <sstream>
#include <string>
class X {
int m_a = 1;
int m_b = 2;
public:
// Constructors
X() = default;
explicit X(int a);
X(int a, int b);
// Arithmetic operators
X& operator+=(const X& x);
X operator+(const X& x) const;
// Increment operators
X& operator++(); // prefix
X operator++(int); // postfix
// String output
std::string print() const;
};
std::ostream& operator<<(std::ostream& os, const X& x);
X::X(int a) : m_a(a) {}
X::X(int a, int b) : m_a(a), m_b(b) {}
X& X::operator+=(const X& x) {
m_a += x.m_a;
m_b += x.m_b;
return *this;
}
X X::operator+(const X& x) const {
X tmp = *this;
tmp += x;
return tmp;
}
X& X::operator++() {
++m_a;
++m_b;
return *this;
}
X X::operator++(int) {
X tmp = *this;
++(*this);
return tmp;
}
std::string X::print() const {
std::ostringstream s;
s << "[" << m_a << "," << m_b << "]";
return s.str();
}
std::ostream& operator<<(std::ostream& os, const X& x) {
return os << x.print();
}