Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. how to be sure each child class inherits a method?

how to be sure each child class inherits a method?

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestionooptutorial
8 Posts 2 Posters 0 Views 1 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.
  • F Offline
    F Offline
    FriendOfAsherah
    wrote on last edited by
    #1

    I have a straight class hierarchy. The base class only has a method F() which calls another method P(). This method P() has to be implemented in all of the child classes and is called by polymorphism. How can I get sure (at compile time) that all child classes implement this method P()?

    class CBase
    {
    private:
    virtual void P() { printf("CBase::P\n"); }
    public:
    void F() { printf("Base: F()->P(): "), P(); }
    };

    class C1 : public CBase
    {
    private:
    virtual void P() override { printf("C1::P\n"); }
    };
    class C2 : public C1
    {
    private:
    virtual void P() override { printf("C2::P\n"); }
    };

    void main()
    {
    CBase* p = new CBase();
    p->F();
    p = new C1();
    p->F();
    p = new C2();
    p->F();
    }

    The above is the structure, when all is correct and will result in: Base: F()->P(): CBase::P Base: F()->P(): C1::P Base: F()->P(): C2::P Now I made a mistake and forgot to implement P() in class C1

    class C1 : public CBase
    {
    private:
    //virtual void P() override { printf("C1::P\n"); }
    };

    the compiler doesn´t error and it will result in Base: F()->P(): CBase::P Base: F()->P(): CBase::P Base: F()->P(): C2::P The only Idea is to use an Interface declaring P() abstract. But since C2 is derived from C1 from CBase how to do? Followeing code will show up an error on missing P(), but also shows up warnings when all is correct: _warning C4584: 'C1': base class 'PI' is already base of 'CBase' warning C4584: 'C2': base class 'PI' is already base of 'C1'

    struct PI
    {
    virtual void P() abstract;
    };

    class CBase : public PI
    {
    private:
    virtual void P() { printf("CBase::P\n"); }
    public:
    void F() { printf("Base: F()->P(): "), P(); }
    };

    class C1 : public CBase, public PI
    {
    private:
    virtual void P() override { printf("C1::P\n"); }
    };
    class C2 : public C1, public PI
    {
    private:
    virtual void P() override { printf("C2::P\n"); }
    };

    also it doesnt help at all because now I have to be sure to derive C1,C2, ... from PI too, not only from its parent So what I need is "method P() has to be declared in all child classes!- error if it is not"_

    L 2 Replies Last reply
    0
    • F FriendOfAsherah

      I have a straight class hierarchy. The base class only has a method F() which calls another method P(). This method P() has to be implemented in all of the child classes and is called by polymorphism. How can I get sure (at compile time) that all child classes implement this method P()?

      class CBase
      {
      private:
      virtual void P() { printf("CBase::P\n"); }
      public:
      void F() { printf("Base: F()->P(): "), P(); }
      };

      class C1 : public CBase
      {
      private:
      virtual void P() override { printf("C1::P\n"); }
      };
      class C2 : public C1
      {
      private:
      virtual void P() override { printf("C2::P\n"); }
      };

      void main()
      {
      CBase* p = new CBase();
      p->F();
      p = new C1();
      p->F();
      p = new C2();
      p->F();
      }

      The above is the structure, when all is correct and will result in: Base: F()->P(): CBase::P Base: F()->P(): C1::P Base: F()->P(): C2::P Now I made a mistake and forgot to implement P() in class C1

      class C1 : public CBase
      {
      private:
      //virtual void P() override { printf("C1::P\n"); }
      };

      the compiler doesn´t error and it will result in Base: F()->P(): CBase::P Base: F()->P(): CBase::P Base: F()->P(): C2::P The only Idea is to use an Interface declaring P() abstract. But since C2 is derived from C1 from CBase how to do? Followeing code will show up an error on missing P(), but also shows up warnings when all is correct: _warning C4584: 'C1': base class 'PI' is already base of 'CBase' warning C4584: 'C2': base class 'PI' is already base of 'C1'

      struct PI
      {
      virtual void P() abstract;
      };

      class CBase : public PI
      {
      private:
      virtual void P() { printf("CBase::P\n"); }
      public:
      void F() { printf("Base: F()->P(): "), P(); }
      };

      class C1 : public CBase, public PI
      {
      private:
      virtual void P() override { printf("C1::P\n"); }
      };
      class C2 : public C1, public PI
      {
      private:
      virtual void P() override { printf("C2::P\n"); }
      };

      also it doesnt help at all because now I have to be sure to derive C1,C2, ... from PI too, not only from its parent So what I need is "method P() has to be declared in all child classes!- error if it is not"_

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      If you make P a pure virtual function, then the compiler will spot if it has not been implemented:

      virtual void P() = 0; // a pure virtual function

      F 1 Reply Last reply
      0
      • L Lost User

        If you make P a pure virtual function, then the compiler will spot if it has not been implemented:

        virtual void P() = 0; // a pure virtual function

        F Offline
        F Offline
        FriendOfAsherah
        wrote on last edited by
        #3

        in which class, where to put this? in CBase? -> then CBase doesnt have its P() method itself in struct PI? ->still used this "=0" is same systax like "abstract"

        L 1 Reply Last reply
        0
        • F FriendOfAsherah

          in which class, where to put this? in CBase? -> then CBase doesnt have its P() method itself in struct PI? ->still used this "=0" is same systax like "abstract"

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          You put it in the CBase class then every class that inherits CBase must implement P.

          F 1 Reply Last reply
          0
          • L Lost User

            You put it in the CBase class then every class that inherits CBase must implement P.

            F Offline
            F Offline
            FriendOfAsherah
            wrote on last edited by
            #5

            does not work ... I can ommit P() in C2 without compile error because its stil defined in C1 also I miss the code of CBase::P() itself like:

            class CBase
            {
            private:
            virtual void P() abstract; //{ printf("CBase::P\n"); } // <= and whats about that code??
            public:
            void F() { printf("Base: F()->P(): "), P(); }
            };

            class C1 : public CBase
            {
            private:
            void P() override { printf("C1::P\n"); }
            };
            class C2 : public C1
            {
            private:
            //void P() override { printf("C2::P\n"); }
            };

            L 1 Reply Last reply
            0
            • F FriendOfAsherah

              does not work ... I can ommit P() in C2 without compile error because its stil defined in C1 also I miss the code of CBase::P() itself like:

              class CBase
              {
              private:
              virtual void P() abstract; //{ printf("CBase::P\n"); } // <= and whats about that code??
              public:
              void F() { printf("Base: F()->P(): "), P(); }
              };

              class C1 : public CBase
              {
              private:
              void P() override { printf("C1::P\n"); }
              };
              class C2 : public C1
              {
              private:
              //void P() override { printf("C2::P\n"); }
              };

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              The function C1::P() is declared private so it is not accessible in class C2. However, it is difficult to see exactly what problem you are trying to solve here.

              F 1 Reply Last reply
              0
              • L Lost User

                The function C1::P() is declared private so it is not accessible in class C2. However, it is difficult to see exactly what problem you are trying to solve here.

                F Offline
                F Offline
                FriendOfAsherah
                wrote on last edited by
                #7

                The structure is: CBase <- C1 <- C2 <- ... etc so straigt inheritance CBase contains the main method CBase::F() which does all calculations needed. And this method F() calls P() which should be choosen by inheritance (polymorphism) CBase::F() { ...; P(); ... } If I have CBase* b = new C2() than C2::P() will be called. If I have CBase* b = new C1() than C1::P() will be called. because of P() is a virtual method, overridden by C1::P().. C2::P() etc now I miss C1::P() (forgot to add it) => if CBase b=new C1() then CBase::F() will call CBase::P() instead of C1::P() because there is no C1::P() the override clause also tells the compiler to check if C1::P(), C2::P() match their parents P(). All is cointained by a complex chaotic SW-project which I will clean by small steps. The idea behind: P() is a call to a factory class which creates objects dependend on parameters AND on the class from which is called CBase::P(int type) creates objects of CPbase_ so CPbase_1, CPbase_2... C1::P(int type) creates obejcts of CP1_ so CP1_1, CP1_2... C2::P(int type) creates obejcts of CP2_ so CP2_1, CP2_2... etc... CBase::F(switch,type) : if (switch) P(type) else CBase::P(type) ... So if switch is set call the factory "of the class", else call the factory of CBase example: Cbase b = new C1(); b->F(true, 3) will create an object of C1_3() All the pointers are stored in a list and for each element in the list the creation-funbtion F() is called list ... C1*,CBase*,C3* etc ... foreach element in list , element->F(switch,type) ...

                1 Reply Last reply
                0
                • F FriendOfAsherah

                  I have a straight class hierarchy. The base class only has a method F() which calls another method P(). This method P() has to be implemented in all of the child classes and is called by polymorphism. How can I get sure (at compile time) that all child classes implement this method P()?

                  class CBase
                  {
                  private:
                  virtual void P() { printf("CBase::P\n"); }
                  public:
                  void F() { printf("Base: F()->P(): "), P(); }
                  };

                  class C1 : public CBase
                  {
                  private:
                  virtual void P() override { printf("C1::P\n"); }
                  };
                  class C2 : public C1
                  {
                  private:
                  virtual void P() override { printf("C2::P\n"); }
                  };

                  void main()
                  {
                  CBase* p = new CBase();
                  p->F();
                  p = new C1();
                  p->F();
                  p = new C2();
                  p->F();
                  }

                  The above is the structure, when all is correct and will result in: Base: F()->P(): CBase::P Base: F()->P(): C1::P Base: F()->P(): C2::P Now I made a mistake and forgot to implement P() in class C1

                  class C1 : public CBase
                  {
                  private:
                  //virtual void P() override { printf("C1::P\n"); }
                  };

                  the compiler doesn´t error and it will result in Base: F()->P(): CBase::P Base: F()->P(): CBase::P Base: F()->P(): C2::P The only Idea is to use an Interface declaring P() abstract. But since C2 is derived from C1 from CBase how to do? Followeing code will show up an error on missing P(), but also shows up warnings when all is correct: _warning C4584: 'C1': base class 'PI' is already base of 'CBase' warning C4584: 'C2': base class 'PI' is already base of 'C1'

                  struct PI
                  {
                  virtual void P() abstract;
                  };

                  class CBase : public PI
                  {
                  private:
                  virtual void P() { printf("CBase::P\n"); }
                  public:
                  void F() { printf("Base: F()->P(): "), P(); }
                  };

                  class C1 : public CBase, public PI
                  {
                  private:
                  virtual void P() override { printf("C1::P\n"); }
                  };
                  class C2 : public C1, public PI
                  {
                  private:
                  virtual void P() override { printf("C2::P\n"); }
                  };

                  also it doesnt help at all because now I have to be sure to derive C1,C2, ... from PI too, not only from its parent So what I need is "method P() has to be declared in all child classes!- error if it is not"_

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  1. Change all private into 'protected' 2. For testing purpose do trial and error of pure virtual test on each level. Best wishes

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups