-4

I am learning C++ at the moment, and I came across a lot of examples where classes are declared differently than I'm used to in for example C# or Java. The following thing isn't clear to me:

When defining a class in C#, you code the methods that you are going to use entirely within the class, like so:

public class example{
 public example(){}

public void doSomething(){
 Console.WriteLine("This is something");
}

}

The method "doSomething()" can be accessed by instantiating a new object, and calling object.doSomething();. All clear to me.

Why do people do the following in C++?:

class example{

private: 
 int _thingy;

public:
 void doSomething();

 }

#include <iostream>

example::doSomething(){
 std::cout << _thingy;
}

int main(){
 example x;
 x.doSomething();
 system("pause");
}

Isn't this bad when you are going to reuse the class in other methods?

lucster
  • 168
  • 8
  • Thats because class definitions are usually placed in a header file (.h), whilst the implementation is defined in the .cpp file. [Related](https://stackoverflow.com/questions/1305947/why-does-c-need-a-separate-header-file) – Tom Sep 24 '20 at 10:28
  • @Tom And when reusing class methods, will you include a cpp file with implementations of methods within the class? So when you use the class methods in multiple files – lucster Sep 24 '20 at 10:31
  • In C++ you *can* define (implement) member function inline inside the class definition as well. But splitting declaration of functions and definition of them helps with [separation of concern](https://en.wikipedia.org/wiki/Separation_of_concerns). – Some programmer dude Sep 24 '20 at 10:32
  • @lucster Please read [this](https://stackoverflow.com/questions/1686204/why-should-i-not-include-cpp-files-and-instead-use-a-header) – Tom Sep 24 '20 at 10:36

1 Answers1

3

For this issue you need to learn a few new concepts...

The first is about how C++ programs are built. A C++ program can contain many independent source files. Each source file is compiled independently of each other, into object files. Then all the object files are linked into the final executable program file.

Then there's the separation of the C++ code into source and header files. A header file contains declarations and class definitions. One typically places the class definition inside a header file. The definition (the implementation) of functions are put in one or more source files.

Then finally how source and header files fit together, and for this you need to learn about translation units. A translation unit is basically a single source file with all the included header files. This is the basic unit that the actual compiler deals with.

And now for the "magic" part: To be able to call a function, the compiler only needs to know that it exist, i.e. it only needs the declaration, not the definition. It is up to the linker which takes each separate object file (which is the result of compiling a translation unit) to resolve the definitions.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621