base class undefined

Pages: 12
The main point is the factory, as one part of the overall program has done its job - it manufactures Players if you tell it what type of Player you want - and that’s all.

You don’t hve to add anything to it’s functionality, especially platform dependent stuff like asking questions better suited to a menu.

I’m therefore not sure what classes and what members you are asking about :)





I'm referencing the cin within the client with the class member "choice" and how I should separate it to a display or menu function.
OK. I thought as much and this is roughly/quickly how you can do it. The enum causes the 'unseemly' duplication which can be overcome with a translator function to go from numbers to the enumeration but that isn't the main issue.

You should be able to see now how the menu is decoupled from the factory and the same factory (different menu though) can be used whether it is a console (this example) or other (eg GUI) interface runs the game.

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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <iostream>
#include <string>


enum player_type{MAJE, NINJA, BARBARIAN};


class Player
{
public:
    virtual ~Player(){};
    virtual std::string printPlayer() = 0;
    static Player* create(player_type type);
};


class Mage :public Player
{
public:
    std::string printPlayer()
    {
        return "Mage";
    }
};



class Ninja :public Player
{
public:
    std::string printPlayer()
    {
        return "Ninja";
    }
};



class Barbarian :public Player
{
public:
    std::string printPlayer() { return "Barbarian"; }
};


Player* Player::create(player_type type)
{
    if (type == MAJE)
        return new Mage();
    else if (type == NINJA)
        return new Ninja();
    else if (type == BARBARIAN)
        return new Barbarian();
    else return NULL;
}


class Client
{
public:
    Client()
    {
        player_type type{NINJA};
        
        int choice;
        std::cin >> choice;
        
        switch (choice)
        {
            case 1:
                type = MAJE;
                break;
            case 2:
                type = NINJA;
                break;
            case 3:
                type = BARBARIAN;
                break;
            default:
                break;
        }
        
        Pplayer = Player::create(type);
    }
    ~Client()
    {
        if (Pplayer)
        {
            delete[] Pplayer;
            Pplayer = NULL;
        }
    }
    
    Player* getclass()
    {
        return Pplayer;
    }
    
private:
    int choice;
    Player* Pplayer;
};

class Menu
{
private:
    Client* client;
    std::string prompt;
    
public:
    Menu(){};
    
    ~Menu(){};
    
    void welcome()
    {
        std::cout
        << "WELCOME ...\n"
        << "What do you want to do today? ";
    }
    
    Player* create_player()
    {
        std::cout
        << "Select type of player:\n"
        << "1. mage 2. ninja 3. barbarian \n";
        
        int choice{0};
        std::cin >> choice;
        
        player_type type{NINJA};
        
        switch (choice)
        {
            case 1:
                type = MAJE;
                break;
            case 2:
                type = NINJA;
                break;
            case 3:
                type = BARBARIAN;
                break;
            default:
                break;
        }
        
        return Player::create( type );
    }
};



int main()
{
    Menu menu;
    menu.welcome();
    
    Player* plr_1 = menu.create_player();
    std::cout << plr_1->printPlayer() << '\n';
    
    Player* plr_2 = menu.create_player();
    std::cout << plr_2->printPlayer() << '\n';
    
    //    Client *pClient = new Client();//allocating info from the heap
    //    player* pPlayer = pClient->getclass();
    //    std::cout<<pPlayer->printPlayer();
    
    return 0;
}
And going that extra step ...

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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <iostream>
#include <string>


enum player_type{MAJE, NINJA, BARBARIAN};

static player_type translator(int aChoice)
{
    player_type type{NINJA};
    
    switch (aChoice)
    {
        case 1:
            type = MAJE;
            break;
        case 2:
            type = NINJA;
            break;
        case 3:
            type = BARBARIAN;
            break;
        default:
            break;
    }
    return type;
}

class Player
{
public:
    virtual ~Player(){};
    virtual std::string printPlayer() = 0;
    
    static Player* create(player_type type);
};


class Mage :public Player
{
public:
    std::string printPlayer()
    {
        return "Mage";
    }
};



class Ninja :public Player
{
public:
    std::string printPlayer()
    {
        return "Ninja";
    }
};



class Barbarian :public Player
{
public:
    std::string printPlayer()
    {
        return "Barbarian";
    }
};


Player* Player::create(player_type type)
{
    if (type == MAJE)
        return new Mage();
    else if (type == NINJA)
        return new Ninja();
    else if (type == BARBARIAN)
        return new Barbarian();
    else return NULL;
}


class Client
{
private:
    int choice{0};
    Player* Pplayer{nullptr};
    
public:
    Client()
    {
        Pplayer = Player::create(translator(choice));
    }
    
    ~Client()
    {
        if (Pplayer)
        {
            delete[] Pplayer;
            Pplayer = nullptr;
        }
    }
    
    Player* getclass()
    {
        return Pplayer;
    }
};


class Menu
{
private:
    Client* client;
    std::string prompt;
    
public:
    Menu(){};
    
    ~Menu(){};
    
    void welcome()
    {
        std::cout
        << "WELCOME ...\n"
        << "What do you want to do today? ";
    }
    
    Player* create_player()
    {
        std::cout
        << "Select type of player:\n"
        << "1. mage 2. ninja 3. barbarian \n";
        
        int choice{0};
        std::cin >> choice;
        return Player::create( translator(choice) );
    }
};



int main()
{
    Menu menu;
    menu.welcome();
    
    Player* plr_1 = menu.create_player();
    std::cout << plr_1->printPlayer() << '\n';
    
    Player* plr_2 = menu.create_player();
    std::cout << plr_2->printPlayer() << '\n';
    
    return 0;
}
Topic archived. No new replies allowed.
Pages: 12