-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOperatorOverload.h
59 lines (49 loc) · 1.04 KB
/
OperatorOverload.h
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
#include <iostream>
/*
https://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B
*/
template<typename T>
class R
{
private:
T _data;
public:
T getData() {return _data;}
void setData(const T& t) { _data = t;}
/*
Arithmetic operators
*/
// a = b
virtual R& operator=(R r) = 0;
// a + b
virtual R operator+(R r) = 0;
// a - b
virtual R operator-(R r) = 0;
//-a
virtual R operator-() = 0;
//-b
virtual R operator+() = 0;
virtual R operator*(R r) = 0;
virtual R operator%(R r) = 0;
virtual R operator/(R r) = 0;
//++a
virtual R& operator++() = 0;
//a++
virtual R operator++(int) = 0;
//--a
virtual R& operator--() = 0;
//++a
virtual R operator--(int) = 0;
/*
implements >> and <<, why? becase: implements outsdie class will be error when compile
https://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Making_New_Friends
*/
friend std::ostream& operator<<(std::ostream &os, R<T> &r) {
os << r._data;
return os;
}
friend std::istream& operator>>(std::istream &is, R<T> &r) {
is >> r._data;
return is;
}
};