[C++] Split class and header to separated files with Visual Studio

Students have a habit of writing all their code in one single file. When you compile it, that file is huge, the program runs slowly, bugs creep in easily, and so on and so forth. So how do you split the pieces of your code into separate files?

If you don’t have a background in object-oriented programming with C++ yet, come back to this blog as soon as you are ready.

1. What you shouldn’t do

Putting a class in its own file forces you to include the same library several times: in the separate file and in the main program

#include "stdafx.h"
#include "iostream"
using namespace std;

2. What you should do

You don’t have to do that. Including the same library twice causes errors.

Instead, in the class header file, add the following:

#ifndef CLASSNAME_H
#define CLASSNAME_H

the libraries you need

your code

#endif

Then, in the main function, include the header.h file you just created

3. Example

To make it easier to picture, here are a few screenshots

04-11-2011 7-41-49 PM

Declaring the class in PhoneNumber.h

04-11-2011 7-44-18 PM

Then, in the file that defines PhoneNumber’s member functions, we include the PhoneNumber.h file

04-11-2011 7-45-44 PM

Finally, include it in the Main function. The program will run smoothly

There is another, newer and simpler way (but it requires Visual Studio 2005 or later as the compiler)

#pragma once

Add this line at the top of the .h file. It tells the compiler to include the libraries only once.

4. Splitting a class into its own file

To illustrate, I’ll create a simple MyInt class that represents an integer

First, start Visual Studio

04-01-2012 10-57-58 AM

Pick the Project type you want, give it a name and hit OK

04-01-2012 10-59-17 AM

Hit Finish in the Project creation Wizard

04-01-2012 11-04-33 AM

Then Visual Studio (VS)

creates a new project for you, with the name you gave it and stdadx.h as the precompiled header

04-01-2012 11-08-05 AM

To add a new class the usual way, right click on Header Files, pick Add > New Item…

04-01-2012 11-11-10 AM

Pick Header File

04-01-2012 11-12-04 AM

Give it a name and hit Add

04-01-2012 11-12-53 AM

A brand new file named MyInt.h is created. This file is where the MyInt class is declared. You could also define the class in this file, but that is not recommended

// MyInt class declaration

#prama once

#include "stdafx.h"

class MyInt
{
private:
    int data;

public:
    MyInt();
    MyInt(int);
};

As you can see, we use the #pragma once technique to avoid including libraries too many times. In the same way, Add a new MyInt.cpp file in the Source Files folder

04-01-2012 11-18-01 AM

04-01-2012 11-18-17 AM

04-01-2012 11-18-38 AM

04-01-2012 11-19-08 AM

Then define the fields and functions of the MyInt class in the MyInt.cpp file

// MyInt class definition

#include "stdafx.h"
#include "iostream"
using std::cin;

#include "MyInt.h"

MyInt::MyInt()
    : data(0)
{
}

MyInt::MyInt(int re)
    : data(re)
{
}

On line 7, because the MyInt class is declared in the MyInt.h header file, you have to include it in order to use it.

That’s it. There is another way to create a class in its own file using the VS Wizard. In the Header Files folder, right click and pick Add > Class…

04-01-2012 11-27-20 AM

Pick CPP class then hit Add

04-01-2012 11-28-47 AM

In the Wizard that shows up, you just type the class name, and the header file and source file names are filled in automatically

04-01-2012 11-29-46 AM

Hit Finish and right away 2 new files are created, with code already in them, placed in the matching folders

04-01-2012 11-33-11 AM

The generated code in the MyInt2.h header file

04-01-2012 11-32-08 AM

The generated code in the MyInt2.cpp source file

04-01-2012 11-35-12 AM

So everything we did by hand, VS does automatically — but it also generates some redundant code. Many developers don’t like that; they still prefer creating their class files themselves.

5. About using Namespaces

As mentioned above, when doing object-oriented programming with VC++ 2010, students usually #include a lot of unnecessary libraries and then write using namespace std;. That is not needed. The std namespace, that is STANDARD, has plenty of built-in functions you never use. Declare only the functions you actually use. For example, in the picture above I use cout, cin and endl, so I only declare those 3.

#include "stdafx.h"
#include "iostream"
using std::cout;
using std::cin;
using std::endl;

These 3 functions live in the iostream library. Some commonly used libraries:

  • stdafx.h – the standard one for VC++ 2010
  • iostream – holds cout, cin, endl
  • string – holds the string handling functions
  • iomanip – holds the output spacing functions (setw, serfill)
  • conio.h – holds getch(). This function is often used to pause the screen. But you shouldn’t use it.