-1

I'm new to c++ and I have 'actually' learned a lot these past days. I just learned how to link multiple files today or what I think is called, using "headers".

My question is related to headers and linking files.

1. I was taught that you are supposed to "define" classes, structures, or functions in the header file. Then in the corresponding c plus plus file you set the definitions? Here is what I am doing, and here's where I read to do it : http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1044842972&id=1043284392

Header.h file code

#ifndef HEADER_H
#define HEADER_H
class simple_math{
public:
    int add( int, int);
     int subtract( int, int);
};

#endif

extras.cpp file code

#include "header.h"

int math::add( int x, int y){
    return (x + y);
};

int math::subtract( int x, int y){
    return( x - y );
};

Why don't we just do everything in the actual .cpp file? Why do we post the declarative code in the header file, then create (excuse me if I'm using wrong terms) prototypes in a separate .cpp file?
It seems kind of odd to me. That some how these functions or class methods get defined in a .cpp file that isn't even "#included"(I guess) in the header. Instead, the header is included in the .cpp file and the code still gets executed properly.

Cayman
  • 19
  • 3
  • Note that your class doesn't encapsulate any state; you could have used a `namespace` for the two functions instead. Also, your source file should be referencing `simple_math::add()` and not `math::add()`; something doesn't tie up there. The 'possible duplicate' question does cover why you use header files — basically, header files are shared between source files, and make sure the source files are using the same definition of shared information. – Jonathan Leffler Jul 20 '13 at 02:43
  • That was my bad, I changed the main "math" to "simple_math" when posting on here. I didn't actually have that executed. – Cayman Jul 20 '13 at 02:49

1 Answers1

2

The header file declares what is available.

It is saying to the compiler that you have access to these classes/methods/functions and here is how they can be used (for checking).

The .cpp file actually contains the code to produce the functionality. You only need to compile this once whereas various other .cpp files need to know how to use it. There is where the header file comes in. another .cpp file can include the header file and then know what is available.

Now comes the linking stage:

You have lots of object files. Some (if not all) have been told there are various functions/methods/classes available to them but they do not contain the machine code to execute them - as they exist elsewhere. Linking does some knitting and resolves these dangling references to functions etc in other objects to produce an executable.

Of course this breaks down a little for templates - but that is another post.

BTW:

It is customary to call header.h as simple_math.h and extras.cpp as simple_math.cpp.

Also int math::add should be simple_math:add. Ditto with the other method.

Ed Heal
  • 59,252
  • 17
  • 87
  • 127