DEV Community

Bryan Douglas
Bryan Douglas

Posted on

How to use multiple files in C++

When I was a newcomer to the C++ scene, I personally, had a fair bit of trouble trying to figure out how to keep classes separate from the main.cpp file.

So I figured I would pass my knowledge around on here to help others who are starting to learn C++ - we will use a "Person" class as an example.

Technically, you could use a single file to hold the generic "Main" class and the "Person" class - but that would make re-usability difficult to organize and understand. So it is generally a good example to keep classes as finely grained as possible, and by that, I mean a single class per file.

To give you an idea of what it would look like when you have both classes in the same file, turn your attention to the code below:

int main(int argc, char** argv) {
    std::cout << "Hello World" << std::endl;
}

class Person
{
private:
    std::string m_givenname;
    std::string m_familyname;

public:
    Person() {};
    ~Person() {};

};
Enter fullscreen mode Exit fullscreen mode

With the way our code is set up above, we can only access the class inside this one file. Not to mention Visual Studio will throw up a few errors when you attempt to use classes this way. It is inevitable that larger C++ applications will grow to be more than one file, so creating classes that can be expanded easily will be very useful later on when creating more complex software.

There are two files that need to be created (which are simple to create if using Visual Studio), those are a Header file which holds all of the predefined function prototypes - which is similar to an initial planning of the functions names and their arguments; and a CPP file which holds the guts of the functions themselves.

Using our "Person" class as an example again, I will show examples of the files below:

"Person.h" (Header file)

#include <iostream>

class Person
{
private:
    std::string m_givenname;
    std::string m_familyname;

public:
    Person();    //Constructor prototype
    ~Person();   //Destructor prototype
};
Enter fullscreen mode Exit fullscreen mode

"Person.cpp" (CPP File)

#include "Person.h"

Person::Person()
{

}

Person::~Person()
{

}
Enter fullscreen mode Exit fullscreen mode

Now, with this configuration for a class - all you need to do when you wish to use the class is to include a (class name may differ in your situation)

#include "Person.h"

Then just proceed to create an instance the way you usually would.

This prevents the class files from being loaded on every single file the software works with, which will save time and system resources, as you decide when and where to use it.

Top comments (4)

Collapse
 
codemouse92 profile image
Jason C. McDonald • Edited

As a rule, I always use *.hpp for C++ header files. It's not uncommon to find a mixture of C and C++ within a repository, and the difference in filename helps clarify that.

To put that another way:

  • *.h: C header file
  • *.c: C implementation file
  • *.hpp: C++ header file
  • *.cpp: C++ implementation file

See how clear that is?

Building on that, once you start including multiple files in your project, you should have some sort of directory structure. Here is what I use; it's far from the only way to do it, but in my experience, it seems to be one of the most effective.

project/
├── main.cpp
├── Makefile
├── include/
│   ├── classy.hpp
└── src/
    └── classy.cpp
Enter fullscreen mode Exit fullscreen mode

As a last note, use Makefiles for building most simple projects. However, as soon as you get into anything more complicated, take a look at CMake (instead of Autotools, which is far harder to learn).

Collapse
 
lilyyanglt profile image
Lily

Hi Jason! Is there any good resources you'd recommend on creating makefiles? I just learned about this today and I am fascinated by it! However, based on your suggested structure for a project, I don't know really know how to write rules in the makefile for grabbing files that are in include and src.
Any suggestion would be greatly appreciated! I am a beginner in c++.
Lily

Collapse
 
codemouse92 profile image
Jason C. McDonald • Edited

Afraid I don't have anything formal. I've kinda figured out Makefiles by experimentation and fragments of examples over the years.

If you want, you can take a look at MousePaw Media's documentation on our C/C++ build system, which combines CMake and Makefiles. The system is a bit unconventional, but it works extraordinarily well.

mousepawmedia.net/rmd/buildsystem/...

NOTE: The information on that link is a draft, and is subject to change.

Thread Thread
 
lilyyanglt profile image
Lily

Thank you for the reply Jason!! Really appreciated! I'll take a look at what you suggested :)