Point 클래스는 점의 좌표를 알려주는 클래스이고, Circle 클래스는 원의 반지름과 중심점의 좌표를 알려주는 클래스이다. 그리고 마지막으로 Ring클래스는 Point, Circle 클래스를 묶는(캡슐화) 클래스이다. Ring클래스는 좌표정보와 반지름을 입력받아 Point, Circle에 전달한다. 


#include <iostream>

using namespace std;


class Point

{

private:

int xpos, ypos;


public:

Point(int x, int y) : xpos(x), ypos(y)         //Constructor

{}

void ShowPointInfo(void) const

{

cout<<"["<<xpos<<", "<<ypos<<"]"<<endl;

}

};


class Circle

{

private:

Point coord;

const int rad;


public:

Circle(const int x, const int y, const int r)

:coord(x, y), rad(r) //Constructor

{}

void ShowCircleInfo(void) const

{

cout<<"radius: "<<rad<<endl;

coord.ShowPointInfo();

}

};


class Ring

{

private:

Circle c1;

Circle c2;


public:

Ring(int x1, int y1, int r1, int x2, int y2, int r2)

:c1(x1, y1, r1), c2(x2, y2, r2)         //Constructor

{}

void ShowRingInfo(void) const

{

cout<<"Inner Circle Info..."<<endl;

c1.ShowCircleInfo();

cout<<"Outter Circle Info..."<<endl;

c2.ShowCircleInfo();

}

};


int main(void)

{

Ring ring(1, 1, 4, 2, 2, 9);

ring.ShowRingInfo();


return 0;

}


실행결과



'Programming > C++' 카테고리의 다른 글

객체 배열 & 객체 포인터 배열  (0) 2012.05.21
생성자 & 소멸자를 이용한 예제  (0) 2012.05.21
소멸자(Destructor)  (0) 2012.05.20
private 생성자  (0) 2012.05.20
디폴트 생성자(Default Constructor)  (0) 2012.05.20
Posted by scii
: