-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathweek3-app1.cpp
77 lines (57 loc) · 1.6 KB
/
week3-app1.cpp
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
#include <iostream>
// function / operator overloading
// const, east/west const, const correctness
// OOP:
// ctor/dtor
// copy-ctor, copy-assignment
// move-ctor, move-assignment
// inheritance
// user-defined literals
// almost always auto - aaa rule: move type to the right
// unified/universal initialization
double get_pi() { return 3.14152; }
double get_pi(const int precision)
{
if(precision == 1)
return 3.1;
if(precision == 2)
return 3.14;
return 3.14152;
}
struct Float
{
float value;
//...
};
class Int
{
int value; // this attrib. is private, it's none of the outside caller's business to touch it
public:
Int(int value) : value(value) { }
// auto return type below is available in C++14 onwards
auto operator+(const Int other) { return Int(value + other.value); }
int get_value() const // touch the internals through here
{
return this->value;
}
// Int operator+(Float other) { /*...*/ }
};
//Int add(Int v1, Int v2) { return Int(v1.value + v2.value); }
//Int operator+(Int v1, Int v2) { return Int(v1.value + v2.value); }
int main(int argc, char* argv[])
{
const auto p = 3.14;
auto pi = get_pi();
auto pi_1 = get_pi(1);
auto a = Int(10);
auto b = Int(20);
// auto c = add(a, b);
auto c = a + b;
// auto c = a.operator+(b);
// we need free function version of op-overloading
// because left object and even right thing do not belong to us
// we can't change their internals
// "Hello" + std::string("World");
std::cout << c.get_value() << std::endl;
return 0;
}