Thread: Question on polymorphism

  1. #1
    Registered User
    Join Date
    Nov 2006

    Question on polymorphism

    I know that to truly partake in polymorphism a class must have at least one virtual method. My question is, what if a class "A" inherits from a base class "B" that has a virtual method, does "A" also need a virtual method for it to be able to be inherited (and partake in polymorphism) properly or does the virtual method from the base class cover that?

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Toronto, ON
    No, once a method is declared as virtual, it stays virtual in all child classes, whether the derived classes specify it as virtual or not.
    But I usually specify virtual all the way down the chain just for consistency and to remind you that it is virtual without having to look at all the base classes to find out.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    The inherited method (i.e., virtual member function) is also A's method, even if the virtual keyword is not used when defining A.
    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

  4. #4
    Registered User
    Join Date
    Apr 2008
    Before you go whole hog with inheritance and polymorphism, I'd recommend reading this article.

    Liskov Substitution Principle

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 newbie question
    By Swerve in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 08:38 PM
  3. Question...
    By TechWins in forum A Brief History of
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. A question Polymorphism
    By omeydhomey in forum C++ Programming
    Replies: 5
    Last Post: 03-09-2003, 11:28 PM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM