Which one is pure virtual function ?

Discussion in 'C++' started by sudhir, Mar 9, 2006.

  1. sudhir

    sudhir Guest

    I defined a pure virtual function like

    virtual void sum()=0; <--- pure virtual function

    but If I defined a virtual function in a base class in case of
    multilevel inheritance for the base pointer to point the right function
    with same signature in base and derived class like--

    virtual void sum() { }

    Now is this a pure virtual function or not and class containing this
    function is abstract class or not ?
    sudhir, Mar 9, 2006
    1. Advertisements

  2. sudhir

    Rolf Magnus Guest

    Could you please give a full example of what you mean? I couldn't really
    follow your description, and sometimes, a few lines of code can say more
    than a thousand words.
    Rolf Magnus, Mar 9, 2006
    1. Advertisements

  3. sudhir

    Daniel T. Guest

    The second function above is implemented (as a no op) so it isn't a pure
    virtual function. The class it is in may or may not be abstract
    depending on whether there are any unimplemented pure-virtual functions.
    Daniel T., Mar 9, 2006
  4. sudhir

    Jerry Coffin Guest

    The second is not a pure virtual function.

    The difference between the two is simple. In the first
    case, you cannot create an instance of this class. To
    create an instance, you must derive from this class, and
    the pure virtual function must be overridden in a derived

    In the second case, you can create an instance of the
    class. A derived class may, but is not required to,
    override this virtual function. Instances of the derived
    class can be created whether the function has been
    overridden or not.
    Jerry Coffin, Mar 9, 2006
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.