본문 바로가기

카테고리 없음

[C++]헤더파일 사용 예제

example1.cpp

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 
#include "point.h"
#include "example2.h"
 
int main()
{
    // 두 점을 만든다.
    Point a = {100100};
    Point b = {200200};
 
    // 함수를 호출한다.
    double dist;
    dist = Distance(a, b);
 
    return 0;
}
 
cs


example2.cpp

 

1
2
3
4
5
6
7
8
9
10
 
#include "example2.h"
#include "point.h"
 
double Distance(const Point& pt1, const Point& pt2)
{
    // 이 함수의 내용은 생략한다.
    return 0.0f;
}
 
cs

 


example2.h 

 

1
2
 
double Distance(const Point& pt1, const Point& pt2);
cs


point.h

 

1
2
3
4
5
6
7
 
struct Point
{
    int x, y;
};
 
 
cs

 


*함수내의 코드나 순서에 따라 오류가 날 가능성이 있으므로 

자기자신의 헤더파일도 추가 해주는 것이 좋다.

*여럿이 한 프로젝트를 진행할 때 유용할듯?

*코드를 좀더 간결하게 할 수 있다는 장점