Class implementation file

File implementing the methods of a class


title: "Class implementation file" type: doc version: 1 created: 2026-02-28 author: "Wikipedia contributors" status: active scope: public tags: ["class-(computer-programming)", "object-oriented-programming-languages", "c++", "articles-with-example-objective-c-code"] description: "File implementing the methods of a class" topic_path: "technology/programming-languages" source: "https://en.wikipedia.org/wiki/Class_implementation_file" license: "CC BY-SA 4.0" wikipedia_page_id: 0 wikipedia_revision_id: 0

::summary File implementing the methods of a class ::

In object-oriented programming, a class implementation file is often used to contain the implementation code for the method(s) of a class. Programming languages like C++ and Objective-C make use of these implementation files so as to separate the interface and implementation of these methods. | access-date = 2013-05-07 | author = Alan Griffiths | title = Separating Interface and Implementation in C++ | publisher = ACCU | year = 2005 | url = http://accu.org/index.php/journals/269

Motivation

Using this structure, a class 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 can instantiate an object of the class. The purpose of this structure is to keep the implementation code hidden, but allow the user to view the design. | access-date = 2013-05-07 | author = Alan Griffiths | title = Separating Interface and Implementation in C++ | publisher = ACCU | year = 2005 | url = http://accu.org/index.php/journals/269 | last = Neuberg | first = Matt | date = 26 May 2011 | title = Programming iOS 4 | publisher = O'Reilly Media, Inc. | chapter = Chapter 4.3 Header File and Implementation File | isbn = 978-1-4493-8843-0 | url-access = registration | url = https://archive.org/details/programmingios40000neub

Users make use of the public interface of an object 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. | access-date = 2013-05-07 | author = Alan Griffiths | title = Separating Interface and Implementation in C++ | publisher = ACCU | year = 2005 | url = http://accu.org/index.php/journals/269 This allows the user the information needed to use the class effectively, but prevents him or her from damaging the compiled code. | access-date = 2013-05-07 | title = C++ Dos and Don'ts | publisher = The Chromium Projects | url = https://www.chromium.org/developers/coding-style/cpp-dos-and-donts

The structure of a class implementation file

An implementation file is used in C++ programming when creating a class definition to split the interface from the implementation. The header file would declare all the member functions (methods) and data methods (fields) that the class has. | access-date = 2013-05-07 | title = Introduction to C++ Classes | url = http://pages.cs.wisc.edu/~hasti/cs368/CppTutorial/NOTES/CLASSES-INTRO.html | access-date = 2013-05-07 | author = Alan Griffiths | title = Separating Interface and Implementation in C++ | publisher = ACCU | year = 2005 | url = http://accu.org/index.php/journals/269 | access-date = 2013-05-07 | author = Febil Chacko Thanikal | title = How to define a template class in a .h file and implement it in a .cpp file | publisher = Code Project | year = 2009 | url = http://www.codeproject.com/Articles/48575/How-to-define-a-template-class-in-a-h-file-and-imp

The implementation file will contain the actual definition or source code of the methods declared in the header 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. | access-date = 2013-05-07 | title = The implementation file in C++ Programming | publisher = ITechTalk | url = http://www.itechtalk.com/thread1517.html It can also include any libraries from the C++ Standard Library that will be used by any of the declared methods in the file. The class implementation file will usually have a line to include the associated header file (see examples below).

Example in C++

An example would be having a class called ExampleClass. The header file of this C++ file would be named "" and the implementation file would be "". | access-date = 2013-05-07 | title = Introduction to C++ Classes | url = http://pages.cs.wisc.edu/~hasti/cs368/CppTutorial/NOTES/CLASSES-INTRO.html | last = Neuberg | first = Matt | date = 26 May 2011 | title = Programming iOS 4 | publisher = O'Reilly Media, Inc. | chapter = Chapter 4.3 Header File and Implementation File | isbn = 978-1-4493-8843-0 | url-access = registration | url = https://archive.org/details/programmingios40000neub

