pure virtual

Discussion in 'C++' started by Guest, Sep 27, 2003.

  1. Guest

    Guest Guest

    I don't understand. What's the difference in practice between 2 next lines?
    When I *must* use first line and when second?
    -------------
    virtual int a(int b) = 0;
    virtual int a(int b);
    -------------

    thank you
     
    Guest, Sep 27, 2003
    #1
    1. Advertising

  2. Guest

    Siana Guest

    On Sat, 27 Sep 2003 20:13:31 +0300, "<- Chameleon ->"
    <> wrote:

    >I don't understand. What's the difference in practice between 2 next lines?
    >When I *must* use first line and when second?
    >-------------
    > virtual int a(int b) = 0;
    > virtual int a(int b);
    >-------------
    >
    >thank you
    >


    The first line declares a function a in some class that has no
    definition - it is there only to be overridden by derived classes for
    the purposes of polymorphism. It also makes the class abstract (unable
    to be instantiated). Derived classes must declare and implement this
    function or else they too are abstract and un-instatiable. It is
    useful in defining an interface.

    The second line declares a function a in some class that must have an
    implementation for that class. That implementation will be inherited
    by derived classes but can still be overridden and perform
    polymorphically.

    S.
     
    Siana, Sep 27, 2003
    #2
    1. Advertising

  3. "<- Chameleon ->" <> wrote in message
    news:bl4gcb$ar2$...
    > I don't understand. What's the difference in practice between 2 next

    lines?
    > When I *must* use first line and when second?
    > -------------
    > virtual int a(int b) = 0;
    > virtual int a(int b);
    > -------------
    >
    > thank you
    >
    >


    You may want to look up 'pure virtual'

    The first one would be used when defining a pure virtual base class. This
    is done in situations where code needs to call the method 'a' for objects of
    types which derive from the class but where you need the derived class to
    implement the actual method (derived class *must* implement the method). If
    we define:

    class C
    {
     
    Thomas Wintschel, Sep 27, 2003
    #3
  4. Siana wrote:

    > On Sat, 27 Sep 2003 20:13:31 +0300, "<- Chameleon ->"
    > <> wrote:
    >
    >
    >>I don't understand. What's the difference in practice between 2 next lines?
    >>When I *must* use first line and when second?
    >>-------------
    >>virtual int a(int b) = 0;
    >>virtual int a(int b);
    >>-------------
    >>
    >>thank you
    >>

    >
    >
    > The first line declares a function a in some class that has no
    > definition


    That is not true in general. A pure virtual function certainly may have
    a definition.

    > - it is there only to be overridden by derived classes for
    > the purposes of polymorphism. It also makes the class abstract (unable
    > to be instantiated). Derived classes must declare and implement this
    > function or else they too are abstract and un-instatiable. It is
    > useful in defining an interface.
    >


    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
     
    Kevin Goodsell, Sep 27, 2003
    #4
  5. Guest

    Ron Natalie Guest

    "Siana" <> wrote in message news:...

    > The first line declares a function a in some class that has no
    > definition


    Wrong answer. It is the designation of the function as pure virtual.
    It does not preclude the function from having a definition. It makes
    the class it is defined in abstract. An abstract class is one that can
    not be instantiated. It serves only as a base class for others.

    > - it is there only to be overridden by derived classes for
    > the purposes of polymorphism.


    Not quite, it means that it must be overridden in dervied classes
    (as well as all other pure virtual functions) in order to be concrete
    (that is, one that can have instances created of it).

    > The second line declares a function a in some class that must have an
    > implementation for that class. That implementation will be inherited
    > by derived classes but can still be overridden and perform
    > polymorphically.


    No it just declares one that is not pure. It means that it doesn't make
    the class abstract.

    While it is the case that you can omit the implementation of a pure virtual
    function, that is secondary to it's meaning.
     
    Ron Natalie, Sep 29, 2003
    #5
  6. Guest

    jeffc Guest

    "<- Chameleon ->" <> wrote in message
    news:bl4gcb$ar2$...
    > I don't understand. What's the difference in practice between 2 next

    lines?
    > When I *must* use first line and when second?
    > -------------
    > virtual int a(int b) = 0;
    > virtual int a(int b);
    > -------------


    There is no time when you *must* use the first (pure virtual). It's a
    design decision on your part. There are 2 reasons you might use it
    a) you want to force any class that inherits from this class to define
    function a
    b) you want that class to be abstract (no one can make an instance of it -
    you can only make instances of subclasses). This is a rather confusing
    means of creating an abstract class, but that's the way it is.
     
    jeffc, Sep 29, 2003
    #6
  7. Guest

    jeffc Guest

    "Thomas Wintschel" <> wrote in message
    news:KYkdb.12037$o21.6208@edtnps84...
    >
    > The first one would be used when defining a pure virtual base class.


    You mean an abstract class.
     
    jeffc, Sep 29, 2003
    #7
  8. Guest

    Ron Natalie Guest

    "jeffc" <> wrote in message news:...
    >
    > "Thomas Wintschel" <> wrote in message
    > news:KYkdb.12037$o21.6208@edtnps84...
    > >
    > > The first one would be used when defining a pure virtual base class.

    >
    > You mean an abstract class.
    >

    Now you're being pedantic :)
     
    Ron Natalie, Sep 29, 2003
    #8
  9. Guest

    jeffc Guest

    "Ron Natalie" <> wrote in message
    news:3f7849ee$0$36943$...
    >
    > "jeffc" <> wrote in message

    news:...
    > >
    > > "Thomas Wintschel" <> wrote in message
    > > news:KYkdb.12037$o21.6208@edtnps84...
    > > >
    > > > The first one would be used when defining a pure virtual base class.

    > >
    > > You mean an abstract class.
    > >

    > Now you're being pedantic :)


    I knew somone was going to say that :) The point to my way of thinking is
    that "pure virtual base class" is not an OO term, it's not a C++ term, it's
    just not any term I've ever heard. I did not of course object to "method",
    nor would I even have objected if he used the term "abstract method" for
    pure virtual function. I'd be interested in knowing if that's what Thomas
    really meant to write and where he heard it. Maybe I should have said "You
    mean an abstract class, right?"
     
    jeffc, Sep 29, 2003
    #9
  10. Guest

    Ron Natalie Guest

    "jeffc" <> wrote in message news:...
    >
    > "Ron Natalie" <> wrote in message
    > news:3f7849ee$0$36943$...
    > >
    > > "jeffc" <> wrote in message

    > news:...
    > > >
    > > > "Thomas Wintschel" <> wrote in message
    > > > news:KYkdb.12037$o21.6208@edtnps84...
    > > > >
    > > > > The first one would be used when defining a pure virtual base class.
    > > >
    > > > You mean an abstract class.
    > > >

    > > Now you're being pedantic :)

    >
    > I knew somone was going to say that :)


    I was just ribbing ya.
     
    Ron Natalie, Sep 29, 2003
    #10
    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:
    612
    hemraj
    Jul 23, 2004
  2. Todd Aspeotis
    Replies:
    3
    Views:
    471
    Kanenas
    May 30, 2005
  3. John Goche
    Replies:
    10
    Views:
    758
    Marcus Kwok
    Dec 8, 2006
  4. Replies:
    7
    Views:
    591
    James Kanze
    May 2, 2007
  5. a
    Replies:
    7
    Views:
    364
    dasjotre
    Jun 28, 2007
Loading...

Share This Page