source and header file use

i am trying to train myself to put functions, classes, etc into separate source and header files as second nature, but i want to check i am arranging things correctly.
the following example is a project i put together with various examples of classes, functions and enums in their own files just to get a feel for how a project should look.

everything runs perfectly but i need to know if this is good practice.


addAndMultiply.h
1
2
3
4
#pragma once

int addAndMultiply(int a, int b, int c);

classExample.h
1
2
3
4
5
6
7
8
9
10
11
#pragma once

class classExample 
{ 
private:   
	int num;  
public:   
	classExample(int n);   
	classExample();   
	int getNum(); 
};

enumExample.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#pragma once

enum EnumExample
{
	E_ONE = 1,
	E_TWO = 2,
	E_THREE = 3,
	E_FOUR = 4,
};

enum class EnumClassExample
{
	EC_ONE = 1,
	EC_TWO = 2,
	EC_THREE = 3,
	EC_FOUR = 4,
};

maths.h
1
2
3
4
5
#pragma once

int add(int a, int b);
int multiply(int a, int b);
int plusClassExample(int a);

addAndMultiply.cpp
1
2
3
4
5
6
7
#include "maths.h"

int addAndMultiply(int a, int b, int c)
{
	int addResult = add(a, b);
	return multiply(addResult, c);
}

classExample.cpp
1
2
3
4
5
6
7
8
9
#include "classExample.h" 

classExample::classExample(int n = 0): num(n) {} 
classExample::classExample(): num(0) {} 

int classExample::getNum() 
{  
	return num; 
}

fileStructurePractice.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>				//for i/o stuff

#include "maths.h"				//for math functions
#include "addAndMultiply.h"		//for math function that calls other function
#include "classExample.h"		//for class "classExample"
#include "enumExample.h"		//for enum and enum classes

int main()
{
	int a{ 10 };										//initialize a = 10
	int b{ E_TWO };									//initialize b = 2 using enum
	int c{ static_cast<int>(EnumClassExample::EC_THREE) };	//initialize c = 3 using enum class

	std::cout << addAndMultiply(a, b, c) << '\n';			//call addAndMultiply > add
	classExample EG{ 3 };							//call classExample "EG" and set to 3		
	std::cout << EG.getNum() << '\n';					//return classExample "EG"
	std::cout << plusClassExample(2) << '\n';			//call maths function

	return 0;
}

maths.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "classExample.h"

int add(int a, int b)
{
	return a + b;
}

int multiply(int a, int b)
{
	return a * b;
}

int plusClassExample(int a)
{
	classExample x{ 3 };
	return x.getNum() + a;
}
Last edited on
This is pretty good.

While not strictly necessary, maths.cpp should include maths.h and addAndMultiply.cpp should include addAndMultiply.h. This will increase the chances of catching an error if the functions doesn't match the prototype.

I would not have a separate addAndMultiple.h & .cpp. Combine them with maths.h

thanks for the reply.
i will be sure to include headers in my source files.

i agree the addAndMultiply should be with the maths functions, i was just keeping things separate so i could call a file from another file to make sure things worked.


now i have to "convert" a project i have already started and move functions into logical files etc :/ should be tedious!

thanks for the help
i have another question about this.
if i include something like <iostream> in my files, should i include it in the header file or the source file?
If i include something like <iostream> in my files, should i include it in the header file or the source file?

The goal is to avoid transitive dependencies. If you need something, include it in the same file. Aim to include as little as possible, particularly in headers.

In each file, include only what is required. For example, given a function declaration in print.hpp
1
2
#include <string_view>
void print(std::string_view s);

And its corresponding definition in print.cpp
1
2
3
4
#include "print.hpp" // include the corresponding header as the first substantive line 
#include <string_view>
#include <iostream> 
void print(std::string_view s) { std::cout << s << '\n'; }
thanks! that makes sense.
this is all coming together now. i have converted my project so that everything is nicely organised into source and header files, but i will check over my includes and make sure everything is included only where it is needed.

thanks everybody for the help <3

EDIT:
this is pretty much solved now, but since the topic is going i hope you guys dont mind me asking more questions?

when it comes to reusing source and header files, what is the typical practice for doing that?
i assume it would be IDE dependant? (i use visual studio).
my guess is i just load the .cpp and.h file into my project from wherever i have stored them? so if i want to add something to a library of modules i want to use later i would typically store the .cpp and .h files in my own built file directory type library?

i hope that makes sense
Last edited on
If you plan to reuse functions in .cpp files, ideally you would create your own library and store the compiled version of the .cpp's there. If this turns out to be too much trouble then just copy the .cpp files as you said.
thanks. i assume creating a library is dependant on IDE, so i would need to consult visual studios documents for info on doing that. i should be able to find that online now i know what i am looking for.

thanks everyone very much for the help! <3
Last edited on
Topic archived. No new replies allowed.