In
object-oriented programming
Object-oriented programming (OOP) is a programming paradigm based on the concept of " objects", which can contain data and code. The data is in the form of fields (often known as attributes or ''properties''), and the code is in the form of ...
, a class implementation file is often used to contain the implementation code for the
method(s) of a
class. Programming languages like C and C++ make use of these implementation files so as to separate the interface and implementation of these methods.
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
Instantiation or instance may refer to:
Philosophy
* A modern concept similar to ''participation'' in classical Platonism; see the Theory of Forms
* The instantiation principle, the idea that in order for a property to exist, it must be had b ...
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.
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.
This allows the user the information needed to use the class effectively, but prevents him or her from damaging the compiled code.
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
Many programming languages and other computer files have a directive, often called include (sometimes copy or import), that causes the contents of the specified file to be inserted into the original file. These included files are called copybooks ...
would declare all the
member functions (methods) and
data methods (fields) that the class has.
The implementation file will contain the actual definition or
source code
In computing, source code, or simply code, is any collection of code, with or without comment (computer programming), comments, written using a human-readable programming language, usually as plain text. The source code of a Computer program, p ...
of the methods declared in the
header file
Many programming languages and other computer files have a directive, often called include (sometimes copy or import), that causes the contents of the specified file to be inserted into the original file. These included files are called copybooks ...
. 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.
It can also include any libraries from the
C++ Standard Library
The C standard library or libc is the standard library for the C programming language, as specified in the ISO C standard. ISO/ IEC (2018). '' ISO/IEC 9899:2018(E): Programming Languages - C §7'' Starting from the original ANSI C standard, it wa ...
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 "example_class.h" and the implementation file would be "example_class.cc".
An example of the structure of example_class.cc would look like this:
#include "example_class.h"
ExampleClass::ExampleClass() = default;
void ExampleClass::AddSomething(int k)
In this example, the implementation for the functions has been omitted, but the functions must be declared in example_class.h like this:
#include
class ExampleClass ;
Example in Objective-C
Another example of how a class implementation file would be structured can be seen with
Objective-C
Objective-C is a general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language. Originally developed by Brad Cox and Tom Love in the early 1980s, it was selected by NeXT for its N ...
, which is used in
iOS programming.
This example will use "ExampleClass". A notable difference between C++ and
Objective-C
Objective-C is a general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language. Originally developed by Brad Cox and Tom Love in the early 1980s, it was selected by NeXT for its N ...
when making use of these implementation files is the extensions used at the end of the files. In C++ it will be .cpp
and in
Objective-C
Objective-C is a general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language. Originally developed by Brad Cox and Tom Love in the early 1980s, it was selected by NeXT for its N ...
it will be .m,
but both will use the same .h extension for their
header file(s)[
]
as shown in the example below.
This is an example of ExampleClass.h in
Objective-C
Objective-C is a general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language. Originally developed by Brad Cox and Tom Love in the early 1980s, it was selected by NeXT for its N ...
:
#import
@interface ExampleClass : NSObject
- (NSString*) name;
@end
This is an example of the class's implementation file Exampleclass.m in
Objective-C
Objective-C is a general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language. Originally developed by Brad Cox and Tom Love in the early 1980s, it was selected by NeXT for its N ...
:
#import "ExampleClass.h"
@implementation ExampleClass
- (NSString*) name
@end
See also
*
C++ classes
A class in C++ is a user-defined type or data structure declared with keyword class that has data and functions (also called member variables and member functions) as its members whose access is governed by the three access specifiers ''privat ...
*
Header file
Many programming languages and other computer files have a directive, often called include (sometimes copy or import), that causes the contents of the specified file to be inserted into the original file. These included files are called copybooks ...
*
Source code
In computing, source code, or simply code, is any collection of code, with or without comment (computer programming), comments, written using a human-readable programming language, usually as plain text. The source code of a Computer program, p ...
*
C++ Standard Library
The C standard library or libc is the standard library for the C programming language, as specified in the ISO C standard. ISO/ IEC (2018). '' ISO/IEC 9899:2018(E): Programming Languages - C §7'' Starting from the original ANSI C standard, it wa ...
*
Objective-C
Objective-C is a general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language. Originally developed by Brad Cox and Tom Love in the early 1980s, it was selected by NeXT for its N ...
References
External links
Header File and Implementation File
{{DEFAULTSORT:Class Implementation File
Class (computer programming)
Object-oriented programming languages
C++
Articles with example Objective-C code