Why do you just declare a function inside a .h
file but not the definition, then write the whole definition in a .cpp
file later? What's the point? Why don't you just put the whole function inside a class inside the header file instead of just declaring it? It seems repetetive and pointless.
When you modify a header, all code that references the contents of that header via #include
must be re-compiled.
If you have a header that is included by many other files, then a single change to the contents of that header may require a rather lengthy rebuild of your entire project. This may not seem important if you are only working on small projects, but imagine production applications with thousands of classes.
Another benefit is when writing a library. When you provide your library binaries for others to use, you need only provide the headers. In this way you can have proprietary source code within the source files themselves that the users do not have access to.
Finally, by separating content into separate header and source files, it makes learning a library easier. Many times when working with a new API I just want to skim the header to get a gist of the class and it's functions. I do not need to see, or want to parse through, the implementation.
This is not to say that you have to have separate header and source files. There are many header-only libraries out there (such as GLM) that serve a wide audience. Of course, these are typically distributed as header-only so that no binary (static or dynamic) is needed.
And of course, if you are creating a template class the implementation generally goes within the header.
How do I structure a program to use the header files and other .cpp
files without piling functions, classes, and variables into the main file? Could I get an example of a simple program using a .h
and a couple of .cpp
files.
This is perhaps beyond the scope of this site, but here is a simple example.
Rectangle.hpp
#ifndef H__RECTANGLE__H
#define H__RECTANGLE__H
class Rectangle
{
public:
Rectangle(float width = 0.0f, float height = 0.0f);
~Rectangle();
void setWidth(float width) noexcept;
void setHeight(float height) noexcept;
float getArea() const noexcept;
protected:
private:
float m_fWidth;
float m_fHeight;
};
#endif
Rectangle.cpp
#include "Rectangle.hpp"
Rectangle::Rectangle(float const width, float const height)
: m_fWidth{ width },
m_fHeight{ height }
{
}
Rectangle::~Rectangle()
{
}
float Rectangle::getArea() const noexcept
{
return m_fWidth * m_fHeight;
}
void Rectangle::setWidth(float const width) noexcept
{
m_fWidth = width;
}
void Rectangle::setHeight(float const height) noexcept
{
m_fHeight = height;
}
main.cpp
#include "Rectangle.hpp"
#include <iostream>
int main()
{
Rectangle rectA{ 5, 5 };
Rectangle rectB{ 3, 2 };
std::cout << "Area of Rectangle A is: " << rectA.getArea() << std::endl;
std::cout << "Area of Rectangle B is: " << rectB.getArea() << std::endl;
return 0;
}
Now how to compile and link these files together depends on the IDE you use.