I'm seeing if I can do without tinkering with header files in C ++ projects. Probably they were the sea of good for C in the '60s, but actually today it is difficult to justify their existence in C ++.
I wrote a small program that consists of four files: main.cpp, file1.hpp, file2.hpp and file3.hpp Each header file has a class defined inside and this class consists of only one method. The characteristic is that in these headers there is the definition of the class, not just the declaration as usual.
The dependency between headers and the main can be seen in the following diagram:
And the sources are:
/*************/
/* file3.hpp */
/*************/
#pragma once
class HaceAlgo3
{
public:
// Estos atributos no se usan. Sólo están porque sí.
int iA;
float fB;
public:
float calcula(float a, int b)
{
return a*b;
}
};
/*************/
/* file2.hpp */
/*************/
#pragma once
#include "file3.hpp"
class HaceAlgo2
{
public:
float calcula(float a, float b)
{
HaceAlgo3 ha;
return ha.calcula(a,3) * b;
}
};
/*************/
/* file1.hpp */
/*************/
#pragma once
#include "file2.hpp"
class HaceAlgo1
{
public:
int calcula(int a, int b)
{
return a*b;
}
};
/************/
/* main.cpp */
/************/
#include <iostream>
#include "file1.hpp"
#include "file3.hpp"
using namespace std;
int main()
{
float fRta;
HaceAlgo2 ha2;
fRta = ha2.calcula(15.0, 20.0);
printf("%f\n", fRta);
HaceAlgo3 ha3;
fRta = ha3.calcula(300.0, 2);
printf("%f\n", fRta);
return 0;
}
In main.cpp, the #include file3.hpp line is unnecessary, but when it is, and there are no redefinition errors and compile correctly, it would show in principle that programming in this way would not cause major problems.
Could there be some kind of error when working in this way on projects of several dozen files? Thank you very much for any response.