You can define classes in C++ the same way as in Java. I have no idea why so many sources teach "declare above main, define below main", because it gives the wrong idea.
When you define a class, like this, it is called defining it inline:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
struct MyClass
{
MyClass()
{
//ctor
}
~MyClass()
{
//dtor
}
void DoStuff()
{
//...
}
private:
//...
};
|
Normally, this would be in a header file.
When you define a class like this, it conventionally takes two files: the header and the source file. Header:
1 2 3 4 5 6 7 8 9 10
|
struct MyClass
{
MyClass();
~MyClass();
void DoStuff();
private:
//...
};
|
Source file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
#include "MyClass.hpp"
MyClass::MyClass()
{
//ctor
}
MyClass::~MyClass()
{
//dtor
}
void MyClass::DoStuff()
{
//...
}
|
You can also mix the two and define some functions inline and others not.
Also note that you do not have to have everything in the same place as main - in the source file with main you can just include the headers (like import statements almost) and then compile all the files together.
As an analogy, headers in C++ are like interfaces in Java, and source files in C++ are like concrete classes in Java. Just don't take this analogy literally ;)