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;
}