Classes: duplicated outputs & an uncertain function?

Hi all again,

I'm now in my 3rd week of my C++ course and I have a few little issues I need explained to me. The Hangman game in question is NOT supposed to be functional yet. It's an generic exercise in classes in which Hangman will be MY final project come the 9th week. So, I'm getting an early start.

Note: it compiles without errors but it's just missing certain 'cout' outputs to console.

Questions and Concerns

1. Why doesn't my 'char Menu::getSelection()' from my menu.cpp not 'cout' not display to console? That's all I need it to do, for now.

2. Unsure if my 'showHiScore()' (which I hard-coded for display purposes only) is written correctly. I don't even get my hard-coded high score, which is 1000. Any advice in this will be greatly appreciated.

3. This one is just a nuisance, really: When the console program runs, I get "class Menu - Inside of 'Menu' default constructor" displayed TWICE. Why does it appear twice when the Menu constructor (and derived class constructor) is defined only once within menu.cpp?

Please take a look at my code:


Menu.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#ifndef Menu_h
#define Menu_h

#include <iostream>

using namespace std;

class Menu
{

public:

     Menu();
    //Default Constructor

    void showTitle();
    //Displays name of game

    void showMenu();
    //Displays menu

    void showSelection();
    //Will show user selections such as New Game and Load Game (not yet implemented)

    char getSelection()const;
    //Get user selection (accessor - getter)

    void setSelection(char);
    //Sets user selection (mutator - setter)

    char sel;
    //variable to hold user selection
};
#endif //Menu_h 


Menu.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include "Menu.h"
#include <iostream>


using namespace std;

Menu::Menu()                      
{
    cout <<"class Menu - Inside of 'Menu' default constructor\n" <<endl;
}

void Menu::showTitle()
{
    cout <<"\n\nclass Menu showTitle() H-A-N-G-M-A-N" <<endl;
}

void Menu::showMenu()
{
    //showMenu() definition
}

void Menu::showSelection()
{
    cout <<"\n\nclass Menu showSelection() - Please enter your selection: " <<endl;
    cout <<"A." <<endl;
    cout <<"B." <<endl;
    cout <<"C." <<endl;
    //showSelection() definition
}

char Menu::getSelection() const
{
    cout <<"\nclass Menu - getSelection() - return the user's selection";
    return sel;
    //getSelection() definition
}

void Menu::setSelection(char sel)
{
    char selection = sel;
    //Stores user's selection in 'sel' variable. 
   //Declares 'selection' as stored variable 'sel'
}


Derived.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef Derived_h
#define Derived_h

#include "Menu.h"
#include <iostream>
#include <string>

using namespace std;


class Derived: public Menu
{
public:

    Derived();
    //Default constructor

    void showMenu();
    //Overrides showMenu() from base class 'Menu'

    void showHiScore(int); 
};
#endif 


Derived.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "Derived.h"
#include <iostream>

using namespace std;

Derived::Derived()
{
    cout <<"class Derived - Inside the 'Derived' constructor" <<endl;
}

void Derived::showMenu()
{
     cout <<"\n\n\t\tCLASS DERIVED (overridden)Menu: showMenu()" <<endl;
     cout <<"\t\t******************************************" <<endl;
     cout <<"\n<---- Menu under construction at this time---->" <<endl;
     //showMenu() overridden 'base class' definition
}

void Derived::showHiScore(int num)
{
     cout <<"\nInside the Class Derived showHiScore()" <<endl;
     int HiScore = 1000;
     cout <<"\n\nThe High Score is: "<<HiScore<<endl; 
}


Main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include "Menu.h"
#include "Derived.h"
#include <iostream>
#include <string>

using namespace std;

int main()
{
     Menu class1;
     //Creates 'class Menu' object
	
     Derived class2;
     //Creates 'class Derived' object

     class1.showTitle();

     class2.showMenu();
     //Displays overridden menu

     class1.showSelection();
     //Show user selection from class Menu

     class1.getSelection();
     //Retrieves user selection

     char sel;
     //Declares 'sel' variable

     cin >> sel;
     //Stores user selection in 'sel'

     class1.setSelection(sel);
     //Sets user selection from stored variable in 'sel'

     class2.showHiScore(1000);
     //class Derived - puts HiScore listed as 1000 for the purposes of this exercise

return 0;
}


