virtual vs pure virtual member function

Discussion in 'C++' started by sam_cit@yahoo.co.in, Apr 30, 2007.

  1. Guest

    Hi Everyone,

    I wanted to know as to what is the exact difference between a virtual
    function and a pure virtual function?
    Thanks in advance!!!
    , Apr 30, 2007
    #1
    1. Advertising

  2. Jim Langston Guest

    <> wrote in message
    news:...
    > Hi Everyone,
    >
    > I wanted to know as to what is the exact difference between a virtual
    > function and a pure virtual function?
    > Thanks in advance!!!


    class MyClass
    {
    public:
    virtual void MyFunction() { }; // Virtual
    };

    class MyDerived: public MyClass
    {
    };

    That will compile.

    class MyClass2
    {
    public:
    virtual void MyFunction() = 0; // Pure Virtual
    };

    class MyDerived2: pubic MyClass2
    {
    };

    That will not compile. Anything that derives from a class with a pure
    virtual must override the method as none is defined.

    A method needs to be virtual so the compiler will look for a derived
    override in a base class. If a method is pure virtual, a derived class must
    override the method.
    Jim Langston, Apr 30, 2007
    #2
    1. Advertising

  3. Guest

    On Apr 30, 9:31 am, wrote:
    > Hi Everyone,
    >
    > I wanted to know as to what is the exact difference between a virtual
    > function and a pure virtual function?
    > Thanks in advance!!!


    If you declare a virtual function in a class say for example B, you
    actually say: The function may be redefined in derived classes.

    class B {
    public:
    virtual void f() { /* ... */ }
    };

    You define the f() for B and you usually redefine the function in
    derived class:
    class D : public B {
    public:
    void f() {/* another defintion */ }
    };
    virtual function is a primary tool for polymorphic bevaviour. If you
    offer no defintion for D::f(), you will use the definition of B::f(),
    because as an Inheritance principle, D inherited the public members of
    B. Also you can declare f() in D as a virtual function, and the
    redefinition process is continued in the classes that derived form D.
    pure virtual function is a kind of virtual functions with a specific
    syntax:
    class B {
    public:
    virtual void f() =0; // =0 means pure virtual
    };
    if a class has at least one pure virtual function, it will be abstract
    class, so instance creation is impossible. B::f() says you should
    implement f() in derived classes:
    class D : public B {
    void f() { /* ... */ }
    };
    If you do not implement f() in D, the D is abstract class by default,
    because it inherits all the pure virtual functions of class B.
    for more detailed (and of course better) description, please see the
    following references:
    . C++ Standard Draft: Chapter 10
    . The C++ Programming Language (3rd edition) by Bjarne Stroustrup:
    Chapter 12.
    . Design and Evolution of C++ by Bjarne Stroustrup: Chapter under
    the title of "Class Concepts Refinements"

    Regards,
    , Apr 30, 2007
    #3
  4. James Kanze Guest

    On Apr 30, 7:31 am, wrote:
    > I wanted to know as to what is the exact difference between a virtual
    > function and a pure virtual function?


    A class which contains a pure virtual function is abstract, and
    cannot be instantiated. A function call using dynamic
    resolution results in undefined behavior if it would resolve to
    a pure virtual function, and a pure virtual function is not
    automatically considered "used", and so doesn't have to be
    implemented unless it is actually called (e.g. by means of a
    scope resolution operator, or implicitly from the destructor of
    a derived class, if it is the destructor).

    Thus:

    class Base
    {
    public:
    virtual void f() ; // Must be defined...
    virtual void g() = 0 ;
    virtual void h() = 0 ;

    Base()
    {
    f() ; // calls Base::f()...
    g() ; // undefined behavior...
    Base::h() ; // calls Base::h, which
    // must be defined.
    }
    } ;

    Note that it is not necessary to defined Base::g() unless some
    derived class calls Base::g().

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Apr 30, 2007
    #4
  5. Prashanth Guest

    > class MyClass
    > {
    > public:
    > virtual void MyFunction() { }; // Virtual
    >
    > };
    >
    > class MyDerived: public MyClass
    > {
    >
    > };
    >
    > That will compile.
    >
    > class MyClass2
    > {
    > public:
    > virtual void MyFunction() = 0; // Pure Virtual
    >
    > };
    >
    > class MyDerived2: pubic MyClass2
    > {
    >
    > };
    >
    > That will not compile. Anything that derives from a class with a pure
    > virtual must override the method as none is defined.


    No. It will compile. MyDerived2 will be an abstract class and you
    cannot create an instance of it.

    > If a method is pure virtual, a derived class must
    > override the method.


    Not necessarily, unless one wants to create an instance of the derived
    class.
    Prashanth, Apr 30, 2007
    #5
  6. red floyd Guest

    red floyd, Apr 30, 2007
    #6
  7. anon Guest

    Prashanth wrote:
    >> class MyClass
    >> {
    >> public:
    >> virtual void MyFunction() { }; // Virtual
    >>
    >> };
    >>
    >> class MyDerived: public MyClass
    >> {
    >>
    >> };
    >>
    >> That will compile.
    >>
    >> class MyClass2
    >> {
    >> public:
    >> virtual void MyFunction() = 0; // Pure Virtual
    >>
    >> };
    >>
    >> class MyDerived2: pubic MyClass2
    >> {
    >>
    >> };
    >>
    >> That will not compile. Anything that derives from a class with a pure
    >> virtual must override the method as none is defined.

    >
    > No. It will compile. MyDerived2 will be an abstract class and you
    > cannot create an instance of it.


    If it compiles for you, it means your compiler is broken. This is what I
    get with gcc 4.1.2:

    [zzz@cherry data_create]$ g++ jj.cpp -o jj
    jj.cpp: In function ‘int main()’:
    jj.cpp:13: error: cannot declare variable ‘a’ to be of abstract type
    ‘MyDerived2’
    jj.cpp:8: note: because the following virtual functions are pure
    within ‘MyDerived2’:
    jj.cpp:4: note: virtual void MyClass2::MyFunction()

    BTW there is a typo in the example. This is correct:

    class MyClass2
    {
    public:
    virtual void MyFunction() = 0;
    };

    class MyDerived2 : public MyClass2
    {
    };

    int main()
    {
    MyDerived2 a;
    }

    >
    >> If a method is pure virtual, a derived class must
    >> override the method.

    >
    > Not necessarily, unless one wants to create an instance of the derived
    > class.
    >


    What is a point of a class, if it is not used??
    anon, May 1, 2007
    #7
  8. James Kanze Guest

    On May 1, 3:25 pm, anon <> wrote:
    > Prashanth wrote:
    > >> class MyClass
    > >> {
    > >> public:
    > >> virtual void MyFunction() { }; // Virtual
    > >> };


    > >> class MyDerived: public MyClass
    > >> {
    > >> };


    > >> That will compile.


    > >> class MyClass2
    > >> {
    > >> public:
    > >> virtual void MyFunction() = 0; // Pure Virtual
    > >> };


    > >> class MyDerived2: pubic MyClass2
    > >> {
    > >> };


    > >> That will not compile. Anything that derives from a class with a pure
    > >> virtual must override the method as none is defined.


    > > No. It will compile. MyDerived2 will be an abstract class and you
    > > cannot create an instance of it.


    > If it compiles for you, it means your compiler is broken.


    The standard requires that it compile. It compiles with every
    compiler I've ever used.

    > This is what I
    > get with gcc 4.1.2:


    > [zzz@cherry data_create]$ g++ jj.cpp -o jj
    > jj.cpp: In function ?int main()?:
    > jj.cpp:13: error: cannot declare variable ?a? to be of abstract type
    > ?MyDerived2?
    > jj.cpp:8: note: because the following virtual functions are pure
    > within ?MyDerived2?:
    > jj.cpp:4: note: virtual void MyClass2::MyFunction()


    Obviously, you've added some code which is illegal.

    > BTW there is a typo in the example. This is correct:


    > class MyClass2
    > {
    > public:
    > virtual void MyFunction() = 0;
    > };


    > class MyDerived2 : public MyClass2
    > {
    > };


    > int main()
    > {
    > MyDerived2 a;


    Mainly, this. That wasn't in Prashanth's example, and in fact,
    he explicitly said that it wasn't legal.

    > }


    > >> If a method is pure virtual, a derived class must
    > >> override the method.


    > > Not necessarily, unless one wants to create an instance of the derived
    > > class.


    > What is a point of a class, if it is not used??


    Who said you cannot use it? You cannot create an instance of it
    (as a full object, at any rate). You can still use it as a base
    class, for further derivation, or use it in any context that
    doesn't require an instance.


    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, May 2, 2007
    #8
    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:
    588
    hemraj
    Jul 23, 2004
  2. Todd Aspeotis
    Replies:
    3
    Views:
    443
    Kanenas
    May 30, 2005
  3. a
    Replies:
    7
    Views:
    352
    dasjotre
    Jun 28, 2007
  4. Rahul
    Replies:
    3
    Views:
    339
    Salt_Peter
    Dec 2, 2007
  5. Replies:
    4
    Views:
    764
    Ben C
    Mar 29, 2008
Loading...

Share This Page