-
Notifications
You must be signed in to change notification settings - Fork 549
/
Copy path14_06.cpp
59 lines (50 loc) · 1.16 KB
/
14_06.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
#include <bits/stdc++.h>
using namespace std;
class MyPair {
private:
int first, second;
public:
MyPair(int first, int second) :
first(first), second(second) {
}
MyPair Add(const MyPair &c2) {
MyPair &c1 = *this;
return MyPair(c1.GetFirst() + c2.GetFirst(), c1.GetSecond() + c2.GetSecond());
}
void print() {
cout << "(" << first << "," << second << ")\n";
}
int GetFirst() const {
return first;
}
void SetFirst(int first) {
this->first = first;
}
int GetSecond() const {
return second;
}
void SetSecond(int second) {
this->second = second;
}
MyPair operator !() {
cout<<"Here\n";
MyPair &c1 = *this;
return MyPair(-1 * c1.GetFirst(),
-1 * c1.GetSecond());
}
int operator[]( int pos) const {
if (pos == 0)
return GetFirst();
return GetSecond();
}
};
int main() {
MyPair x(10, 20);
cout<<x[0]<<" "<<x[1]<<"\n";
// error: lvalue required as left operand of assignment
// x[0] = 7;
// x[0] => just a function that return temp var, e.g. g
// so u r doing g = 7 this has nothing with object x
// we need g points in memory to first when [0]
return 0;
}