Any suggestions for correcting my questions / concerns would be greatly appreciated.





3. This one is just a nuisance, really: When the console program runs, I get "class Menu - Inside of 'Menu' default constructor" displayed TWICE. Why does it appear twice when the Menu constructor (and derived class constructor) is defined only once within menu.cpp?


Lines 10 and 13 of Main.cpp create two different Menu objects. The code on line 10 will call the Menu constructor, which I'm sure you know already. In addition, although the 'class2' object is of type Derived, it inherits from type Menu, and thus will execute the Menu constructor too (before the Derived constructor). This explains the two identical lines in your output.

With respect to your first two questions, I'm not getting the same behaviour. I ran your code above and got the following output:


class Menu - Inside of 'Menu' default constructor

class Menu - Inside of 'Menu' default constructor

class Derived - Inside the 'Derived' constructor


class Menu showTitle() H-A-N-G-M-A-N


                CLASS DERIVED (overridden)Menu: showMenu()
                ******************************************

<---- Menu under construction at this time---->


class Menu showSelection() - Please enter your selection:
A.
B.
C.

class Menu - getSelection() - return the user's selectionA

Inside the Class Derived showHiScore()


The High Score is: 1000


(Note that I put in 'A' as an input to the program above)

So, I was able to get the high score shown, and the getSelection() output worked as expected. This answers your questions 1 and 2.

If you're not getting this behaviour, perhaps tell us some more about the environment and compiler you're using.

Hope that helps.

P.S. You have a variable 'sel' in your Menu.h, and also a local variable 'sel' in your main method. This doesn't look good to me! Think about the problems that this can cause, and try to fix it.
....although the 'class2' object is of type Derived, it inherits from type Menu, and thus will execute the Menu constructor too (before the Derived constructor). This explains the two identical lines in your output.


Thanks Sammy! That makes complete sense to me, however, (and just for output purposes) I need to somehow display that I am 'inside' each constructor without duplicity. I commented out the definition of the Menu constructor and ONLY the Derived constructor definition displayed (as expected).

BUT, when I un-commented the Menu constructor definition and commented out the Derived constructor def., that's when I got "Inside of 'Menu' default constructor" displayed twice. So, why would the Menu constructor be calling itself twice?


(Note that I put in 'A' as an input to the program above)

So, I was able to get the high score shown, and the getSelection() output worked as expected. This answers your questions 1 and 2.


I saw 'A' at the end "return the user's selectionA" but I was unsure where you chose to input it. There was no char definitions for the selections. How did you do that?


If you're not getting this behaviour, perhaps tell us some more about the environment and compiler you're using.


I'm using Visual Studio 2008 in Win7. Your compiler seems to compile it a bit differently than mine. What are you using to compile?

Sidenote: I DID get the getSelection() to display, by the way.

Sammy, thanks for replying!

A derived object declaration always calls a base constructor, you can't change that. However, you can choose the constructor it calls ;) Take a look at this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
using namespace std;

class Base
{
public:

    Base()
    {
        cout << "Hi! I'm the Base constructor!" << endl;
    }

    //dummy base constructor
    Base(int)
    {

    }
};

class Derived_A:public Base
{
public:
    Derived_A() //same as Derived_A:Base()
    {
        cout << "Hi! I'm the Derived_A constructor!" << endl;
    }
};

class Derived_B:public Base
{
public:

    //call the dummy base constructor
    //instead of the default
    Derived_B():Base(0)
    {
        cout << "Hi! I'm the Derived_B constructor!" << endl;
    }
};

int main()
{
    cout << "Declaring a Base object..." << endl;
    Base b;

    cout << "\nDeclaring a Derived_A object..." << endl;
    Derived_A da;

    cout << "\nDeclaring a Derived_B object..." << endl;
    Derived_B db;

    cout << "\nhit enter to quit...";
    cin.get();
}

