Class implementation file

Last updated

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. [1]

Contents

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. [2] [3]

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. [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

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. [6] [7] [8]

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. [9] 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 "example_class.h" and the implementation file would be "example_class.cc". [10] [11]

An example of the structure of example_class.cc would look like this:

#include"example_class.h"ExampleClass::ExampleClass()=default;voidExampleClass::AddSomething(intk){...}

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

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

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. [13] 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 [14] and in Objective-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 of ExampleClass.h in Objective-C:

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

This is an example of the class's implementation fileExampleclass.m in Objective-C:

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

See also

Related Research Articles

In object-oriented programming, a class is an extensible program-code-template for creating objects, providing initial values for state and implementations of behavior.

<span class="mw-page-title-main">Executable and Linkable Format</span> Standard file format for executables, object code, shared libraries, and core dumps.

In computing, the Executable and Linkable Format, is a common standard file format for executable files, object code, shared libraries, and core dumps. First published in the specification for the application binary interface (ABI) of the Unix operating system version named System V Release 4 (SVR4), and later in the Tool Interface Standard, it was quickly accepted among different vendors of Unix systems. In 1999, it was chosen as the standard binary file format for Unix and Unix-like systems on x86 processors by the 86open project.

<span class="mw-page-title-main">PHP</span> Scripting language created in 1994

PHP is a general-purpose scripting language geared towards web development. It was originally created by Danish-Canadian programmer Rasmus Lerdorf in 1993 and released in 1995. The PHP reference implementation is now produced by the PHP Group. PHP was originally an abbreviation of Personal Home Page, but it now stands for the recursive initialism PHP: Hypertext Preprocessor.

Cocoa is Apple's native object-oriented application programming interface (API) for its desktop operating system macOS.

<span class="mw-page-title-main">GNUstep</span> Open source widget toolkit and application development tools

GNUstep is a free software implementation of the Cocoa Objective-C frameworks, widget toolkit, and application development tools for Unix-like operating systems and Microsoft Windows. It is part of the GNU Project.

<span class="mw-page-title-main">Library (computing)</span> Collection of non-volatile resources used by computer programs

In computer science, a library is a collection of non-volatile resources used by computer programs, often for software development. These may include configuration data, documentation, help data, message templates, pre-written code and subroutines, classes, values or type specifications. In IBM's OS/360 and its successors they are referred to as partitioned data sets.

A filename extension, file name extension or file extension is a suffix to the name of a computer file. The extension indicates a characteristic of the file contents or its intended use. A filename extension is typically delimited from the rest of the filename with a full stop (period), but in some systems it is separated with spaces.

The Visual Component Library (VCL) is a visual component-based object-oriented framework for developing the user interface of Microsoft Windows applications. It is written in Object Pascal.

The C standard library or libc is the standard library for the C programming language, as specified in the ISO C standard. Starting from the original ANSI C standard, it was developed at the same time as the C library POSIX specification, which is a superset of it. Since ANSI C was adopted by the International Organization for Standardization, the C standard library is also called the ISO C library.

<span class="mw-page-title-main">Windows Forms</span> Graphical user interface software library

Windows Forms (WinForms) is a free and open-source graphical (GUI) class library included as a part of Microsoft .NET, .NET Framework or Mono, providing a platform to write client applications for desktop, laptop, and tablet PCs. While it is seen as a replacement for the earlier and more complex C++ based Microsoft Foundation Class Library, it does not offer a comparable paradigm and only acts as a platform for the user interface tier in a multi-tier solution.

In computer science, a mutator method is a method used to control changes to a variable. They are also widely known as setter methods. Often a setter is accompanied by a getter, which returns the value of the private member variable. They are also known collectively as accessors.

<span class="mw-page-title-main">XMLHttpRequest</span> Web API to transfer data between a web browser and a web server

XMLHttpRequest (XHR) is a JavaScript class containing methods to asynchronously transmit HTTP requests from a web browser to a web server. The methods allow a browser-based application to make a fine-grained server call and store the results in XMLHttpRequest's responseText attribute. The XMLHttpRequest class is a component of Ajax programming. Prior to Ajax, an HTML form needed to be completely sent to the server followed by a complete browser page refresh.

In computing, ioctl is a system call for device-specific input/output operations and other operations which cannot be expressed by regular file semantics. It takes a parameter specifying a request code; the effect of a call depends completely on the request code. Request codes are often device-specific. For instance, a CD-ROM device driver which can instruct a physical device to eject a disc would provide an ioctl request code to do so. Device-independent request codes are sometimes used to give userspace access to kernel functions which are only used by core system software or still under development.

In the Java computer programming language, an annotation is a form of syntactic metadata that can be added to Java source code. Classes, methods, variables, parameters and Java packages may be annotated. Like Javadoc tags, Java annotations can be read from source files. Unlike Javadoc tags, Java annotations can also be embedded in and read from Java class files generated by the Java compiler. This allows annotations to be retained by the Java virtual machine at run-time and read via reflection. It is possible to create meta-annotations out of the existing ones in Java.

Charm++ is a parallel object-oriented programming paradigm based on C++ and developed in the Parallel Programming Laboratory at the University of Illinois at Urbana–Champaign. Charm++ is designed with the goal of enhancing programmer productivity by providing a high-level abstraction of a parallel program while at the same time delivering good performance on a wide variety of underlying hardware platforms. Programs written in Charm++ are decomposed into a number of cooperating message-driven objects called chares. When a programmer invokes a method on an object, the Charm++ runtime system sends a message to the invoked object, which may reside on the local processor or on a remote processor in a parallel computation. This message triggers the execution of code within the chare to handle the message asynchronously.

LispWorks is computer software, a proprietary implementation and integrated development environment (IDE) for the programming language Common Lisp. LispWorks was developed by the UK software company Harlequin Ltd., and first published in 1989. Harlequin ultimately spun off its Lisp division as Xanalys Ltd., which took over management and rights to LispWorks. In January 2005, the Xanalys Lisp team formed LispWorks Ltd. to market, develop, and support the software.

Many programming languages and other computer files have a directive, often called include, import, or copy, that causes the contents of the specified file to be inserted into the original file. These included files are called header files or copybooks. They are often used to define the physical layout of program data, pieces of procedural code, and/or forward declarations while promoting encapsulation and the reuse of code or data.

Objective-C is a high-level 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 NeXTSTEP operating system. Due to Apple macOS’s direct lineage from NeXTSTEP, Objective-C was the standard programming language used, supported, and promoted by Apple for developing macOS and iOS applications until the introduction of the Swift programming language in 2014. Thereafter, its usage has been consistently declining among developers and it has often been described as a "dying" language.

Swift is a high-level general-purpose, multi-paradigm, compiled programming language developed by Apple Inc. and the open-source community. Swift compiles to machine code, as it is an LLVM-based compiler. Swift was first released in June 2014, and the Swift toolchain has shipped in Xcode since version 6, released in 2014.

<span class="mw-page-title-main">Nim (programming language)</span> Programming language

Nim is a general-purpose, multi-paradigm, statically typed, compiled high-level systems programming language, designed and developed by a team around Andreas Rumpf. Nim is designed to be "efficient, expressive, and elegant", supporting metaprogramming, functional, message passing, procedural, and object-oriented programming styles by providing several features such as compile time code generation, algebraic data types, a foreign function interface (FFI) with C, C++, Objective-C, and JavaScript, and supporting compiling to those same languages as intermediate representations.

References

  1. Alan Griffiths (2005). "Separating Interface and Implementation in C++". ACCU. Retrieved 2013-05-07.
  2. Alan Griffiths (2005). "Separating Interface and Implementation in C++". ACCU. Retrieved 2013-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. Retrieved 2013-05-07.
  5. "C++ Dos and Don'ts". The Chromium Projects. Retrieved 2013-05-07.
  6. "Introduction to C++ Classes" . Retrieved 2013-05-07.
  7. Alan Griffiths (2005). "Separating Interface and Implementation in C++". ACCU. Retrieved 2013-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. Retrieved 2013-05-07.
  9. "The implementation file in C++ Programming". ITechTalk. Retrieved 2013-05-07.
  10. "Introduction to C++ Classes" . Retrieved 2013-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" . Retrieved 2013-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" . Retrieved 2013-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" . Retrieved 2013-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.