Pure Virtual Function declaration

Discussion in 'C++' started by srivant.samir@gmail.com, Sep 11, 2012.

  1. Guest

    What is the difference between two forms of following virtual functions :-

    virtual void foo const =0;

    virtual void foo {}
    , Sep 11, 2012
    #1
    1. Advertising

  2. On 9/10/12 11:24 PM, wrote:
    > What is the difference between two forms of following virtual functions :-
    >
    > virtual void foo const =0;
    >
    > virtual void foo {}
    >


    first, neither is a function since there is no parameter list with
    either of them, not even an empty ().


    assuming you mean between:

    virtual void foo() const =0;
    virtual void foo() {}

    the first defines a function, that
    1) promises that it, and all version that over ride it, will not change
    the object it is called on (except for mutable members),and

    2) must be over-ridden by a deriving class (and thus no object of this
    class can exist as full object, as there must be a deriving class.


    The second form just defines a function that does nothing, it CAN be
    over-ridden is a derived class, but it doesn't need to be. That
    over-ridding version is also allowed to change the object it is called
    on. an object of this class might be able to be defined (assuming
    nothing else prohibits it).
    Richard Damon, Sep 11, 2012
    #2
    1. Advertising

  3. On Tuesday, September 11, 2012 7:54:47 AM UTC+4:30, (unknown) wrote:
    > What is the difference between two forms of following virtual functions :-
    >
    >
    >
    > virtual void foo const =0;
    >
    >
    >
    > virtual void foo {}


    Hi

    At first I assume you meant:
    virtual void foo1() =0;
    virtual void foo2();
    for more clarity, I use foo1 and foo2.
    Also, please note at the moment the const
    qualifier doesn't affect on our discussion
    Both are virtual functions, and
    virtual means: may be redefined later in a class derived
    from this one.
    More precisely the first one is "pure" virtual function.
    Using pure virtual function, we can make an abstract
    class like this:
    class C1 {
    public:
    virtual void foo1() =0;
    };
    Please note you "can" define foo1 but
    you can't create an object of C1, because C1
    has one pure virtual function. You can derive
    a class like D from C1 and -re-define foo1:
    class D : public C1 {
    public:
    void foo1() {/* ... */ }
    };
    foo2 isn't pure virtual. It's just virtual:
    class C2 {
    virtual void foo2() { /* ... */ }
    };
    You "have to" define foo2 at class C1 because it's a C++ rule:
    A virtual function must be defined in the class in which
    they are first declared.
    Also a derived class like D can -re-define foo2.
    It's obvious C2 isn't abstract and you can make object of
    that.

    HTH,
    -- Saeed Amrollahi Boyouki
    Saeed Amrollahi, Sep 11, 2012
    #3
  4. Guest

    On Tuesday, September 11, 2012 8:54:47 AM UTC+5:30, (unknown) wrote:
    > What is the difference between two forms of following virtual functions :-
    >
    >
    >
    > virtual void foo const =0;
    >
    >
    >
    > virtual void foo {}



    First one is Pure virtual functions which is nothing but definition is there in Derived classes.

    Second one is Virtual functions i.r run time polymorphisam

    for more information
    http://ccppcoding.blogspot.in/
    , Sep 19, 2012
    #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:
    606
    hemraj
    Jul 23, 2004
  2. kelvSYC
    Replies:
    6
    Views:
    7,221
    Richard Herring
    May 17, 2005
  3. John Goche
    Replies:
    10
    Views:
    736
    Marcus Kwok
    Dec 8, 2006
  4. Replies:
    7
    Views:
    586
    James Kanze
    May 2, 2007
  5. a
    Replies:
    7
    Views:
    362
    dasjotre
    Jun 28, 2007
Loading...

Share This Page