Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Polymorphism didn't work



  • Hello guys , so i'm writing code to move an object (her a green ball ) i created the class Personnage (to create the object ) and 3 others class who inherit from Personnage (Tortue is the class to move the Object slowly , human class tho move the object more quick than tortue and Flash to move the object the fastest) , now in my main program when i created the object and i want to move it , it didn't move (it seems like i have a problem with polymorphism when i create flash and try to move it with moveUp(*flash) it didn't work )

    6_1550240132066_Capture d’écran 2019-02-15 à 15.07.08.png

    5_1550240132066_Capture d’écran 2019-02-15 à 15.06.59.png

    4_1550240132066_Capture d’écran 2019-02-15 à 15.06.37.png

    3_1550240132066_Capture d’écran 2019-02-15 à 15.06.22.png

    2_1550240132066_Capture d’écran 2019-02-15 à 15.06.02.png

    0_1550240343389_Capture d’écran 2019-02-15 à 15.18.47.png

    P.S: i didn't put a constructor for the 3 class ( Flash , tortue and human ) because i gonna move only one object who is the green ball , its logic what i have do ?



  • what's with all the

    this->
    

    syntax? It is superfluous (I like that word) and usually unnecessary unless you explictly need to reference a shadowed parameter variable.


  • Qt Champions 2017

    ... and also please copy-paste the code as text. Images make it really, really hard to follow what's going on.


  • Lifetime Qt Champion

    Hi,

    To add to my fellow, where are you calling these move methods ?



  • i want to create one object Personnage ( who is the green ball ) and control the move of this green ball (only one) with 3 différents form (Tortue is the slow one , human the medium and flash the fastest one ) ,how can i do this ? i need only one constructor to create the object (i put it in Personnage) and 3 class who inherit from Personnage (Tortue, Human and flash) without constructor (because these class are only need to move the main object ) , my problem is to control only one object with different form , how can i do this ?


  • Lifetime Qt Champion

    Technically you don't need three subclasses, you can just have setters which set the speed of your "Personnage" class.



  • @SGaist yeah but i want to do it with classes to apply polymorphism


  • Moderators

    @Zunneh
    your problem is that you shadow your base class functions instead of overwriting them.

    IIRC name lookup stops if it finds a name in one of your bases. It won't look beyond in other bases.

    So it finds the base function and does not look for your other implementation.

    IF you really want to go the way of different classes, make your base class function virtual and overwrite them in the derived classes



  • @J.Hilk i make my base class virtual and i write the same functions in the other class , my problem i think is with constructor , i create the object in my base class (Personnage her ) and the 3 child are without constructor , but the child can't control the object created in the base class , this is my problem , i want to say to the child that they are going to control the object of the class base (the green ball)



  • 1st of all, can you add override to the derived classes to make sure you are indeed reimplementing the virtual methods? https://en.cppreference.com/w/cpp/language/override

    i create the object in my base class

    If you create an object of the base class then it will use the methods defined in the base class, not on the derived ones



  • @VRonin said in Polymorphism didn't work:

    https://en.cppreference.com/w/cpp/language/override

    so i shouldn't create it on the base class ?



  • @Zunneh No, you just add the override keyword in the base class to tell the compiler to make 100% sure you are indeed reimplementing a virtual method. It's just a safety feature, no change in code behaviour



  • @VRonin yeah but where i should create the object ? the green ball , you tell me i can't create it in the base class because it will only use base class methods



  • members can be created anywhere you want, the base class is fine, what I meant is that if you create Personnage a; it will behave as a Personnage. Always.



  • Personnage.h

     ifndef PERSONNAGE_H
     #define PERSONNAGE_H
     #include <QMainWindow>
     #include <QtWidgets>
    
     class Personnage:public QWidget
    {
    public: 
    
    Personnage(QWidget *parent=0);
    ~Personnage();
    virtual void moveToUp();
    virtual void moveToLeft();
    virtual void moveToRight();
    virtual void moveToDown();
    
    protected:
    
      int m_x;
      int m_y;
      QLabel *test;
     };
    
    void moveUp(Personnage *e);
    void moveLeft(Personnage *e);
    void moveRight(Personnage *e);
    void moveDown(Personnage *e);
    
     #endif // PERSONNAGE_H
    

    Personnage.cpp

     #include "personnage.h"
    
     Personnage::Personnage(QWidget *parent):QWidget(parent)
    {
    
    QLabel *test=new QLabel(this);
    test->setFixedSize(30,30);
    QPixmap image5("/Users/mac/Documents/Qt projet/PolymorphysmeV1/bouton-vert.png");
    test->setPixmap(image5);
    
    }
    
    void Personnage::moveToUp()
    {
    
    move(40,m_y);
    
    }
    
    void Personnage::moveToLeft()
    
    {
    
    move(20,m_y);
    
    }
    
    void Personnage::moveToRight()
    
    {
    
    move(0,m_y);
    
    }
    
    void Personnage::moveToDown()
    
    {
    
    move(30,m_y);
    
    }
    
    void moveUp(Personnage *e)
    
    {
    
    e->moveToUp();
    
    }
    
    void moveLeft(Personnage *e)
    
    {
    
     e->moveToLeft();
    
    }
    
    void moveRight(Personnage *e)
    
    {
    
    e->moveToRight();
    
    }
    
    void moveDown(Personnage *e)
    
    {
    
    e->moveToDown();
    
    }
    

    Now this is the child Flash.h

    #ifndef FLASH_H
    #define FLASH_H
    #include"personnage.h"
    class Flash:public Personnage
    
    {
    
    public:
    void moveToUp() override;
    void moveToLeft() override;
    void moveToRight() override;
    void moveToDown() override;
    
    private:
    
     };
    
    #endif // FLASH_H
    

    This is the child flash.cpp

    #include "flash.h"
    void Flash::moveToUp()
    {
    move(10,10);
    }
    void Flash::moveToLeft()
    {
    move(m_x+10,m_y);
    }
    void Flash::moveToRight()
    {
    move(m_x-10,m_y);
    }
    void Flash::moveToDown()
    {
    move(m_x,m_y+10);
    }
    

    now in main program MainWindow.cpp

    lawl=new Personnage(this);
    flash =new Flash; 
    moveUp(lawl); //This work
    moveUp(flash); // this don't work its like i didn't write this command


  • @Zunneh said in Polymorphism didn't work:

    moveUp(flash); // this don't work its like i didn't write this command

    lawl and flash are 2 different items. Since you never give a parent to flash it remains as a separate top level window and since you never call show on it it is never displayed on screen. move works as expected



  • @VRonin Flash is the same object created with lawl (Personnage ) how can i do it ?


  • Moderators

    @Zunneh

    flash =new Flash(this);
    auto pers = qobject_cast<Personage*>(flash);
    if(pers)
        pers->moveUp();
    flash->moveUp();
    

    really, you should consider an other approach:

    class myDot :  public QLabel
    {
     enum MoveSpeed{
         Personage = 1,
         Tortue = 2,
         Flash  = 10
    }; E_ENUM(MoveSpeed)
    .....
    
    public: 
    void moveToUp(MoveSpeed speed){
         move (m_x, m_y - static_cast<int>(speed));
    }
    
    }
    


  • @Zunneh said in Polymorphism didn't work:

    Flash is the same object created with lawl

    It is not. 2 new = 2 different objects



  • @VRonin yeah you are right i tried it and its 2 different objects , so i forget something , i'm moving 2 Different objects and my goal is to move one object with different form



  • my goal is to move one object with different form

    @SGaist said in Polymorphism didn't work:

    Technically you don't need three subclasses, you can just have setters which set the speed of your "Personnage" class.

    @J.Hilk said in Polymorphism didn't work:

    really, you should consider an other approach:

    class myDot :  public QLabel
    {
     enum MoveSpeed{
         Personage = 1,
         Tortue = 2,
         Flash  = 10
    }; E_ENUM(MoveSpeed)
    .....
    
    public: 
    void moveToUp(MoveSpeed speed){
         move (m_x, m_y - static_cast<int>(speed));
    }
    
    }
    


  • thank you guys , it work :)


Log in to reply