[문제]
아래의 코드를 보고 모든 클래스에 생성자를 정의하라.
#include<iostream>
using namespace std;
class Point
{
private:
int xpos;
int ypos;
public:
void Init(int x, int y)
{
xpos = x;
ypos = y;
}
void ShowPointInfo() const
{
cout << "[" << xpos << ", " << ypos << "]" << endl;
}
};
class Circle
{
private:
Point center;
int rad;
public:
void Init(int x, int y, int r)
{
center.Init(x, y);
rad = r;
}
void ShowCircleInfo() const
{
cout << "radius: " << rad << endl;
center.ShowPointInfo();
}
};
class Ring
{
private:
Circle InCir, OutCir;
public:
void Init(int inxpos, int inypos, int inrad, int outxpos, int outypos, int outrad)
{
InCir.Init(inxpos, inypos, inrad);
OutCir.Init(outxpos, outypos, outrad);
}
void ShowRingInfo() const
{
cout << "Inner Circle Info.." << endl;
InCir.ShowCircleInfo();
cout << "Outter Circle Info.." << endl;
OutCir.ShowCircleInfo();
}
};
int main()
{
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;
int ypos;
public:
Point(int x, int y) :xpos(x), ypos(y)
{ }
void ShowPointInfo() const
{
cout << "[" << xpos << ", " << ypos << "]" << endl;
}
};
class Circle
{
private:
Point center;
int rad;
public:
Circle(int x, int y, int r) :center(x, y)
{
rad = r;
}
void ShowCircleInfo() const
{
cout << "radius: " << rad << endl;
center.ShowPointInfo();
}
};
class Ring
{
private:
Circle InCir, OutCir;
public:
Ring(int inxpos, int inypos, int inrad, int outxpos, int outypos, int outrad)
:InCir(inxpos, inypos, inrad), OutCir(outxpos, outypos, outrad)
{ }
void ShowRingInfo() const
{
cout << "Inner Circle Info.." << endl;
InCir.ShowCircleInfo();
cout << "Outter Circle Info.." << endl;
OutCir.ShowCircleInfo();
}
};
int main()
{
Ring ring(1, 1, 4, 2, 2, 9);
ring.ShowRingInfo();
return 0;
}
<참고>
윤성우의 열혈 C++ 프로그래밍
'C++ 언어 > 연습문제' 카테고리의 다른 글
C++ 언어 연습문제21 (0) | 2022.10.24 |
---|---|
C++ 언어 연습문제20 (0) | 2022.10.21 |
C++ 언어 연습문제18 (0) | 2022.10.20 |
C++ 언어 연습문제17 (0) | 2022.10.19 |
C++ 언어 연습문제16 (0) | 2022.10.18 |