Implementing Classes¶
Declaration and Implementation¶
Example Header / Declaration¶
project-root/inc/Student.h
1 2 3 4 5 6 7 |
|
Example Implementation¶
project-root/src/Student.cpp
1 2 3 4 |
|
During the semester, classes can be separated into declaration and implementation.
In this case, we need to create both a .h
(header) and a .cpp
(source) file.
In the declaration, we define the class methods without implementing them - that is, we don't provide method bodies and end them with a semicolon, for example:
1 |
|
.cpp
file.
These implementations must be compiled!
When implementing the method - since it's done outside the class - we need to explicitly state which class it belongs to. This is done using the scope operator:
1 |
|
As shown, this method belongs to the Student
class.
To make sure the class declaration is available when compiling the implementation file (.cpp), we need to include the corresponding header.
1 |
|
Specifying Includes¶
During implementation, there are multiple ways to specify #include
directives.
The simplest way is to use quotation marks (" "
) and provide a relative path to the header file (.h
) from the location of the implementation file (.cpp
).
Assuming a folder structure like: project-root/ ├── inc/ ← header files │ └── Student.h └── src/ ← source files └── Student.cpp
In this case, the relative include path from Student.cpp
to Student.h
would be:
#include "../inc/Student.h"
This is very common, however, we can also specify include directories.
In this case, during the #include
directive, we can use paths relative to these include directories—even if the implementation (.cpp
) files are located in completely different places.
Specifying Include Directories (terminal / g++)¶
When compiling from the terminal, we need to tell the compiler which paths are include directories so that the #include "path"
directives in the implementation (.cpp
) files can be resolved.
This can be done using the -I
option.
Assuming the directory structure mentioned earlier, compiling from the root directory:
g++ src/Student.cpp -I ./inc
With this option, the following include directive in the implementation is valid:
#include "Student.h"
because root/inc
is an include directory, so the declaration (.h) files inside it are accessible from anywhere.
If only the root directory were specified:
g++ src/Student.cpp -I ./
then the include path must contain the inc folder, since the base search directory is root:
#include "inc/Student.h"
Specifying Include Directories (CMake / CLion)¶
CMake is a build system, more information can be found here.
For specific questions, contact via email or Discord!
Using CMake, the generated CMake file can be used, we just need to specify the include directories.
By adding the line:
include_directories(./inc)
the inc
directory located in the root folder will also become an include directory.
Using CMake from the terminal is also possible, requiring only the cmake and make programs. Further information is available via email or Discord.
Created: 2025-09-04