3

Someone asked a question similar to mine here: Function declaration inside or outside the class?

Why do you just declare a function inside a .h file but not the definition, and 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.

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 file and maybe a couple of .cpp files?

Community
  • 1
  • 1
  • When you take an operating system design and a compiler design class in school, and learn how the linker works, you will lean the answer to your question. – Sam Varshavchik Mar 09 '17 at 02:42

1 Answers1

8

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.

Community
  • 1
  • 1
ssell
  • 6,429
  • 2
  • 34
  • 49
  • What's the state of play with modern C++ compilers avoiding code bloat when the functions defined in the class are inlined, even if they're uncomfortably big? Do they handle the complexities, or is that just an old-fashioned concern? – Jonathan Leffler Mar 09 '17 at 03:50
  • 1
    @JonathanLeffler To be honest, I do not have a good answer for your question regarding modern compilers. Perhaps that would make a good question itself to see if someone with greater expertise could weigh in. – ssell Mar 09 '17 at 04:16