Maintaining Classes in Separate Files in C++

Written by Hamza on. Posted in C++

I previously mentioned in Basics of Object Oriented Programming that the main body of any class that we write in our program is usually written in a separate file called .cpp file and its declarations in another header file .h file. This is done in order to avoid clustering of the class definition with the execution body of the program. It makes it pretty easy and efficient to put all the classes in separate files in a large program. This is then included later in a main file and its functions are used in the manner we like. Here is the detailed procedure for it. 

The header files hold the class declarations, i.e, all data members and member function prototypes. The implementation or definition of the member functions, as a good programming practice, are written in the .cpp file. In this article, I will be explaining step by step  how to add classes separately as header files and .cpp files in your C++ program.

If you are using Visual Studio or Codeblocks or any such text editor and compiler, there is usually an option to add a class into your program. For this article, I will be showing you how to add a class in Microsoft Visual Studio 2010.

Firstly after creating your project, right click in your programs text editor and click on ‘Add->Class’, as shown below:

creating a class


After clicking on the C++ class option, this following window will appear. Add in the relevant details about the class name. I have chosen the class name to be ‘test_class’.


creating a separate class


Instantly, Visual Studio splits the class into a .h and a .cpp file. The .h file will hold the main body, with the data members and member function prototypes. I have added in some dummy class members and functions for our examples sake.

(Note: Visual Studio also includes the default constructor and destructor  but I have deleted this as I have not yet  dealt with these concepts in my series. Watch out for it in the next article however!)

Header File of a class


The statement at the beginning, always include them. You won’t get it until we reach inheritance, so better use them as it is now. As you can see, our class consists of two data members and two functions that respectively return the two variables. The implementation of these functions will be in the .cpp file, as shown below:

Definition File of a class


Notice the highlighted ‘#include’ statement which includes the class header file. This is also done automatically after creating a class in Visual Studio. One more thing to notice is the ‘ :: ‘ operator. This is scope resolution which operator specifies the class these functions belong to, and is used at the start of the function implementation. The syntax followed is:

<p>&lt;return_type &gt;&lt;name_of_class&gt; :: &lt;name_of_function&gt; (parameters)</p>
<p>.   //implementation of the function;</p>

This should be kept in mind whenever writing a class separately in a .cpp file.

In your main file, just include this class header file and you can access its code. This is a relatively simple way of getting started on making classes. No matter if you are using any text editor or compiler, the steps to make a class will almost always be exactly the same as those I have described above. Even if you are using a simple Notepad editor, you can simply make two separate .h and .cpp files and manually do the include and get started that way.

Tags: , , , ,