Polymorphic problems

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.

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// 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;

        virtual void 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;
        static char chFiller;
        int nTerminalColumns;
        int nTerminalRows;
        time_t tMenuAccessTime;
};

class WelcomeMenu : public Menu
{
    public:
        WelcomeMenu();
        virtual ~WelcomeMenu();

        virtual void 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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <ctime>

#include "Utilities.h"
#include "Menu.h"

using namespace std;

int main()
{
    Menu* objCurrentMenu;
    objCurrentMenu = new WelcomeMenu;

    objCurrentMenu->ShowWelcomeMessage();

    delete objCurrentMenu;
    return 0;
}


Thanks as always.
Last edited on
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.
Thanks, MiiNiPaa! Makes sense.
Topic archived. No new replies allowed.