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();
};
#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;
}
#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;
}
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!
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
#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?
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.