헤더 부분
1. 함수 원형
2. 구조체, 클래스, 템플릿 선언
3. #define 또는 const 사용 기호 상수
4. 인라인 함수
하나의 c++파일만 사용시
#include <iostream>
using namespace std;
struct MyStruct {
string name;
int age;
};
void display(MyStruct&);
int main() {
MyStruct A = { "Jack", 20 };
display(A);
return 0;
}
void display(MyStruct& temp) {
cout << "이름 : " << temp.name << endl;
cout << "나이 : " << temp.age;
}
분리 후
struct.h 헤더파일
#ifndef STRUCT // 몸체 파일 에서 struct.h 한번 읽은 후 또 읽어 오는 것을 방지
#define STRUCT
#include <iostream>
using namespace std;
struct MyStruct {
string name;
int age;
};
void display(MyStruct&);
#endif
c++ 몸체 파일
#include "struct.h"
#include "struct.h"
int main() {
MyStruct A = { "Jack", 20 };
display(A);
return 0;
}
c++ 함수 파일
#include "struct.h"
void display(MyStruct& temp) {
cout << "이름 : " << temp.name << endl;
cout << "나이 : " << temp.age;
}
728x90
'c++ 기초' 카테고리의 다른 글
21) 생성자, 소멸자 (0) | 2025.01.04 |
---|---|
20) 클래스 (0) | 2025.01.03 |
18) 함수 템플릿 (0) | 2025.01.01 |
17) 함수 오버로딩 (0) | 2025.01.01 |
16) 참조, 포인터, 값에 의한 호출 (0) | 2025.01.01 |