1

i'm new to c++ (i had some time with Java and C before) i can't get it... eclipse is creating header and cpp files when i want to create some new class, why ? thing is , it seems like the header includes the class basic implantation so what does the cpp file is being used ?

header:

#ifndef HEAP_H_
#define HEAP_H_

namespace std {

class Heap {
public:
        Heap();
        virtual ~Heap();
};

} /* namespace std */
#endif /* HEAP_H_ */

cpp:

#include "Heap.h"

namespace std {

Heap::Heap() {
        // TODO Auto-generated constructor stub

}

Heap::~Heap() {
        // TODO Auto-generated destructor stub
}

} /* namespace std */

the object oriented thinking in c++ just seems weird. what's the point of an header file when you have a class file with methods ?
anyway , thx ahead!

Or Groman
  • 123
  • 1
  • 11

2 Answers2

2

The header file is where the declaration of the class is. This is done so that multiple files are able to include the Heap class.

You COULD just write the implementation in the header file by itself, but then you wouldn't be able to include it in more than one file due to the method/class/whatever being declared multiple times. This is actually true in C as well. If you create an include file:

int double_me(int number) {
        return number * 2;
}

And then include that in multiple files, the compiler will complain about the same method being declared multiple times.

That being said, the header file is where all the class functions and variables are declared. The .cpp source file is where the implementation is written.

For instance, if you wanted to add a method to the Heap class:

Heap.h:

#ifndef HEAP_H_
#define HEAP_H_

namespace std {

class Heap {
public:
        Heap();
        virtual ~Heap();
        void do_nothing(int some_parameter);
};

} /* namespace std */
#endif /* HEAP_H_ */

Heap.cpp:

#include "Heap.h"

namespace std {

Heap::Heap() {
    // TODO Auto-generated constructor stub

}

Heap::~Heap() {
    // TODO Auto-generated destructor stub
}

void Heap::do_nothing(int some_parameter) {
}

Another advantage is for reduced compile times. If a recompile was to take place, but the Heap object file was not changed, then it won't have to recompile the Heap implementation.

eaglgenes101
  • 169
  • 5
elimirks
  • 1,452
  • 2
  • 17
  • 30
  • i think i get it now! anyway the header is some sort of an interface to the class itself? did i got it right? i can't vote up your answer but i think it's clear to me what's the point of it (thx :) ). – Or Groman Dec 05 '13 at 21:40
  • Right on. You could still accept it as the answer :D – elimirks Dec 05 '13 at 21:49
1

It is pretty standard for a C++ class to have both a header file and an implementation file. The reason for this is that it is common for other functions which use the class to need to know the way in which to make calls to the class (found in the header file), while they don't need to know how those calls are actually implemented. The only functions that are implemented in a header file should be inline (which get moved into the calling function), or template functions.

In fact, if the compiler implements the same function multiple times into multiple object (*.o) files, it will come back with an error during linking. This is the same as it is with C implementation files header files. The header file contains the prototypes, and the implementation file contains the, well, implementation.

Dr.Tower
  • 985
  • 5
  • 11