0

I have 3 classes that derive from one another - GameScreen is the base class to which MenuScreen is derived from. I then have a third class 'TitleScreen' which derives from 'MenuScreen'.

The flow is basically from the base class: 'GameScreen' -> 'MenuScreen' -> 'TitleScreen'

The base class 'GameScreen' has no parameters in it's constructor, like wise with 'TitleScreen', however I need a parameter for 'MenuScreen'. I currently have the header files as:

GameScreen.h

class GameScreen
{
public:
    GameScreen();
}

MenuScreen.h

class MenuScreen : public GameScreen
{
public:
    MenuScreen(std::string title);
}

TitleScreen.h

class TitleScreen : public MenuScreen
{
public:
    TitleScreen(std::string title) : MenuScreen(title);
}

What I'm having difficulty trying to understand is if this is possible in C++ (I'm following a C# sample for Game State Management which does this). Reading through class inheritance in some books I have it only covers parameters inherited from the base class, were as my sample base class has no parameters.

Shane Smith
  • 117
  • 4
  • 11
  • 3
    Yes, it works exactly as you have written - except that you should call the MenuScreen constructor in the actual implementation, not in the declaration of the constructor. – jlahd Jul 26 '13 at 18:50

1 Answers1

1
  1. You are missing ; after each class declaration.

  2. If you write TitleScreen(std::string title) : MenuScreen(title) you are defining the body of the method but the body is missing... so you should put just declaration to your TitleScreen.h :

    class TitleScreen : public MenuScreen
    {
    public:
        TitleScreen(std::string title);
    };
    

    and then place the body of the constructor to TitleScreen.cpp:

    #include "TitleScreen.h"
    
    TitleScreen::TitleScreen(std::string title) : MenuScreen(title)
    {
        // ..
    }
    

Edit: fixed the terminology accordint to this question.

Community
  • 1
  • 1
nio
  • 5,141
  • 2
  • 24
  • 35
  • You are confusing declaration with definition terminology. And it is also worth mentioning that both can go together as in C#, or separately as in your example, possibly with pros and cons. –  Jul 26 '13 at 18:56