As for the things that don't appear in the output, just put a couple of cin.get(); statements before return 0; in main. I suspect that they do appear but the console window doesn't wait for you to see them :P
Last edited on
@SoloXX: I also compiled that using Visual C++ Express 2008 on Windows 7. If m4ster r0shi's comments didn't help, I suggest to set a breakpoint in the IDE, debug your application (by hitting F5) and step through it...this might help you understand what's happening.
Sammy, m4ster r0shi,

I did as you both said and seemed to have worked out some bugs. On one of my debugging attempts, the ONLY error left I'm getting is on Line 48: Menu.cpp - 'showHiScore' identifier not found. Normally, if this was one big Main(), the solution would've been easy, however, working with multiple files and classes confuses me a bit still.

Where I get real vague is when it comes to calling a function (showHiScore()) from the parent class that belongs to the derived class. I've added case statements for the selections (A, B, C) but am only implementing 'C' at this time, which is the High Score. I've looked and looked in my textbook about how to call a public function from another class but nothing really jumped out at me.

I've modified Menu.cpp and Derived.cpp a little bit so that is what I will post in hopes that you can offer more advice or direction.

Menu.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include "Menu.h"
#include <iostream>


using namespace std;
int HiScore;

Menu::Menu()
{
	cout <<"class Menu - Inside of 'Menu' default constructor\n" <<endl;
}
void Menu::showTitle()
{
        cout <<"\n\nclass Menu showTitle() H-A-N-G-M-A-N" <<endl;
}

void Menu::showMenu()
{
    //showMenu() definition
}

void Menu::showSelection() const
{
        cout <<"\n\nclass Menu showSelection() - Please enter your selection: \n" <<endl;
	cout <<"A. New Game" <<endl;
	cout <<"B. Load Game" <<endl;
	cout <<"C. High Score" <<endl;

	//getSelection() definition
	//Selection 'C' hard-coded for exercise purpose
	//showSelection() definition
}

char Menu::getSelection() const
{
	cout <<"\nclass Menu getSelection() - return the user's selection";

	switch (selection)
	{
	    case 'A':
	         cout <<"You have selected New Game";    //no functionality yet
	         break;
           case 'B':
                cout <<"You have selected Load Game";   //no functionality yet
                break;
           case 'C':
                cout <<"You have selected High Score" <<endl;
			 showHiScore();
                break;
	}

	return selection;
	//getSelection() definition
}

void Menu::setSelection(char selection)
{
}


Derived.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "Derived.h"
#include <iostream>

using namespace std;

Derived::Derived()
{
	cout <<"class Derived - Inside the 'Derived' constructor" <<endl;
}

void Derived::showMenu()
{
	cout <<"\n\n\t\tCLASS DERIVED (overridden) Menu: showMenu()" <<endl;
	cout <<"\t\t******************************************" <<endl;
        cout <<"\n\t<---- Menu functionality under construction at this time---->" <<endl;
       //showMenu() overridden 'base class' definition
}

void Derived::showHiScore(int num)
{
	int HiScore = 1000;
	cout <<"\n\nThe High Score is: "<<HiScore <<endl;
}






Line 48: showHiScore();

This function doesn't exist.
I thought I defined it in Derived.cpp. Is this not the case?

1
2
3
4
5
void Derived::showHiScore(int num)
{
	int HiScore = 1000;
	cout <<"\n\nThe High Score is: "<<HiScore <<endl;
}


Is it not defined in the proper format or....? What did I do wrong?
SoloXX wrote:
I thought I defined it in Derived.cpp.

Yeah, but you're calling it from a Menu object. Menu::showHiScore(int) isn't defined. Consider implementing getSelection() as a member function of the Derived instead of the Menu class.

EDIT: Why do you want the Derived class anyway?... Just put everything inside Menu...
Last edited on
Ahh..I will do as you suggest implementing getSelection() in Derived and see where that takes me.


Why do you want the Derived class anyway?... Just put everything inside Menu...

As part of our class exercise, we're supposed to come up with two different classes based on multiple inheritance. I just labeled my parent class as Menu and the derived class as Derived.
Hey m4ster r0shi....your suggestion worked. My showHiScore(1000) displays now.

Thanks r0shi and sammy for being patient with me. I really appreciated it.
Topic archived. No new replies allowed.