General question about multiple games running from one source?

So I am going to try and keep this as general as possible so as not to infringe on any plagerism rules.

I have an assignment to make 3 board games run from one source in a g++ terminal, coming from a Java background it was my intention to have one file with the games as separate classes. However as I start to try it in C++ I've realised this is next to impossible.

I believe I have to have each "class" in separate header files but how do I combine them to be selectable from one program?
It is identical to in Java, in fact. What makes you think it is impossible?
Esentially in java the structure would be

Class
{
constructor

methods
}

Main
{
running the methods
}

But it is my under standing that the methods are named where methods are in that java example but in C++ the actual coding has to be under the main. When you have three game classes as well as shared generics it becomes a little impossible
The only difference here is that the main function is not in any specific class (which makes more sense anyway). Why would the main function be in just one of your game classes? It should be independent of any of them and just start whichever game based on user request by instantiating and .play()ing it, for example.

I'm not sure what you mean by shared generics, but if you can do it in Java you can certainly do something almost identical in C++.
Last edited on
I feel like i may have communicated my second post poorly.

If it was java I would have classes for each game where it would hold the logic and the running of the game. They would have a shared Main which would point to one of the games run methods depending on user input.

Im confused when it comes to c++ because the methods for each class arent "fleshed out" within the class but underneath the main
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 ;)
Last edited on
Topic archived. No new replies allowed.