0

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

2
Contributors
3
Replies
4
Views
12 Years
Discussion Span
Last Post by Narue
0

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();
}
0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.