Thread: partial polymorphism

  1. #1
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251

    partial polymorphism

    What if I want a derived class to first execute the base class method and then its own method redefinition?

    I tried the following but it didn't work

    Code:
    #include <iostream>
    
    class Base_c
    {
    public:
        void vMethod(){std:: cout << "base" << std::endl;}
    };
    
    class Derived_c : public Base_c
    {
         virtual void vMethod(){std::cout << "intermediate" << std::endl;}
    };
    
    class Derived_2_c : public Derived_c
    {
         virtual void vMethod(){std::cout << "derived" << std::endl;}
    };
    
    int main()
    {
          Base_c *b;
          Derived_2_c d;
          b = &d;
          b->vMethod();
    }
    i would like it to produce the (double) output
    base
    derived

    it would be useful to handle both the base fields and the derived fuelds as well

  2. #2
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251
    I tiried this and it worked but could you suggest something else?

    Code:
    #include <iostream>
    
    class Base_c
    {
    public:
        void vMethod(){std:: cout << "base" << std::endl;}
    };
    
    class Derived_c : public Base_c
    {
    public:
         virtual void vMethod(){std::cout << "intermediate" << std::endl;}
    };
    
    class Derived_2_c : public Derived_c
    {
         virtual void vMethod(){std::cout << "derived" << std::endl;}
    };
    
    
            Derived_c *b;
            Derived_2_c d;
            b = &d;
            ((Base_c*)b)->vMethod();
            b->vMethod();
            
            getchar();

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    #include <iostream>
    
    class Base
    {
    public:
        virtual void vMethod() { std:: cout << "base" << std::endl; }
    };
    
    class Derived: public Base
    {
    public:
        virtual void vMethod()
        {
            Base::vMethod();
            std::cout << "intermediate" << std::endl;
        }
    };
    
    int main()
    {
          Derived d;
          Base& b = d;
          b.vMethod();
    }
    The base, the class to whose type you have a pointer to reference must have the function declared virtual. Otherwise the function won't be virtual.
    You forgot to make the vMethod in derived public. It should be, since you are calling the vMethod.
    By prefixing the name of the base class and "::", I can tell the compiler that the following function shall be in this namespace. So it pretty much tells the compiler to call the function vMethod in the base class.
    Last edited by Elysia; 12-01-2008 at 05:43 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251
    Quote Originally Posted by Elysia View Post
    Code:
    #include <iostream>
    
    class Base
    {
    public:
        virtual void vMethod() { std:: cout << "base" << std::endl; }
    };
    
    class Derived: public Base
    {
    public:
        virtual void vMethod()
        {
            Base::vMethod();
            std::cout << "intermediate" << std::endl;
        }
    };
    
    int main()
    {
          Derived d;
          Base& b = d;
          b.vMethod();
    }
    great
    sorry I am a newbie

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You should also read up a little on polymorphism. After all, examples don't do justice and you need to learn what it actually does.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251
    Quote Originally Posted by Elysia View Post
    You forgot to make the vMethod in derived public. It should be, since you are calling the vMethod.
    but it worked (i tried it and i had the output
    base
    derived
    )

  7. #7
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251
    Quote Originally Posted by Elysia View Post
    You should also read up a little on polymorphism. After all, examples don't do justice and you need to learn what it actually does.
    could you suggest good links?

  8. #8
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251
    Quote Originally Posted by mynickmynick View Post
    but it worked (i tried it and i had the output
    base
    derived
    )
    probably it inherited the public propriety by Derived_c

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by mynickmynick View Post
    could you suggest good links?
    http://cboard.cprogramming.com/showthread.php?t=74078

    Quote Originally Posted by mynickmynick View Post
    probably it inherited the public propriety by Derived_c
    Well, yes, it did, but then you overrode it with a private one, so it becomes private.
    It is one of the things that is syntactically correct, but not semantically correct.
    There was a thread about that some time ago... hrm...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by mynickmynick View Post
    but it worked (i tried it and i had the output
    base
    derived
    )
    It worked, but compare the two codes. You have to cast to do what you want. That is not good. The point is to write something simple and to be clear. It would be simpler to have a method2 in the derived class rather than cast!

    Your code works, because the intermediate class has two vMethods(). One from the base class (Base) and one from its own.
    In other words, one Base::vMethod and one Derived::vMethod.
    The compiler chooses the vMethod more "appropriate". It the pointer points to the Base class, then it uses the Base::Method. It at the derived it uses the Derived::vMethod.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Btw, public/private is enforced at compile time. Because the compiler sees that the type is Base, it checks in Base and sees that it is public, and so does not generate a compile error.
    But in reality, Derived's method is called, which is private, which pretty much defies the purpose of those keywords.
    Consider that, and why I suggested you add public to the derived class.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by mynickmynick
    What if I want a derived class to first execute the base class method and then its own method redefinition?
    If it so happens that you want to call the base class' method from the derived class' method then Elysia's suggestion applies since it actually does that. On the other hand, if you always want to call the base class' method and then allow derived classes to do something extra, then you could use the non-virtual interface idiom, e.g.,
    Code:
    #include <iostream>
    
    class Base
    {
    public:
        void method()
        {
            std::cout << "base" << std::endl;
            vMethod();
        }
    
        ~Base() {}
    private:
        virtual void vMethod() {}
    };
    
    class Derived : public Base
    {
    private:
        virtual void vMethod()
        {
            std::cout << "derived" << std::endl;
        }
    };
    
    int main()
    {
        Derived d;
        Base& b = d;
        b.method();
    }
    The member function that is made public and called in non-virtual, but a virtual member function provides a hook by which a derived class can do something extra. If there is an intermediate base class then you may have to add another hook method or resort to what Elysia suggested in the most derived class' method.

    Note that the base class destructor is declared virtual. If you do not know why, then read the answer to this FAQ: When should my destructor be virtual?

    Quote Originally Posted by mynickmynick
    could you suggest good links?
    Read this FAQ section: Inheritance — proper inheritance and substitutability
    Last edited by laserlight; 12-01-2008 at 08:01 AM. Reason: Whoops, wrong closing bbcode tag.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251
    [QUOTE=laserlight;812545]If it so happens that you want to call the base ..
    QUOTE]

    thank you all

  14. #14
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251
    what if the arguments of the derived method change?

  15. #15
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Then the derived method is no longer an override of the base method. For virtual functions, the arguments cannot change in C++.

    (In type theory, they may change in specific ways, but C++ doesn't support this.)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polymorphism - "pointers" or "references"?
    By Petike in forum C++ Programming
    Replies: 10
    Last Post: 06-04-2009, 05:06 PM
  2. polymorphism with vars?
    By mynickmynick in forum C++ Programming
    Replies: 6
    Last Post: 01-07-2009, 03:53 AM
  3. Why do I get partial web-pages with recv?
    By hardi in forum Networking/Device Communication
    Replies: 11
    Last Post: 12-28-2006, 02:50 PM
  4. Replies: 3
    Last Post: 10-31-2005, 12:05 PM
  5. Partial Specialization error?
    By cboard_member in forum C++ Programming
    Replies: 10
    Last Post: 09-05-2005, 04:23 AM