-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweek06.cpp
More file actions
80 lines (66 loc) · 1.51 KB
/
week06.cpp
File metadata and controls
80 lines (66 loc) · 1.51 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
// 예제 5-2 : 객체 리턴
#include <iostream>
using namespace std;
class Circle {
int radius;
public:
Circle() { radius = 1; }
Circle(int radius) { this->radius = radius; }
void setRadius(int radius) { this->radius = radius; }
double getArea() { return 3.14*radius*radius; }
};
Circle getCircle() {
Circle tmp(30);
return tmp;
}
int main() {
Circle c;
cout << c.getArea() << endl;
c = getCircle();
cout << c.getArea() << endl;
}
// 실행 결과
3.14
2826
// 예제 5-3 : 기본 타입 변수에 대한 참조
#include <iostream>
using namespace std;
int main() {
cout << "i" << '\t' << "n" << '\t' << "refn" << endl;
int i = 1;
int n = 2;
int &refn = n;
n = 4;
refn++;
cout << i << '\t' << n << '\t' << refn << endl;
refn = i;
refn++;
cout << i << '\t' << n << '\t' << refn << endl;
int *p = &refn;
*p = 20;
cout << i << '\t' << n << '\t' << refn << endl;
}
// 실행 결과
i n refn
1 5 5
1 2 2
1 20 20
// 예제 5-4 : 객체에 대한 참조
#include <iostream>
using namespace std;
class Circle {
int radius;
public:
Circle() { radius = 1; }
Circle(int radius) { this->radius = radius; }
void setRadius(int radius) { this->radius = radius; }
double getArea() { return 3.14*radius*radius; }
};
int main() {
Circle circle;
Circle &refc = circle;
refc.setRadius(10);
cout << refc.getArea() << " " << circle.getArea();
}
// 실행 결과
314 314