Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Class implementation file

From Wikipedia, the free encyclopedia
File implementing the methods of a class

Inobject-oriented programming, aclass implementation file is often used to contain the implementation code for themethod(s) of aclass. Programming languages like C++ and Objective-C make use of these implementation files so as to separate the interface and implementation of these methods.[1]

Motivation

[edit]

Using this structure, aclass definition file containing the declaration of the class and its members is also created. If the class definition has been included and the implementation file for its methods is available, the user caninstantiate anobject of the class. The purpose of this structure is to keep the implementation code hidden, but allow the user to view the design.[2][3]

Users make use of the public interface of anobject so as to make creating objects as simple as possible, ensuring that client code does not distract the user with unnecessary details of the class's implementation.[4]This allows the user the information needed to use the class effectively, but prevents him or her from damaging the compiled code.[5]

The structure of a class implementation file

[edit]

An implementation file is used inC++ programming when creating aclass definition to split the interface from the implementation. Theheader file would declare all themember functions (methods) anddata methods (fields) that the class has.[6][7][8]

The implementation file will contain the actual definition orsource code of the methods declared in theheader file. This file can start with a header block, which provides comments that describe the purpose of the defined class and any details about the creation of the actual file, such as the author of the file and date the file was created.[9]It can also include any libraries from theC++ Standard Library that will be used by any of the declared methods in the file. Theclass implementation file will usually have a line to include the associated header file (see examples below).

Example in C++

[edit]

An example would be having a class calledExampleClass. The header file of this C++ file would be named "Example.hpp" and the implementation file would be "ExampleClass.cpp".[10][11]

In this example, the implementation for the functions has been omitted, but the functions must be declared inExampleClass.hpp like this:[12]

#include<string>classExampleClass{private:std::stringname;public:ExampleClass();// Constructor.voidaddSomething(intk);};

An example of the structure ofExampleClass.cpp would look like this:

#pragma once#include"ExampleClass.hpp"ExampleClass::ExampleClass()=default;voidExampleClass::addSomething(intk){// ...}

This structure can be replicated withC++ modules as well, albeit with slightly different semantics.

InExampleClass.cppm (the declarations file):

exportmoduleorg.wikipedia.ExampleClass;importstd;usingstd::string;namespaceorg::wikipedia{exportclassExampleClass{private:stringname;public:ExampleClass();// Constructor.voidaddSomething(intk);};}

InExampleClass.cpp (the definitions/implementations file):

moduleorg.wikipedia.ExampleClass;namespaceorg::wikipedia{ExampleClass::ExampleClass()=default;voidExampleClass::addSomething(intk){// ...}}

This can be included into aMain.cpp file like so:

importorg.wikipedia.ExampleClass;usingorg::wikipedia::ExampleClass;intmain(){ExampleClassmyInst;myInst.addSomething(5);return0;}

Example in Objective-C

[edit]

Another example of how a class implementation file would be structured can be seen withObjective-C, which is used iniOS programming.[13]This example will use "ExampleClass". A notable difference between C++ andObjective-C when making use of these implementation files is the extensions used at the end of the files. In C++ it will be.cpp[14]and inObjective-C it will be.m,[15]but both will use the same.h extension for their header file(s)[16][17]as shown in the example below.

This is an example ofExampleClass.h inObjective-C:

#import <UIKit/UIKit.h>@interfaceExampleClass :NSObject{// instance variable declarations go here}-(NSString*)name;@end

This is an example of theclass's implementation fileExampleclass.m inObjective-C:

#import "ExampleClass.h"@implementationExampleClass-(NSString*)name{return@"…";}@end

See also

[edit]

References

[edit]
  1. ^Alan Griffiths (2005)."Separating Interface and Implementation in C++". ACCU. Retrieved2013-05-07.
  2. ^Alan Griffiths (2005)."Separating Interface and Implementation in C++". ACCU. Retrieved2013-05-07.
  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++". ACCU. Retrieved2013-05-07.
  5. ^"C++ Dos and Don'ts". The Chromium Projects. Retrieved2013-05-07.
  6. ^"Introduction to C++ Classes". Retrieved2013-05-07.
  7. ^Alan Griffiths (2005)."Separating Interface and Implementation in C++". ACCU. Retrieved2013-05-07.
  8. ^Febil Chacko Thanikal (2009)."How to define a template class in a .h file and implement it in a .cpp file". Code Project. Retrieved2013-05-07.
  9. ^"The implementation file in C++ Programming". ITechTalk. Retrieved2013-05-07.
  10. ^"Introduction to C++ Classes". Retrieved2013-05-07.
  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". Retrieved2013-05-07.
  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". Retrieved2013-05-07.
  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". Retrieved2013-05-07.
  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.

External links

[edit]
Retrieved from "https://en.wikipedia.org/w/index.php?title=Class_implementation_file&oldid=1317280922"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2026 Movatter.jp