Thread: inheritance, polymorphism, and virtual functions

  1. #1
    Allways learning cs_student's Avatar
    Join Date
    Aug 2008
    Location
    ~/
    Posts
    39

    inheritance, polymorphism, and virtual functions

    I have read up on these ideas as well as used them in many trivial programs. ie animal-mammal-dog|cat|ape.

    I can't see how it would be practical to use in a real situation, however. Could someone please enlighten me, or lead me to an article which might?

    Thanks for your time and effort,

    cs_student

  2. #2
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    To my understanding:

    Virtual functins and Polymorphism are based off of inheritance. When you inherit from a class you receive access to its members(with certain limits). If a function in the parent class is virtual, you have the opportunity to change it. This is useful because what if the function in your base class doesn't match the needs of your new class exactly? Polymorphism as I have seen it is most useful in a sort of generic programming sense. You can program methods/functions to accept paramaters for your base class, and just let the compiler work out what you have actually passed it. Now you can build one function to handle all of your classes on your particular inheritance hiearchy.

  3. #3
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    I can't see how it would be practical to use in a real situation, however.
    ^_^

    Congratulations! If the only exposure you've had is textbook '[animal]<-[mammal]<-[[dog]|[cat]]' that is the correct conclusion.

    Rather simply: runtime polymorphisms is the goal of virtual functions as implemented through inheritance. With polymorphisms in mind, the correct model is usually the one with the widest correct applicability of algorithms. "Real world" relationships are rarely all that useful form this perspective.

    A good example of polymorphisms: the unix view of the world as "everything is a file".

    Soma

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by valaris View Post
    This is useful because what if the function in your base class doesn't match the needs of your new class exactly?
    No, that's not quite right.
    You can still override functions from the base class in your derived classes.
    Applying virtual to it only means that the "correct" function will be called if you call the function on an object passed via a pointer or reference to the base class.

    The point of polymorphism is that you can have an unlimited number of objects of the same type and the same interface and work with those from a single function.
    Say you have a couple of documents. They are all documents, but of different types.
    So they all inherit from a base class called CDocument. CDocument has a virtual function called Process.
    All these document classes inherit from CDocument.
    In your app, when you need to "process" these documents, you might call a function called ProcessDocument that takes a CDocument& argument.
    The function calls the Process member function (and some other stuff).
    Regardless of what type of object you ACTUALLY passed in, the code will execute correctly due to the polymorphism. The compiler will call the function in the ACTUAL class you passed as argument and not the function in CDocument.
    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.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Here's a real world example from the Irrlicht 3D graphics API. Note that getBoundingBox() and render() are pure virtual functions in the ISceneNode interface meant to be implemented in subclasses.

    http://irrlicht.sourceforge.net/docu...cene_node.html

  6. #6
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Quote Originally Posted by Elysia View Post
    No, that's not quite right.
    You can still override functions from the base class in your derived classes.
    Applying virtual to it only means that the "correct" function will be called if you call the function on an object passed via a pointer or reference to the base class.

    The point of polymorphism is that you can have an unlimited number of objects of the same type and the same interface and work with those from a single function.
    Say you have a couple of documents. They are all documents, but of different types.
    So they all inherit from a base class called CDocument. CDocument has a virtual function called Process.
    All these document classes inherit from CDocument.
    In your app, when you need to "process" these documents, you might call a function called ProcessDocument that takes a CDocument& argument.
    The function calls the Process member function (and some other stuff).
    Regardless of what type of object you ACTUALLY passed in, the code will execute correctly due to the polymorphism. The compiler will call the function in the ACTUAL class you passed as argument and not the function in CDocument.
    Thanks :P I didn't know you could still override functions if they weren't virtual. So if you declare foo() in the base class and foo() in the child, and then make an instance of child and call foo; the child's foo will be called or the base? Assuming these functions weren't virtual...

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by valaris View Post
    Thanks :P I didn't know you could still override functions if they weren't virtual. So if you declare foo() in the base class and foo() in the child, and then make an instance of child and call foo; the child's foo will be called or the base? Assuming these functions weren't virtual...
    An instance of the child will call foo in the child. Where the problem comes is if you have a BASE-pointer holding an address of a child, and then call through that, it will call the base function, because the compiler can not know that the pointer is pointing to a child class.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by matsp View Post
    ...Where the problem comes is if you have a BASE-pointer holding an address of a child, and then call through that, it will call the base function, because the compiler can not know that the pointer is pointing to a child class.
    Which comes round to the actual problem being discussed
    Making the functions virtual will then cause the compiler to call the child's foo function instead of the base's, since seeing as the function is virtual, the compiler will look up the ACTUAL* type of the object and call the appropriate function.

    *) Of course, that's not quite how it usually works on real world compiler implementations, but it's a simplification
    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.

  9. #9
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    Quote Originally Posted by cs_student View Post
    I have read up on these ideas as well as used them in many trivial programs. ie animal-mammal-dog|cat|ape.

    I can't see how it would be practical to use in a real situation, however. Could someone please enlighten me, or lead me to an article which might?

    Thanks for your time and effort,

    cs_student
    they are massively useful.

    in my application, i have to acquire measurements from many different instruments of different kinds. i can create a single queue of instances of the objects' base class, and use a virtual getter method to have each execute the derived behavior.

    using virtual methods allows you to execute varying behavior and retain the ability to aggregate objects into containers without relying on RTTI.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. mixed template and virtual polymorphism
    By m37h0d in forum C++ Programming
    Replies: 0
    Last Post: 06-09-2009, 07:15 AM
  2. Polymorphism; Update functions and accessibility
    By CaptainMaxxPow in forum C# Programming
    Replies: 2
    Last Post: 04-23-2009, 08:48 AM
  3. Platform game gravity problem
    By Akkernight in forum Game Programming
    Replies: 33
    Last Post: 02-22-2009, 01:10 PM
  4. Polymorphism, inheritance and containers
    By musicalhamster in forum C++ Programming
    Replies: 6
    Last Post: 11-27-2007, 10:23 AM
  5. virtual functions in polymorphism
    By omeydhomey in forum C++ Programming
    Replies: 3
    Last Post: 03-15-2003, 04:51 PM