Prosto

C++에서의 파일 분할. (+예) 본문

Programing/C++ Programing

C++에서의 파일 분할. (+예)

Prosto 2011. 9. 27. 02:13



일단 저번 예제를 파일 분할 함으로 얻을 수 있는 것이 있습니다.
(헤더 cpp main...)

#################### 헤더파일 ####################### Point.h
//헤더파일의 경우 클래스의 선언을 담습니다.

class Point
{
private:
   int xpos;
   int ypos;
 
public:
   void PointSet(int x, int y);
   void MovePos(int x, int y);   //점의 좌표이동
   void AddPoint(const Point &pos); //점의 좌표증가
   void ShowPosition();    //현재 x, y좌표정보 출력
};

#################### cpp파일 ####################### Point.cpp
// 클래스의 정의(멤버함수의 정의)를 담습니다. (main과 정의부를 따로 분리할 경우.)

#include<iostream>
#include"Point.h"


using namespace std;


 void Point::PointSet(int x, int y) //포인트 위치 지정
 {
  xpos=x;
  ypos=y;
 }

 void Point::MovePos(int x, int y) //포인트 이동.
 {
  xpos += x;
  ypos += y;
 };

 void Point::AddPoint(const Point &pos) //다른 포인트와 합치기
 {
  xpos += pos.xpos;
  ypos += pos.ypos;
 };

 void Point::ShowPosition() //포인트 위치 출력.
 {
  cout << "[" << xpos << " , " << ypos << "]" << endl;
 }


#################### main ####################### main.cpp

#include"Point.h"

int main(void)
{
 Point pos1;
 pos1.PointSet(12, 4);
 Point pos2;
 pos2.PointSet(20, 30);

 pos1.MovePos(-7, 10);
 pos1.ShowPosition();  // [5, 14] 출력.

 pos1.AddPoint(pos2);
 pos1.ShowPosition();  // [25, 44] 출력.

 return 0;
}



정상적인 실행 경과 :

파일 분할을 하게 되면 비록 파일은 더 많아지지만 관리하기가 매우 용이해집니다.

프로그래밍 시 수정을 하게되는 경우가 빈번하게 발생하게 됩니다.

이 때에 찾아가기도 편하고 수정하기도 편합니다.

파일 분할을 연습하여 숙련된 사용은 꼭 필요한 것이라 여겨집니다.

주석과 파일 분할! 중요합니다.

 

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

클래스를 이용한 예제. (좌표 점 이동)  (0) 2011.09.27
이름공간(name space)  (0) 2011.09.25
인라인(inline) 함수  (0) 2011.09.25
매개변수의 디폴트 값.  (0) 2011.09.25
Function Overloading  (0) 2011.09.25
Comments