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
    1. Advertising

  2. sudhir

    Rolf Magnus Guest

    sudhir wrote:

    > 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 ?


    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
    #2
    1. Advertising

  3. sudhir

    Daniel T. Guest

    In article <>,
    "sudhir" <> wrote:

    > 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 ?


    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.

    --
    Magic depends on tradition and belief. It does not welcome observation,
    nor does it profit by experiment. On the other hand, science is based
    on experience; it is open to correction by observation and experiment.
     
    Daniel T., Mar 9, 2006
    #3
  4. sudhir

    Jerry Coffin Guest

    In article <1141900843.424145.167110
    @u72g2000cwu.googlegroups.com>,
    says...
    > 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 ?


    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
    class.

    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.

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
     
    Jerry Coffin, Mar 9, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. IK
    Replies:
    2
    Views:
    628
    hemraj
    Jul 23, 2004
  2. Todd Aspeotis
    Replies:
    3
    Views:
    489
    Kanenas
    May 30, 2005
  3. John Goche
    Replies:
    10
    Views:
    784
    Marcus Kwok
    Dec 8, 2006
  4. Replies:
    7
    Views:
    602
    James Kanze
    May 2, 2007
  5. a
    Replies:
    7
    Views:
    376
    dasjotre
    Jun 28, 2007
Loading...

Share This Page