In this example, the implementation for the functions has been omitted, but the functions must be declared in **** like this: | access-date = 2013-05-07 | title = Introduction to C++ Classes | url = http://pages.cs.wisc.edu/~hasti/cs368/CppTutorial/NOTES/CLASSES-INTRO.html ::code[lang=cpp] #include

class ExampleClass { private: std::string name;
public: ExampleClass(); // Constructor. void addSomething(int k);
}; ::

An example of the structure of **** would look like this: ::code[lang=cpp] #pragma once

#include "ExampleClass.hpp"

ExampleClass::ExampleClass() = default;

void ExampleClass::addSomething(int k) { // ... }

::

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

In **** (the declarations file): ::code[lang=cpp] export module org.wikipedia.ExampleClass;

import std;

using std::string;

namespace org::wikipedia {

export class ExampleClass { private: string name;
public: ExampleClass(); // Constructor. void addSomething(int k);
};

} ::

In **** (the definitions/implementations file): ::code[lang=cpp] module org.wikipedia.ExampleClass;

namespace org::wikipedia {

ExampleClass::ExampleClass() = default;

void ExampleClass::addSomething(int k) { // ... }

} ::

This can be included into a file like so:

::code[lang=cpp] import org.wikipedia.ExampleClass;

using org::wikipedia::ExampleClass;

int main() { ExampleClass myInst; myInst.addSomething(5); return 0; } ::

Example in Objective-C

Another example of how a class implementation file would be structured can be seen with Objective-C, which is used in iOS programming. | last = Neuberg | first = Matt | date = 26 May 2011 | title = Programming iOS 4 | publisher = O'Reilly Media, Inc. | chapter = Chapter 4.3 Header File and Implementation File | isbn = 978-1-4493-8843-0 | url-access = registration | url = https://archive.org/details/programmingios40000neub This example will use "ExampleClass". A notable difference between C++ and Objective-C when making use of these implementation files is the extensions used at the end of the files. In C++ it will be .cpp | access-date = 2013-05-07 | title = Introduction to C++ Classes | url = http://pages.cs.wisc.edu/~hasti/cs368/CppTutorial/NOTES/CLASSES-INTRO.html and in Objective-C it will be .m, | last = Neuberg | first = Matt | date = 26 May 2011 | title = Programming iOS 4 | publisher = O'Reilly Media, Inc. | chapter = Chapter 4.3 Header File and Implementation File | isbn = 978-1-4493-8843-0 | url-access = registration | url = https://archive.org/details/programmingios40000neub but both will use the same .h extension for their header file(s) | access-date = 2013-05-07 | title = Introduction to C++ Classes | url = http://pages.cs.wisc.edu/~hasti/cs368/CppTutorial/NOTES/CLASSES-INTRO.html | last = Neuberg | first = Matt | date = 26 May 2011 | title = Programming iOS 4 | publisher = O'Reilly Media, Inc. | chapter = Chapter 4.3 Header File and Implementation File | isbn = 978-1-4493-8843-0 | url-access = registration | url = https://archive.org/details/programmingios40000neub as shown in the example below.

This is an example of ExampleClass.h in Objective-C: ::code[lang=objc] #import <UIKit/UIKit.h>

@interface ExampleClass : NSObject { // instance variable declarations go here }

  • (NSString*) name; @end ::

This is an example of the class's implementation file Exampleclass.m in Objective-C: ::code[lang=objc] #import "ExampleClass.h"

@implementation ExampleClass

  • (NSString*) name { return @"…"; } @end ::

References

::callout[type=info title="Wikipedia Source"] This article was imported from Wikipedia and is available under the Creative Commons Attribution-ShareAlike 4.0 License. Content has been adapted to SurfDoc format. Original contributors can be found on the article history page. ::

class-(computer-programming)object-oriented-programming-languagesc++articles-with-example-objective-c-code