-4

I been searching a lot lately about header files and stuff and i always see the same things, but i dont get something, why use the .h header file when you can just throw the code on the .cpp file?

Why do this?:

//getten.h
#pragma once
int GetTen();

//getten.cpp
#include getten.h
int GetTen(){ return 10; }

//main.cpp
#include "getten.h"
printf(GetTen());

When you can do:

//getten.cpp
int GetTen(){ return 10; }

//main.cpp
#include "getten.cpp"
printf(GetTen());

1 Answers1

0

The .h files usually contain the interface methods, and the .cpp files contain the implementations for majority of those methods. So, the header file (.h) is usually the interface for a entire unit of the .cpp file (more like an instruction file) which basically just the class definition and function prototypes.

The .cpp file would then be used to provide the implementation for all the functions and classes within that specific header.

de_classified
  • 1,927
  • 1
  • 15
  • 19
  • I think the question is not how it usually is but **why** it is how it usually is. You could mention that projects are split into multiple compile units to speed up the build process and to allow collaborative development on the same project. You could mention shared libraries and APIs, the difference between declaration and definition, internal and external linkage ... – Thomas Sablik Apr 01 '20 at 09:40