C++

[CPP] 분할 구현

Guk-blog 2024. 3. 15. 14:10
728x90
반응형

- .h(헤더) 파일에는 함수나 클래스의 선언
- .cpp 파일에는 함수나 클래스의 구현(구현하는 부분에서는 선언한 .h파일을 #include "~~.h"식으로 전처리 해주어야 한다)
- 이후 함수를 참조하려면 .h파일 전처리(#include) 하면 해당 함수 사용 가능  

test.h

class TestClass{
private:
  int m_testValue;
public:
  void Output();
  void Set(int _value);
  int Get();
}

test.cpp

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

void Test::Output(){
  std::cout << m_testValue << std::endl;
}

int Test::Get(){
  return m_testValue;
}

void Test::Set(int _value){
  m_testValue = value;
}

main.cpp

#include "test.h"

int main(){
 int value = 77;
 Test::Set(value);
 Test::Output(); // 77
 value = -70;
 Test::Set(Test::Get() + value)
 Test::Output(); // 7
 return 0;
}

728x90
반응형