Question on polymorphism

This is a discussion on Question on polymorphism within the C++ Programming forums, part of the General Programming Boards category; I know that to truly partake in polymorphism a class must have at least one virtual method. My question is, ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    184

    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
    Location
    Toronto, ON
    Posts
    3,545
    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
    Location
    Singapore
    Posts
    22,119
    The inherited method (i.e., virtual member function) is also A's method, even if the virtual keyword is not used when defining A.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    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, 06:06 PM
  2. Polymorphism newbie question
    By Swerve in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 09:38 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 10: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21