2

In OOP, you want to break apart a program into multiple classes.

In C#, you would do as such:

namespace a
{
  public class ClassA
  {
     //Methods...that are defined and have code in them.

  }
}

and to use that class, you just do "using namespace a;".

Say I want to create a class in C++, and define them, and put code in them.

class ClassA
{
  public:
   void methodA();
}

ClassA::methodA()
{
  //implementation.
}

To access this implementation, you would just use #include "ClassA.h". I fully understand that, and then you have to implement that code again? That seems counterproductive as I like to spread my project over a lot of classes.

So what would be the proper procedure to implement ClassA and not re-implement all it's methods again?

  • 1
    Why do you have to re-implement the code again? The implementation is either given in the `.h` file, or in a separately compiled `.cpp` file which you can link your program to. – jogojapan Oct 30 '12 at 03:04
  • Ah! So you basically implement it once, and you share the .h? Perfect, I was scared when I thought you had to #include , and do all the implementation all over again. I am starting to like C++ :D –  Oct 30 '12 at 03:14
  • Yes, you can implement everything in the `.h` file (i.e. the header file). Make sure you declare functions as `inline` if you do. Alternatively, you can put the implementation in a `.cpp` file, compile that to a library and share that. – jogojapan Oct 30 '12 at 03:19
  • See http://stackoverflow.com/questions/12671383/benefits-of-header-only-libraries and http://stackoverflow.com/questions/6200752/c-header-only-template-library for more on how to make that decision. – jogojapan Oct 30 '12 at 03:19
  • "In OOP..." the thing you describe has nothing to do with object oriented programming. It's the build model. C++ has an ancient model based around 'translation units', object files almost entirely devoid of metadata, and a linker while C# has modules. – bames53 Oct 30 '12 at 03:57

3 Answers3

3

You don't have to reimplement them in each CPP file, the C++ linker takes care of making sure the definitions get matched together.

All you need is:

A header:

#ifndef FOO_H
#define FOO_H

class Foo{
  //Junk goes here
};

#endif

A cpp:

#include "foo.h"

//implementations for junk goes here
void Foo::junk(){

}

And then you can include foo.h. Each cpp will be compiled to a .o file. Than, those .o files are handed to the linker which can figure out where definitions are and piece together the code correctly.

daniel gratzer
  • 52,833
  • 11
  • 94
  • 134
2

C and C++ have a different way of doing things. As long as you have a declaration for a class, method, or external variable the compiler will happily compile and leave off the actual definition of the methods, classes, etc, for link time. This is simplifying things a lot, but basically the compiler will leave a hint to the linker in the object file, saying that the linker needs to insert the address of the method here.

So you just need to include the "ClassA.h" file and you can compile fine.

Because of this you see some different behavior in C and C++ than you would in C#. For example, in C or C++ it's perfectly fine to have two different items (methods, variables, etc) that are named the same in different files as long as neither one is visible outside the file. Whereas in C# you would have to use different namespaces or different names. Note - not that I'm saying this is good practice, it's just possible.

David Mason
  • 1,545
  • 2
  • 14
  • 14
1

The .h header files contain the class specification. The corresponding .cpp files contain the implementation and are compiled to .o files. During development, you would include .h files to access the APIs provided by the class. During compilation/linking stage, you would include the .o files also along with your source files to form the final binary. You don't need to implement anything again, w.r.t to the class you are using.

Vikdor
  • 23,934
  • 10
  • 61
  • 84