Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Polymorphism didn't work
Forum Updated to NodeBB v4.3 + New Features

Polymorphism didn't work

Scheduled Pinned Locked Moved Solved General and Desktop
22 Posts 6 Posters 4.2k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    Zunneh
    wrote on last edited by
    #1

    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 ?

    my english is average, please use simple words and try to be the most explicit, thank you

    kshegunovK J.HilkJ 2 Replies Last reply
    0
    • Kent-DorfmanK Offline
      Kent-DorfmanK Offline
      Kent-Dorfman
      wrote on last edited by Kent-Dorfman
      #2

      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.

      I light my way forward with the fires of all the bridges I've burned behind me.

      1 Reply Last reply
      1
      • Z Zunneh

        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 ?

        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by kshegunov
        #3

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

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply
        3
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • Z Offline
            Z Offline
            Zunneh
            wrote on last edited by Zunneh
            #5

            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 ?

            my english is average, please use simple words and try to be the most explicit, thank you

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

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

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              Z VRoninV 2 Replies Last reply
              4
              • SGaistS SGaist

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

                Z Offline
                Z Offline
                Zunneh
                wrote on last edited by
                #7

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

                my english is average, please use simple words and try to be the most explicit, thank you

                1 Reply Last reply
                0
                • Z Zunneh

                  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 ?

                  J.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on last edited by
                  #8

                  @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


                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  Z 1 Reply Last reply
                  2
                  • J.HilkJ J.Hilk

                    @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

                    Z Offline
                    Z Offline
                    Zunneh
                    wrote on last edited by
                    #9

                    @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)

                    my english is average, please use simple words and try to be the most explicit, thank you

                    1 Reply Last reply
                    0
                    • VRoninV Offline
                      VRoninV Offline
                      VRonin
                      wrote on last edited by
                      #10

                      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

                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                      ~Napoleon Bonaparte

                      On a crusade to banish setIndexWidget() from the holy land of Qt

                      Z 1 Reply Last reply
                      2
                      • VRoninV VRonin

                        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

                        Z Offline
                        Z Offline
                        Zunneh
                        wrote on last edited by
                        #11

                        @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 ?

                        my english is average, please use simple words and try to be the most explicit, thank you

                        VRoninV 1 Reply Last reply
                        0
                        • Z Zunneh

                          @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 ?

                          VRoninV Offline
                          VRoninV Offline
                          VRonin
                          wrote on last edited by
                          #12

                          @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

                          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                          ~Napoleon Bonaparte

                          On a crusade to banish setIndexWidget() from the holy land of Qt

                          Z 1 Reply Last reply
                          0
                          • VRoninV VRonin

                            @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

                            Z Offline
                            Z Offline
                            Zunneh
                            wrote on last edited by
                            #13

                            @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

                            my english is average, please use simple words and try to be the most explicit, thank you

                            1 Reply Last reply
                            0
                            • VRoninV Offline
                              VRoninV Offline
                              VRonin
                              wrote on last edited by
                              #14

                              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.

                              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                              ~Napoleon Bonaparte

                              On a crusade to banish setIndexWidget() from the holy land of Qt

                              1 Reply Last reply
                              0
                              • Z Offline
                                Z Offline
                                Zunneh
                                wrote on last edited by
                                #15

                                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
                                

                                my english is average, please use simple words and try to be the most explicit, thank you

                                VRoninV 1 Reply Last reply
                                0
                                • Z Zunneh

                                  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
                                  
                                  VRoninV Offline
                                  VRoninV Offline
                                  VRonin
                                  wrote on last edited by
                                  #16

                                  @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

                                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                  ~Napoleon Bonaparte

                                  On a crusade to banish setIndexWidget() from the holy land of Qt

                                  Z 1 Reply Last reply
                                  2
                                  • VRoninV VRonin

                                    @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

                                    Z Offline
                                    Z Offline
                                    Zunneh
                                    wrote on last edited by
                                    #17

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

                                    my english is average, please use simple words and try to be the most explicit, thank you

                                    J.HilkJ VRoninV 2 Replies Last reply
                                    0
                                    • Z Zunneh

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

                                      J.HilkJ Offline
                                      J.HilkJ Offline
                                      J.Hilk
                                      Moderators
                                      wrote on last edited by J.Hilk
                                      #18

                                      @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));
                                      }
                                      
                                      }
                                      

                                      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                                      Q: What's that?
                                      A: It's blue light.
                                      Q: What does it do?
                                      A: It turns blue.

                                      1 Reply Last reply
                                      3
                                      • Z Zunneh

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

                                        VRoninV Offline
                                        VRoninV Offline
                                        VRonin
                                        wrote on last edited by
                                        #19

                                        @Zunneh said in Polymorphism didn't work:

                                        Flash is the same object created with lawl

                                        It is not. 2 new = 2 different objects

                                        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                        ~Napoleon Bonaparte

                                        On a crusade to banish setIndexWidget() from the holy land of Qt

                                        Z 1 Reply Last reply
                                        2
                                        • VRoninV VRonin

                                          @Zunneh said in Polymorphism didn't work:

                                          Flash is the same object created with lawl

                                          It is not. 2 new = 2 different objects

                                          Z Offline
                                          Z Offline
                                          Zunneh
                                          wrote on last edited by Zunneh
                                          #20

                                          @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 english is average, please use simple words and try to be the most explicit, thank you

                                          1 Reply Last reply
                                          0

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved