클래스 구현 파일

Class implementation file

객체 지향 프로그래밍에서 클래스 구현 파일은 클래스 메서드에 대한 구현 코드를 포함하기 위해 종종 사용됩니다.C 및 C++와 같은 프로그래밍 언어는 이러한 [1]구현 파일을 사용하여 이러한 메서드의 인터페이스와 구현을 분리합니다.

동기

이 구조를 사용하면 클래스 및 해당 멤버의 선언을 포함하는 클래스 정의 파일도 생성됩니다.클래스 정의가 포함되어 있고 해당 메서드에 대한 구현 파일을 사용할 수 있는 경우 사용자는 클래스의 개체를 인스턴스화할 수 있습니다.이 구조의 목적은 구현 코드를 숨긴 상태로 유지하되 사용자가 설계를 [2][3]볼 수 있도록 하는 것입니다.

사용자는 오브젝트의 퍼블릭인터페이스를 사용하여 오브젝트를 최대한 간단하게 작성할 수 있으므로 클라이언트코드가 클래스 [4]구현의 불필요한 세부사항으로 사용자의 주의를 분산시키지 않습니다.이를 통해 사용자는 클래스를 효과적으로 사용하는 데 필요한 정보를 얻을 수 있지만 컴파일된 [5]코드가 손상되지 않습니다.

클래스 구현 파일의 구조

구현 파일은 C++ 프로그래밍에서 구현에서 인터페이스를 분할하는 클래스 정의를 만들 때 사용됩니다.헤더 파일은 클래스에 [6][7][8]있는 모든 멤버 함수(메서드)와 데이터 메서드(필드)를 선언합니다.

구현 파일에는 헤더 파일에 선언된 메서드의 실제 정의 또는 소스 코드가 포함됩니다.이 파일은 헤더 블록으로 시작할 수 있습니다.이 블록은 정의된 클래스의 목적과 파일의 작성자 및 파일이 [9]작성된 날짜 등 실제 파일 작성에 대한 자세한 내용을 설명하는 주석을 제공합니다.또한 파일 내의 선언된 메서드에서 사용되는 C++ 표준 라이브러리의 라이브러리를 포함할 수도 있습니다.클래스 실장 파일에는 보통 관련된 헤더파일을 포함하는 행이 있습니다(아래 예 참조).

C++의 예

예를 들면, 「CHANGE BOOK」라고 하는 수업을 하는 것입니다.ExampleClass이 C++ 파일의 헤더 파일 이름은 "example_class"입니다.h" 및 구현 파일은 "disc_class.cc"[10][11]가 됩니다.

example_class.cc 구조의 예는 다음과 같습니다.

#실패하다 "class_class.h"  클래스 예시::클래스 예시() = 체납;  무효 예: 클래스:Add Something(인트 k) {    ... } 

이 예에서는 함수의 실장은 생략되어 있지만 함수는 다음과 같이 [12]example_class.h로 선언해야 합니다.

#실패하다 <문자열>  학급 클래스 예시 {  일반의:   클래스 예시();  // 컨스트럭터   무효 Add Something(인트 k);             사적인:   표준::스트링 이름_;                       }; 

Objective-C의 예

클래스 구현 파일이 어떻게 구성되는지에 대한 또 다른 예는 iOS [13]프로그래밍에서 사용되는 Objective-C를 통해 확인할 수 있습니다.이 예에서는 "ExampleClass"를 사용합니다.이러한 실장 파일을 사용할 때 C++와 Objective-C의 현저한 차이는 파일 끝에 사용되는 확장자입니다.C++에서는 .cpp[14] 되고 Objective-C에서는 .[15]m이 됩니다.다만, 다음의 [16][17]예와 같이, 양쪽 모두 헤더 파일에 같은 .h 확장자를 사용합니다.

다음 예제에서는 Objective-C의 ExampleClass.h를 보여 줍니다.

#import <UIKit/UIKit.h> @interface ExampleClass : NSObject { // instance 변수 선언은 여기로 갑니다} - (NSString*) 이름; @end

다음 예제에서는 Objective-C 있는 클래스의 구현 파일 Exampleclass.m을 보여 줍니다.

# Import "Example Class.h"  @communications(@정보) 클래스 예시 - (동작하지 않다*) 이름. {     돌아가다 @"…"; } @end 

「 」를 참조해 주세요.

레퍼런스

  1. ^ Alan Griffiths (2005). "Separating Interface and Implementation in C++". http://accu.org/index.php/journals/: ACCU. Retrieved 2013-05-07. {{cite web}}:외부 링크 location=(도움말)
  2. ^ Alan Griffiths (2005). "Separating Interface and Implementation in C++". http://accu.org/index.php/journals/: ACCU. Retrieved 2013-05-07. {{cite web}}:외부 링크 location=(도움말)
  3. ^ Neuberg, Matt (26 May 2011). "Chapter 4.3 Header File and Implementation File". Programming iOS 4. O’Reilly Media, Inc. ISBN 978-1-4493-8843-0.
  4. ^ Alan Griffiths (2005). "Separating Interface and Implementation in C++". http://accu.org/index.php/journals/: ACCU. Retrieved 2013-05-07. {{cite web}}:외부 링크 location=(도움말)
  5. ^ "C++ Dos and Don'ts". https://www.chromium.org/developers/coding-style/cpp-dos-and-donts: The Chromium Projects. Retrieved 2013-05-07. {{cite web}}:외부 링크 location=(도움말)
  6. ^ "Introduction to C++ Classes". http://pages.cs.wisc.edu/~hasti/cs368/CppTutorial/NOTES/. Retrieved 2013-05-07. {{cite web}}:외부 링크 location=(도움말)CS1 유지보수: 위치(링크)
  7. ^ Alan Griffiths (2005). "Separating Interface and Implementation in C++". http://accu.org/index.php/journals/: ACCU. Retrieved 2013-05-07. {{cite web}}:외부 링크 location=(도움말)
  8. ^ Febil Chacko Thanikal (2009). "How to define a template class in a .h file and implement it in a .cpp file". http://www.codeproject.com/Articles/48575/How-to-define-a-template-class-in-a-h-file-and-imp: Code Project. Retrieved 2013-05-07. {{cite web}}:외부 링크 location=(도움말)CS1 유지보수: 위치(링크)
  9. ^ "The implementation file in C++ Programming". http://www.itechtalk.com/: ITechTalk. Retrieved 2013-05-07. {{cite web}}:외부 링크 location=(도움말)
  10. ^ "Introduction to C++ Classes". http://pages.cs.wisc.edu/~hasti/cs368/CppTutorial/NOTES/CLASSES-INTRO.html. Retrieved 2013-05-07. {{cite web}}:외부 링크 location=(도움말)CS1 유지보수: 위치(링크)
  11. ^ Neuberg, Matt (26 May 2011). "Chapter 4.3 Header File and Implementation File". Programming iOS 4. O’Reilly Media, Inc. ISBN 978-1-4493-8843-0.
  12. ^ "Introduction to C++ Classes". http://pages.cs.wisc.edu/~hasti/cs368/CppTutorial/NOTES/CLASSES-INTRO.html. Retrieved 2013-05-07. {{cite web}}:외부 링크 location=(도움말)CS1 유지보수: 위치(링크)
  13. ^ Neuberg, Matt (26 May 2011). "Chapter 4.3 Header File and Implementation File". Programming iOS 4. O’Reilly Media, Inc. ISBN 978-1-4493-8843-0.
  14. ^ "Introduction to C++ Classes". http://pages.cs.wisc.edu/~hasti/cs368/CppTutorial/NOTES/. Retrieved 2013-05-07. {{cite web}}:외부 링크 location=(도움말)CS1 유지보수: 위치(링크)
  15. ^ Neuberg, Matt (26 May 2011). "Chapter 4.3 Header File and Implementation File". Programming iOS 4. O’Reilly Media, Inc. ISBN 978-1-4493-8843-0.
  16. ^ "Introduction to C++ Classes". http://pages.cs.wisc.edu/~hasti/cs368/CppTutorial/NOTES/. Retrieved 2013-05-07. {{cite web}}:외부 링크 location=(도움말)CS1 유지보수: 위치(링크)
  17. ^ Neuberg, Matt (26 May 2011). "Chapter 4.3 Header File and Implementation File". Programming iOS 4. O’Reilly Media, Inc. ISBN 978-1-4493-8843-0.

외부 링크