본문 바로가기

System Programmings/C++

[C++] 클래스 포함

반응형

cur 클래스가 point 클래스를 포함하는 관계

cur 클래스의 정보은닉

약속된 것 : public으로 지정된 멤버 함수들의 원형

약속되지 않은 것 : 두 개의 point 객체를 갖는 벰버

#include <iostream>
#include "cur.h"
using namespace std;

int main()
{
	cur a(1,2,3,4); // cur 클래스 타입 변수 선언
	a.print(); // 출력
	cur b; // 초기화 값이 0인지 확인하기 위해 대입 없이 선언
	b.print(); // point 클래스의 기본 생성자의 값에 따라 바뀐다. point::point()

	cur c;
	c.setbottomright(point(3, 4)); // 아래의 오른쪽 x, y값 설정
	c.settopleft(point(1, 2)); // 위의 왼쪽 x, y값 설정
	c.print();

	return 0;
}
#ifndef POINT_H
#define POINT_H

class point
{
public:

	// 생성자들
	point();
	point(int x, int y);
	point(const point& pt);

	// 접근자
	void SetX(int x); // X값 설정
	void SetY(int y);
	int GetX() {return _x;} // x값 리턴
	int GetY() {return _y;}

protected:
	// point 클래스의 멤버 변수를 가질 x, y값
	int _x;
	int _y;

};
// 입력값을 0~100사이로 제한
inline void point::SetX(int x)
{
	if (x>100)
	_x=100;
	else if(x<0)
	_x=0;
	else _x=x;
}

inline void point::SetY(int y)
{
	if (y>100)
	_y=100;
	else if(y<0)
	_y=0;
	else
	_y=y;
}

#endif
#include "point.h"


point::point(const point& pt)
{
	_x=pt._x;
	_y=pt._y;
}

point::point(int x, int y)
{
	SetX(x);
	SetY(y);
}

point::point() // 기본 생성자
{
	_x=0;
	_y=0;
}
#ifndef CUR_H
#define CUR_H

#include "point.h"

class cur
{
public:
	void settopleft(const point& pt); // point 클래스를 이용
	void setbottomright(const point& pt);
	cur();
	cur(int x, int y, int z, int q); // 왼쪽 위 x,y 좌표 오른쪽 아래 x, y 좌표 설정
	void print();
protected:
	// cur 클래스가 가질 벰버 변수
	point LT; // point 클래스 타입 선언 (LT : Left Top)
	point RB; // point 클래스 타입 선언 (RB : Right Bottom)
};

#endif
_M#]

cur.cpp

cur.h

main.cpp

point.cpp

point.h


반응형