Can someone explain this to me once and for all? I keep getting different answers everywhere I look. What is the correct way to separate the class declaration from it's member functions? The way I am doing it now is that the declaration and all of the member functions are in a .h file. Supposedly, the declaration should be in a .h and the member functions should be in a .C file? So in that case, would you include your .h file in that .C file? Then what? do you include the .C file in whatever file with the main function is or do you compile it all together at the command line. I'm using g++ if that makes a difference.

Thanks,
Matt

Recommended Answers

All 3 Replies

The class is declared in a header file:

// c.h
#ifndef C_H
#define C_H

class C {
  void foo();
};

#endif

The definitions for the class are in an implementation file:

// c.C
#include "c.h"

void C::foo()
{
  //...
}

Files that need to use the class include the header file:

#include "c.h"

int main()
{
  C test;

  test.foo();
}

so then do you compile c.C along with the one that contains the main function? i.e. g++ foo.C c.C -o foo

Yes, all of the .C files get compiled and linked together.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.