Hi, Forum.
I'm hoping you all can help me solve a problem with polymorphism that's been bugging me. I'm building a simple system management console application. I've abstracted the console "Menu" and derived from it a "WelcomeMenu" class with public inheritance.
The problem is that when instantiating a Menu* object and assigning it a new WelcomeMenu...I'm still not able to use WelcomeMenu's "ShowWelcomeMessage() with the Menu* object. Instead, I get "error: Class 'Menu' has no member function call 'ShowWelcomeMessage().' Which is true, but I thought a pointer-to-Menu object should be able to use the public methods of derived classes without casting in this case. Am I way off here? Still a newb. Code follows.
// Menu and WelcomeMenu Classes
#ifndef MENU_H
#define MENU_H
#include <ctime>
#include <iostream>
#include <string>
#include <termios.h>
#include <unistd.h>
#include <cstdio>
#include <sys/ioctl.h>
#include "User.h"
using std::cout;
using std::cin;
using std::string;
class Menu
{
public:
Menu();
virtual ~Menu() = 0;
virtualvoid DisplayMenuHeading(); // Display's console menu heading
time_t CaptureTimeOfMenuAccess(); // Time menu was accessed for logging
User* AuthenticateUser(const string& username, // Basic authentication of user w/ pw
User* sessionUser); // masking
protected:
int GetTerminalColumns() const; // Returns terminal width in columns
int GetTerminalLines() const; // Returns terminal height in rows
static string GetSoftwareName(); // Returns application name for header
static string GetVersionName(); // Returns software version number
int GetPasswordChar(); // A "getch()" for password masking
string ReceivePassword( // Used to convert pw chars to string
bool bShowAsterisk = true);
void DisplayCentering( // Formating terminal centering
const string& someString);
void ShowMenuHeading() const; // Prints menu heading on term
void ClearMenuScreen() const; // Clears term w/o system call
private:
//User* selectedUser;
static string szSoftwareName;
static string szVersionName;
staticchar chFiller;
int nTerminalColumns;
int nTerminalRows;
time_t tMenuAccessTime;
};
class WelcomeMenu : public Menu
{
public:
WelcomeMenu();
virtual ~WelcomeMenu();
virtualvoid DisplayMenuHeading(); // Displays Menu Heading
void ShowWelcomeMessage(); // Prints a welcome/licensing message
void DisplayNewUserPrompts() const; // Prompt for new user creation
void DisplayUserLoginPrompts() const; // Prompt for user login
void DisplayFailedAuthenticationMessage(); // Failed authentication message
void DisplayListOfSystems( // Show systems user has access to
const User* selectedUser);
const string& GetMenuName() const;
private:
};
I get a compiler error when running this simple program:
That is because class Menu dos not have a method called ShowWelcomeMessage() and because of that you cannot call it when dealing with Menu objects. Remember pointers and references to Menu pointing to derived objects does not know about that and allows working with them as Menu objects only.