Method Implementation
   HOME

TheInfoList



OR:

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 Class or The Class may refer to: Common uses not otherwise categorized * Class (biology), a taxonomic rank * Class (knowledge representation), a collection of individuals or objects * Class (philosophy), an analytical concept used differentl ...
. 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 In object-oriented programming, a class is an extensible program-code-template for creating objects, providing initial values for state (member variables) and implementations of behavior (member functions or methods). In many languages, the class n ...
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 Object may refer to: General meanings * Object (philosophy), a thing, being, or concept ** Object (abstract), an object which does not exist at any particular time or place ** Physical object, an identifiable collection of matter * Goal, an ...
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 Object may refer to: General meanings * Object (philosophy), a thing, being, or concept ** Object (abstract), an object which does not exist at any particular time or place ** Physical object, an identifiable collection of matter * Goal, an ...
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 In object-oriented programming, a class is an extensible program-code-template for creating objects, providing initial values for state (member variables) and implementations of behavior (member functions or methods). In many languages, the class n ...
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 comments, written using a human-readable programming language, usually as plain text. The source code of a program is specially designed to facilitate the w ...
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 was ...
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 NeXT ...
, which is used in
iOS iOS (formerly iPhone OS) is a mobile operating system created and developed by Apple Inc. exclusively for its hardware. It is the operating system that powers many of the company's mobile devices, including the iPhone; the term also include ...
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 NeXT ...
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 NeXT ...
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 NeXT ...
: #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 NeXT ...
: #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 comments, written using a human-readable programming language, usually as plain text. The source code of a program is specially designed to facilitate the w ...
*
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 was ...
*
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 NeXT ...


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