IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage C++ Discussion :

Polymorphisme (ou truc bête je sais pas trop en fait :'( )


Sujet :

Langage C++

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Homme Profil pro
    Étudiant
    Inscrit en
    Septembre 2016
    Messages
    13
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Septembre 2016
    Messages : 13
    Par défaut Polymorphisme (ou truc bête je sais pas trop en fait :'( )
    Bonjour à tous, alors voilà en gros je fais un mini rpg (je débute en c++, il est pas très gros) et en faite j'ai 3 personnages différents: Guerrier, Mage, Tank
    Quand je lance mon code pour faire rapide c un tout par tour ca marche très bien le seul problème c que j'ai deux fonction "jouer" (celle qui s'occupe juste d'un tour):
    - une pour le Guerrier et le Tank (entre les deux en dehors des caractéristiques ya pas de differences)
    - et une pour le Mage qui lui a de la mana, peut lancer un sort...
    le problème ce que quand il appelle le tour d'un mage c'est le "jouer" universel et non celui du mage qui est appelle et ca je comprend pas ...

    .cpp (les 2 sont vers la ligne 240)
    Code :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
     
    #include "Personnage.h"
    #include "Arme.h"
    #include <iostream>
    #include <string>
    #include <memory>
    #include <ctime>
    #include <cstdlib>
    #include <vector>
     
    using namespace std;
     
     
    Personnage::Personnage(string nomPersonnage, string nomClasse, int vie, int nombrePotionDeVie, string nomArme, int degatsArme): m_nom(nomPersonnage), m_classe(nomClasse), m_vieMax(vie), m_nombrePotionDeVieMax(nombrePotionDeVie), m_arme(nomArme, degatsArme)
    {
        m_vie = vie;
        m_nombrePotionDeVie = nombrePotionDeVie;
    }
     
    Guerrier::Guerrier(string nomPersonnage): Personnage(nomPersonnage, "Guerrier", 150, 1, "Epee en fer", 40)
    {
     
    }
     
    Mage::Mage(string nomPersonnage): Personnage(nomPersonnage, "Mage", 120, 1, "Epee en bois", 15), m_manaMax(150), m_nombrePotionDeManaMax(3), m_regMana(30), m_pouvoirMagique("Boule de feu", 45, 90)
    {
        m_mana = m_manaMax;
        m_nombrePotionDeMana = m_nombrePotionDeManaMax;
    }
     
    Tank::Tank(string nomPersonnage): Personnage(nomPersonnage, "Tank", 220, 3, "Epee en bois", 15)
    {
     
    }
     
    void Personnage::recevoirDegats(int nombreDegats)
    {
        m_vie -= nombreDegats;
        if(m_vie < 0)
        {
            m_vie = 0;
        }
    }
     
    void Personnage::attaquer(Personnage &cible)
    {
        cible.recevoirDegats(m_arme.getDegats());
    }
     
    void Personnage::afficherEtat() const
    {
     
        cout << "Vie : " << m_vie << " (Vie max : " << m_vieMax << ")" << endl;
        cout << "Nombre de potion de vie : " << m_nombrePotionDeVie << " (Nombre de potion de vie max : " << m_nombrePotionDeVieMax << ")" << endl;
        m_arme.afficher();
        cout << endl;
    }
     
    void Mage::afficherEtat() const
    {
        cout << "Vie : " << m_vie << " (Vie max : " << m_vieMax << ")" << endl;
        cout << "Mana : " << m_mana << " (Mana max : " << m_manaMax << ")" << endl;
        m_arme.afficher();
        cout << endl;
    }
     
    void Personnage::boirePotionDeVie()
    {
        if(m_nombrePotionDeVie > 0 && m_vieMax > m_vie + 30)
        {
            m_vie += 30;
            m_nombrePotionDeVie--;
        }
        else if(m_vieMax < m_vie + 30)
        {
            m_vie = m_vieMax;
        }
        else if(m_nombrePotionDeVie < 1)
        {
            cout << "Vous n'avez plus de potion de vie ;(" << endl;
        }
    }
     
    void Mage::boirePotionDeMana()
    {
        if(m_nombrePotionDeMana > 0 && m_manaMax > m_mana + 30)
        {
            m_mana += 30;
            m_nombrePotionDeMana--;
        }
        else if(m_manaMax < m_mana + 30)
        {
            m_mana = m_manaMax;
        }
        else if(m_nombrePotionDeMana < 0)
        {
            cout << "Vous n'avez plus de potion de mana ;(" << endl;
        }
    }
     
    void Mage::lancerSort(Personnage &cible)
    {
        if(m_mana > m_pouvoirMagique.getCoutMana())
        {
            cible.recevoirDegats(m_pouvoirMagique.getDegats());
            m_mana -= m_pouvoirMagique.getCoutMana();
        }
        else
        {
            cout << "Vous n'avez plus assez de mana ! (Mana : " << m_mana << ")" << endl;
        }
    }
     
    int Personnage::getVie() const
    {
        return m_vie;
    }
     
    string Personnage::getNom() const
    {
        return m_nom;
    }
     
    string Personnage::getClasse() const
    {
        return m_classe;
    }
     
    void jouerAutomatiquement()
    {
        int x;
        Personnage *xP;
        do
        {
            cout << "Voulez-vous jouer seul (1) ou a plusieurs (2) ?"<< endl;
            cin >> x;
        }while(x != 1 && x != 2);
        if(x == 1)
        {
            xP = creationPersonnage();
            IA(xP);                //////////////////////////////////////////////////////////////////
        }
        else
        {
            do
            {
                cout << "A combien de joueurs voulez-vous jouer ?" << endl;
                cin >> x;
            }while(x <= 1);
            vector<Personnage*> tableauPersonnage;
            for(int i(0); i < x; i++)
            {
                tableauPersonnage.push_back(creationPersonnage());
            }
            tourParTour(tableauPersonnage);
        }
    }
     
    Personnage* creationPersonnage()
    {
        string nomClasse;
        string nomPersonnage;
        string *pointeurNomPersonnage = &nomPersonnage;
        do
        {
        cout << "Quelle classe desirez-vous prendre parmi Guerrier, Mage ou Tank ?" << endl;
        cin >> nomClasse;
        }while(nomClasse != "Guerrier" && nomClasse != "Mage" && nomClasse != "Tank");
        cout << "Quel est le nom de votre " << nomClasse << " ?" << endl;
        cin >> nomPersonnage;
        if(nomClasse == "Guerrier")
        {
            Guerrier *xG = new Guerrier(nomPersonnage);
            cout << "Vous venez de creer le " << nomClasse << " " << *pointeurNomPersonnage << " !" << endl;
            xG->afficherEtat();
            return  xG;
        }
        else if(nomClasse == "Mage")
        {
            Mage *xM = new Mage(nomPersonnage);
            cout << "Vous venez de creer le " << nomClasse << " " << *pointeurNomPersonnage << " !" << endl;
            xM->afficherEtat();
            return xM;
        }
        else
        {
            Tank *xT = new Tank(nomPersonnage);
            cout << "Vous venez de creer le " << nomClasse << " " << *pointeurNomPersonnage << " !" << endl;
            xT->afficherEtat();
            return xT;
        }
    }
     
    void IA(Personnage *xP)
    {
        Personnage *xIA;
        string nomIA("IA");
        srand(time(0));
        int x = rand()%3;
        switch(x)
        {
            case 0: /////////////////////////////////////////////////////////////
                xIA = new Guerrier(nomIA);
                break;
            case 1:
                xIA = new Mage(nomIA);
                break;
            case 2:
                xIA = new Tank(nomIA);
                break;
        }
    }
     
    void tourParTour(vector<Personnage*> tableauPersonnage)
    {
        vector<Personnage*> tableauPersonnageEnVie = tableauPersonnage;
        int x;
        for(int i(0); i < tableauPersonnage.size(); i++)
        {
            if(tableauPersonnage[i]->getVie() > 0)
            {
               x++;
            }
        }
        while(tableauPersonnageEnVie.size() > 1)
        {
            for(int i(0); i < tableauPersonnageEnVie.size(); i++)
            {
                if(tableauPersonnageEnVie[i]->getVie() > 0)
                {
                    tableauPersonnageEnVie[i]->jouer(tableauPersonnageEnVie[i], tableauPersonnageEnVie);
                }
                else
                {
                    tableauPersonnageEnVie.erase(tableauPersonnageEnVie.begin() + i);
                }
            }
        }
        cout << "--------------------------------------------------------" << endl;
        cout << tableauPersonnageEnVie[0]->getNom() << " a gagne la partie !" << endl;
        cout << "--------------------------------------------------------" << endl;
    }
     
    void Personnage::jouer(Personnage *perso, vector<Personnage*> tableauPersonnageEnVie)
    {
        if(m_vie > 0)
        {
            int x;
            do
            {
                cout << "------ Tour de " << m_nom << " ------" << endl;
                cout << "(vie: " << m_vie << "/" << m_vieMax << ")" << endl;
                cout << "Voulez-vous : " << endl;
                cout << "- attaquer un personnage (1)" << endl;
                cout << "- boire une potion de vie (2)" << endl;
                cin >> x;
            }while(x < 1 || x > 2);
            switch(x)
            {
                case 1:
                    int y1;
                    do
                    {
                        cout << "Qui voulez-vous attaquer ?" << endl;
                        for(int i(0); i < tableauPersonnageEnVie.size(); i++)
                        {
                            if(tableauPersonnageEnVie[i] != perso)
                            {
                                cout << "- " << tableauPersonnageEnVie[i]->getNom() << "( vie : " << tableauPersonnageEnVie[i]->getVie() << " ) ( " << i << " )" << endl;
                            }
                        }
                        cin >> y1;
                    }while(y1 < 0 || y1 > tableauPersonnageEnVie.size());
                    for(int i(0); i < tableauPersonnageEnVie.size(); i++)
                    {
                        if(y1 == i)
                        {
                            perso->attaquer(*tableauPersonnageEnVie[i]);
                        }
                    }
                    break;
                case 2:
                    perso->boirePotionDeVie();
                    break;
            }
        }
    }
     
    void Mage::jouer(Mage *perso, vector<Personnage*> tableauPersonnageEnVie)
    {
        if(m_vie > 0)
        {
            int x;
            do
            {
                cout << "------ Tour de " << m_nom << " ------" << endl;
                cout << "(vie: " << m_vie << "/" << m_vieMax << " mana : " << m_mana << "/" << m_manaMax << ")" << endl;
                cout << "Voulez-vous : " << endl;
                cout << "- attaquer un personnage (1)" << endl;
                cout << "- lancer un sort (2)" << endl;
                cout << "- boire une potion de vie (3)" << endl;
                cout << "- boire une potion de mana (4)" << endl;
                cin >> x;
            }while(x < 1 || x > 4);
            switch(x)
            {
                case 1:
                    int y1;
                    do
                    {
                        cout << "Qui voulez-vous attaquer ?" << endl;
                        for(int i(0); i < tableauPersonnageEnVie.size(); i++)
                        {
                            if(tableauPersonnageEnVie[i] != perso)
                            {
                                cout << "- " << tableauPersonnageEnVie[i]->getNom() << "( vie : " << tableauPersonnageEnVie[i]->getVie() << " ) ( " << i << " )" << endl;
                            }
                        }
                        cin >> y1;
                    }while(y1 < 1 || y1 > tableauPersonnageEnVie.size());
                    for(int i(0); i < tableauPersonnageEnVie.size(); i++)
                    {
                        if(y1 == i)
                        {
                            perso->attaquer(*tableauPersonnageEnVie[i]);
                        }
                    }
                    break;
                case 2:
                    int y2;
                    do
                    {
                        cout << "Qui voulez-vous attaquer ?" << endl;
                        for(int i(0); i < tableauPersonnageEnVie.size(); i++)
                        {
                            if(tableauPersonnageEnVie[i] != perso)
                            {
                                cout << "- " << tableauPersonnageEnVie[i]->getNom() << "( vie : " << tableauPersonnageEnVie[i]->getVie() << " ) ( " << i << " )" << endl;
                            }
                        }
                        cin >> y2;
                    }while(y2 < 1 || y2 > tableauPersonnageEnVie.size());
                    for(int i(0); i < tableauPersonnageEnVie.size(); i++)
                    {
                        if(y2 == i)
                        {
                            perso->lancerSort(*tableauPersonnageEnVie[i]);
                        }
                    }
                    break;
                case 3:
                    perso->boirePotionDeVie();
                    break;
                case 4:
                    perso->boirePotionDeMana();
                    break;
            }
        }
    }

    le .h (je les ai mis en virtual)
    Code :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    #ifndef PERSONNAGE_H_INCLUDED
    #define PERSONNAGE_H_INCLUDED
     
    #include <iostream>
    #include <string>
    #include <vector>
    #include "Arme.h"
    #include "PouvoirMagique.h"
     
    class Personnage
    {
        public:
     
        Personnage(std::string nomPersonnage, std::string nomClasse, int vie, int nombrePotionDeVie, std::string nomArme, int degatsArme);
        void recevoirDegats(int nbDegats);
        void attaquer(Personnage &cible);
        void afficherEtat() const;
        void boirePotionDeVie();
        int getVie() const;
        std::string getNom() const;
        std::string getClasse() const;
        virtual void jouer(Personnage *perso, std::vector<Personnage*> tableauPersonnageEnVie);
     
        protected:
     
        int m_vie, m_vieMax;
        int m_nombrePotionDeVie, m_nombrePotionDeVieMax;
        std::string m_nom;
        std::string m_classe;
        Arme m_arme;
    };
     
     
    class Guerrier : public Personnage
    {
        public:
     
        Guerrier(std::string nomPersonnage);
     
        protected:
    };
     
     
    class Mage : public Personnage
    {
        public:
     
        Mage(std::string nomPersonnage);
        void lancerSort(Personnage &cible);
        void boirePotionDeMana();
        void afficherEtat() const;
        virtual void jouer(Mage *perso, std::vector<Personnage*> tableauPersonnageEnVie);
     
     
        protected:
     
        int m_mana, m_manaMax;
        int m_nombrePotionDeMana, m_nombrePotionDeManaMax;
        int m_regMana;
        PouvoirMagique m_pouvoirMagique;
    };
     
     
    class Tank : public Personnage
    {
        public:
     
        Tank(std::string nomPersonnage);
     
        protected:
    };
     
    void jouerAutomatiquement();
    Personnage* creationPersonnage();
    void IA(Personnage *xP);
    void tourParTour(std::vector<Personnage*> tableauPersonnage);
     
    #endif // PERSONNAGE_H_INCLUDED

  2. #2
    Membre Expert
    Inscrit en
    Mars 2005
    Messages
    1 431
    Détails du profil
    Informations forums :
    Inscription : Mars 2005
    Messages : 1 431
    Par défaut
    Les deux fonctions n'ont pas la même signature : celle de la classe de base accepte un Personnage * en premier argument alors que celle de la classe dérivée accepte un Mage *. Le compilateur les voit donc comme deux méthodes distinctes et non pas comme une implémentation spécialisée.

    Prends l'habitude d'ajouter le qualificateur override (ou final) aux redéclarations dans les classes dérivées, le compilateur génèrera ainsi une erreur si aucune méthode correspondante n'est présente dans la classe de base.

  3. #3
    Expert éminent

    Femme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Juin 2007
    Messages
    5 202
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France, Essonne (Île de France)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Juin 2007
    Messages : 5 202
    Par défaut
    C'est parce que tu n'a pas redéfini la fonction jouer() de personnage, mais créé une autre fonction qui la masque.
    D'ailleurs, si tu avais utilisé les mot-clé override, tu l'aurai vu.

  4. #4
    Membre averti
    Homme Profil pro
    Étudiant
    Inscrit en
    Septembre 2016
    Messages
    13
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Septembre 2016
    Messages : 13
    Par défaut
    Deso pour le doublon en faite j'avais mis en résolu l'autre car je l'avais crée au mausais endroit et j'en ai crée un autre en bon endroit (ici)
    Merci à tous j'ai résolu le problème en dégageant le premiere argument qui servais a rien et faisait bugguer le truc, en utilisant "this" ca marche a merveille
    Merci à tous

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 1
    Dernier message: 19/02/2006, 19h52

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo