일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- LG Aimers 4th
- 지도학습
- Classification
- regression
- 회귀
- PCA
- AI
- deep learning
- GPT-4
- LG
- OpenAI
- 티스토리챌린지
- supervised learning
- Machine Learning
- ChatGPT
- 해커톤
- 머신러닝
- 딥러닝
- gpt
- LG Aimers
- LLM
- 오블완
- 분류
Archives
- Today
- Total
SYDev
C++ Chapter 04-2 : 캡슐화(Encapsulation) 문제풀이 본문
문제
다음의 Point 클래스를 기반으로 하여(활용하여) 원을 의미하는 Circle 클래스를 정의하자.
class Point
{
private:
int xpos, ypos;
public:
void Init(int x, int y)
{
xpos = x;
ypos = y;
}
void ShowPointInfo() const
{
cout<<"["<<xpos<<", "<<ypos<<"]"<<endl;
}
};
Circle 객체에는 좌표상의 위치정보(원의 중심좌표)와 반지름의 길이 정보를 저장 및 출력할 수 있어야 한다.
그리고 여러분이 정의한 Circle 클래스를 기반으로 Ring 클래스도 정의하자.
링은 두개의 원으로 표현 가능하므로(바깥쪽 원과 안쪽 원), 두 개의 Circle 객체를 기반으로 정의가 가능하다.
다음 main 함수를 기반으로 실행시키자.
int main(void)
{
Ring ring;
ring.Init(1, 1, 4, 2, 2, 9);
ring.ShowRingInfo();
return 0;
}
실행의 예
Inner Circle Info...
radius : 4
[1, 1]
Outter Circle Info...
radius : 9
[2, 2]
문제풀이
#include <iostream>
using namespace std;
class Point
{
private:
int xpos, ypos;
public:
void Init(int x, int y)
{
xpos = x;
ypos = y;
}
int GetX() const
{
return xpos;
}
int GetY() const
{
return ypos;
}
void ShowPointInfo() const
{
cout<<"["<<xpos<<", "<<ypos<<"]"<<endl;
}
};
class Circle
{
private:
Point cirmid;
int rad;
public:
void Init(const Point &pos, const int &r)
{
cirmid = pos;
rad = r;
}
void ShowPointInfo() const
{
cout<<"radius : "<<rad<<endl;
cirmid.ShowPointInfo();
}
};
class Ring
{
private:
Point innerPos;
Point outterPos;
Circle innerCir;
Circle outterCir;
public:
void Init(const int &x1, const int &y1,const int &rad1, const int &x2, const int &y2,const int &rad2 )
{
innerPos.Init(x1,y1);
outterPos.Init(x2,y2);
innerCir.Init(innerPos,rad1);
outterCir.Init(outterPos,rad2);
}
void ShowRingInfo() const
{
cout<<"Inner Circle Info..."<<endl;
innerCir.ShowPointInfo();
cout<<"Outter Circle Info..."<<endl;
outterCir.ShowPointInfo();
}
};
int main(void)
{
Ring ring;
ring.Init(1, 1, 4, 2, 2, 9);
ring.ShowRingInfo();
return 0;
}
출처 : 윤성우, <윤성우의 열혈 C++ 프로그래밍>, 오렌지미디어, 2010.05.12
'Programming Lang > C++' 카테고리의 다른 글
C++ Chapter 04-3 : 생성자(Constructor)와 소멸자(Destructor)(1) (0) | 2023.07.16 |
---|---|
C++ Chapter 04-2 : 캡슐화(Encapsulation) 문제풀이 (0) | 2023.07.15 |
C++ Chapter 04-2: 캡슐화(Encapsulation) (0) | 2023.07.15 |
C++ Chapter 04-1 : 정보은닉(Information Hiding) (0) | 2023.07.15 |
C++ Chapter 03-2 : 객체지향 프로그래밍의 이해 (0) | 2023.07.13 |