Creating a C++ reusable Header File and its Implementation Files
Last Updated :
08 Oct, 2021
Reusability is one of the most important concepts of Software Engineering. Reusability means developing code that can be reused either in the same program or in different programs. C++ allows reusability through inheritance, containership, polymorphism, and genericity. But, there is another way to define independent building blocks. This can be achieved by creating header files and implementation files.
Header files are the files that include the class declaration. The name of the class is generally the same as that of the header file. (For example, a LinkedList class will be stored inside a LinkedList.h header file)
On the other hand, the implementation file consists of the function definition of the class which was defined inside the header file. Generally, the file name is the name of the class, with a .cpp extension. (For example, a LinkedList class's function definition will be stored inside a LinkedList.cpp header file)
Now, to create an object of the class, defined in the above header file, there must be a main() function. But wait, where to define a main() function, particularly, which file?
The main function is defined inside another file, known as the driver file, or, in some cases, the client file.
Example: Here, complexNum class is implemented. It is split up into two files. The header file has the extension.h and contains the class definitions.
Header File:
C++
// Header file complexNum.h
#ifndef COMPLEXNUM_H
#define COMPLEXNUM_H
class complexNum {
private:
int real;
int imaginary;
public:
// With default value,
// default constructor
complexNum(const int a = 0,
const int b = 0);
// setter function
void setNum(const int a,
const int b);
// Prints the complex number
// in the form real + i(imaginary),
// i->iota
void print() const;
// An overloaded operator to compare
// two complex number objects
bool operator==(const complexNum&);
};
#endif
Implementation File:
C++
// Implementation file
// complexNum.cpp
#include "complexNum.h"
#include <iostream>
using namespace std;
// A default constructor
complexNum::complexNum(int a,
int b)
{
real = a;
imaginary = b;
}
// A function to set values
void complexNum::setNum(const int a,
const int b)
{
real = a;
imaginary = b;
}
// A function to print the complex
// number in the form real +
// (imaginary)i, i->iota
void complexNum::print() const
{
cout << real << " + " << imaginary << "i" << endl;
}
// An overloaded operator to
// compare two complex Number
// objects
bool complexNum::operator==(const complexNum& obj)
{
if (this->real == obj.real && this->imaginary == obj.imaginary) {
return true;
}
return false;
}
Now to check for correctness and to implement the above complexNum class, there is a need for a driver file. Below is the driver file:
C++
// Driver file to illustrate
// the implementation of
// complexNum.cpp file
#include "complexNum.h"
#include <iostream>
using namespace std;
// Driver code
int main()
{
// Defines a complex number
// object (obj1 = 4 + 5i)
complexNum obj1(4, 5);
complexNum obj2;
// Defines a complex number
// object (obj2 = 3 + 4i)
obj2.setNum(3, 4);
// Prints the complex number
obj1.print();
obj2.print();
// Checks, if two complex
// number objects are equal or
// not
if (obj1 == obj2) {
cout << "Both the numbers are equal" << endl;
}
else {
cout << "Numbers are not equal" << endl;
}
return 0;
}
Output:
Note:
The header, implementation as well as driver files should be in the same folder. Otherwise, provide the link of the present working directory in the include statements.
Header files are already used by programmers, which are very useful, and become handy while implementing various data structures and algorithms. For example, dynamic arrays can be implemented using <vector> header file. Advantages Of Storing Class Definition In Different Files:
- Inheritance can be used to achieve code reusability, but the drawback of it is that a class has to be inherited from a class inside the same file. One cannot inherit a class from a different file.
- But, this issue is resolved by using the header and implementation files, hence making the class reusable.
- If the class implementation doesn’t change, then there is no need to recompile it.
Similar Reads
Implementation of file allocation methods using vectors
Prerequisite: File Allocation Methods Different File Allocation methods: 1. Contiguous File Allocation Methods: This is a type of allocation in which a file occupies contiguous blocks of a given memory. This type of allocation is fastest because we can access any part of the file just by adding it t
15+ min read
How to Implement User Defined Shared Pointers in C++?
shared_ptr is one of the smart pointer introduced as a wrapper to the old raw pointers in C++ to help in avoiding the risks and errors of raw pointers. In this article, we will learn how to implement our own user defined shared pointer in C++. What is shared_ptr in C++? A std::shared_ptr is a contai
5 min read
How to Call a Virtual Function From a Derived Class in C++?
In C++, virtual functions play an important role because they allow the users to perform run-time polymorphism. While dealing with inheritance and virtual functions, it is very crucial to understand how to call a virtual function from a derived class. In this article, we will learn how to call a vir
2 min read
C++ Program to Read Content From One File and Write it Into Another File
Here, we will see how to read contents from one file and write it to another file using a C++ program. Let us consider two files file1.txt and file2.txt. We are going to read the content of file.txt and write it in file2.txt Contents of file1.txt: Welcome to GeeksForGeeks Approach: Create an input f
2 min read
How to Declare a Static Variable in a Class in C++?
In C++, a static variable is initialized only once and exists independently of any class objects so they can be accessed without creating an instance of the class. In this article, we will learn how to declare a static variable in a class in C++. Static Variable in a Class in C++To declare a static
2 min read
How to Open and Close a File in C++?
In C++, we can open a file to perform read and write operations and close it when we are done. This is done with the help of fstream objects that create a stream to the file for input and output. In this article, we will learn how to open and close a file in C++. Open and Close a File in C++ The fst
2 min read
How to Create a Pure Virtual Function in C++?
In C++, pure virtual functions are those functions that are not implemented in the base class. They are instead implemented in the derived classes if necessary. In this article, we will discuss how to create a pure virtual function in a class in C++. How to Create a Pure Virtual Function in C++? To
2 min read
How to Redirect cin and cout to Files in C++?
In C++, we often ask for user input and read that input using the cin command and for displaying output on the screen we use the cout command. But we can also redirect the input and output of the cin and cout to the file stream of our choice. In this article, we will look at how to redirect the cin
2 min read
C++ Program to Read and Print All Files From a Zip File
In this article, we will explore how to create a C++ program to read and print all files contained within a zip archive. What are zip files? Zip files are a popular way to compress and store multiple files in a single container. They use an algorithm to decrease the size of the data they store. They
3 min read
How to Create a Stack of User-Defined Data Type in C++?
In C++, we have a stack data structure that follows the LIFO (Last In First Out) rule and allows operations such as push, pop, and top at one end of the structure. In this article, we will learn how to create a stack of user-defined data types in C++. Example: Input://user defined data typestruct Po
2 min read