Skip to content

Commit df345d2

Browse files
committedFeb 27, 2022
[명품 C++ 프로그래밍] 03장 클래스와 객체 - 예제
1 parent f99d492 commit df345d2

File tree

6 files changed

+171
-0
lines changed

6 files changed

+171
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
class Circle {
5+
public:
6+
int radius;
7+
double getArea();
8+
};
9+
10+
double Circle::getArea() {
11+
return 3.14*radius*radius;
12+
}
13+
14+
int main()
15+
{
16+
Circle donut; // 클래스 크기의 메모리가 할당
17+
donut.radius = 1;
18+
double area = donut.getArea();
19+
cout << "donut의 면적은 " << area << endl;
20+
21+
Circle pizza;
22+
pizza.radius = 30;
23+
area = pizza.getArea();
24+
cout << "pizza 면적은 " << area << endl;
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
class Rectangle {
5+
public:
6+
int width;
7+
int height;
8+
9+
int getArea();
10+
};
11+
12+
int Rectangle::getArea() {
13+
return width * height;
14+
}
15+
16+
17+
int main() {
18+
Rectangle rect;
19+
rect.width = 3;
20+
rect.height = 5;
21+
cout << "사각형의 면적은 " << rect.getArea() << endl;
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
class Circle {
5+
public:
6+
int radius;
7+
Circle();
8+
Circle(int r);
9+
double getArea();
10+
};
11+
12+
Circle::Circle() {
13+
radius = 1;
14+
cout << "반지름 " << radius << " 원 생성" << endl;
15+
}
16+
17+
Circle::Circle(int r) {
18+
radius = r;
19+
cout << "반지름 " << radius << " 원 생성" << endl;
20+
}
21+
22+
double Circle::getArea() {
23+
return 3.14*radius*radius;
24+
}
25+
26+
int main() {
27+
Circle donut;
28+
double area = donut.getArea();
29+
cout << "donut의 면적은 " << area << endl;
30+
31+
Circle pizza(30);
32+
area = pizza.getArea();
33+
cout << "pizza의 면적은 " << area << endl;
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
class Circle {
5+
public:
6+
int radius;
7+
Circle();
8+
Circle(int r);
9+
double getArea();
10+
};
11+
12+
/* 위임 생성자 */
13+
Circle::Circle() : Circle(1) { }
14+
15+
/* 타겟 생성자 */
16+
Circle::Circle(int r) {
17+
radius = r;
18+
cout << "반지름 " << radius << " 원 생성" << endl;
19+
}
20+
21+
double Circle::getArea() {
22+
return 3.14*radius*radius;
23+
}
24+
25+
int main() {
26+
Circle donut;
27+
double area = donut.getArea();
28+
cout << "donut의 면적은 " << area << endl;
29+
30+
Circle pizza(30);
31+
area = pizza.getArea();
32+
cout << "pizza의 면적은 " << area << endl;
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
class Point {
5+
int x, y;
6+
public:
7+
Point();
8+
Point(int a, int b);
9+
void show() { cout << "(" << x << ", " << y << ")" << endl; }
10+
};
11+
12+
Point::Point() : Point(0, 0) { }
13+
14+
Point::Point(int a, int b) : x(a), y(b) { }
15+
16+
int main() {
17+
Point origin;
18+
Point target(10, 20);
19+
origin.show();
20+
target.show();
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
class Rectangle {
5+
public:
6+
int width, height;
7+
Rectangle();
8+
Rectangle(int w, int h);
9+
Rectangle(int length);
10+
bool isSquare();
11+
};
12+
13+
Rectangle::Rectangle() : Rectangle(1, 1) { }
14+
15+
Rectangle::Rectangle(int w, int h) {
16+
width = w; height = h;
17+
}
18+
19+
Rectangle::Rectangle(int length) {
20+
width = height = length;
21+
}
22+
23+
bool Rectangle::isSquare() {
24+
return width == height;
25+
}
26+
27+
28+
int main() {
29+
Rectangle rect1;
30+
Rectangle rect2(3, 5);
31+
Rectangle rect3(3);
32+
33+
if (rect1.isSquare()) cout << "rect1은 정사각형이다." << endl;
34+
if (rect2.isSquare()) cout << "rect2는 정사각형이다." << endl;
35+
if (rect3.isSquare()) cout << "rect3은 정사각형이다." << endl;
36+
}

0 commit comments

Comments
 (0)
Please sign in to comment.