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' streams.


Questions and Concerns

1. Why doesn't my 'char Menu::getSelection()' from my menu.cpp not display the cout stream? 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 the cout stream. 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 is defined only once within menu.cpp?

Please take a look at my code:

Menu.h

#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();
    //Shows user the selections such as New Game and Load Game

    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

#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() will 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'
}

Derive.h

#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'

    int showHiScore(int num); 
};

#endif

Derived.cpp

#include "Menu.h"
#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
}

int Derived::showHiScore(int num)
{
	cout <<"\nInside the Class Derived showHiScore()" <<endl;
	int HiScore = 1000;
	return HiScore;
	}

Main.cpp

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

using namespace std;

int main()
{
	Menu class1;
	//Constructor - creates 'class Menu' object
	
	Derived class2;
        //Constructor - 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;
}

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

I'm assuming by "display the cout stream" you mean it doesn't write anything to the console? I think you may need to flush the buffer by doing << std::endl;

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

What do you mean by "correctly"? What is it supposed to do? What does it currently do?

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 is defined only once within menu.cpp?

I imagine you are seeing the parent class constructor being called when you are instantiating a derived class.

Dave

I'm assuming by "display the cout stream" you mean it doesn't write anything to the console? I think you may need to flush the buffer by doing << std::endl;

Thank you and yes, the console. However, will a destructor work in place of <<std::endl;? Would that be the same thing?

What do you mean by "correctly"? What is it supposed to do? What does it currently do?

What I want the showHiScore() to do is just simply have a display that says, "High Score: 1000". In the future, I will work on implementing a scoring system somehow.

I imagine you are seeing the parent class constructor being called when you are instantiating a derived class.

Hmm...my logic was I thought if created 1 constructor per class and defined each constructor in their respective *.cpp, it would display once per class. Is my logic off?

Thanks for replying!

Calling a destructor shouldn't flush the stream. I don't know much about it, but there have been times that my output hasn't appeared when I expect it to and I've needed to flush the stream with an endl to fix it.

As for the constructors, since one of your classes is a derived class, it's constructor gets called along with its parent class constructor.

Calling a destructor shouldn't flush the stream. I don't know much about it, but there have been times that my output hasn't appeared when I expect it to and I've needed to flush the stream with an endl to fix it.

As for the constructors, since one of your classes is a derived class, it's constructor gets called along with its parent class constructor.

I commented out the 'derived' header and cpp files and the duplicity went away but I don't understand HOW to have both classes 'couts' display properly.

Any suggestions?

This article has been dead for over six months. Start a new discussion instead.