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.