pure virtual

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Guest's Avatar

    pure virtual

    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


  • Siana

    #2
    Re: pure virtual

    On Sat, 27 Sep 2003 20:13:31 +0300, "<- Chameleon ->"
    <cham_gss@hotma il.NOSPAM.com> wrote:
    [color=blue]
    >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
    >[/color]

    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.

    Comment

    • Thomas Wintschel

      #3
      Re: pure virtual

      "<- Chameleon ->" <cham_gss@hotma il.NOSPAM.com> wrote in message
      news:bl4gcb$ar2 $1@nic.grnet.gr ...[color=blue]
      > I don't understand. What's the difference in practice between 2 next[/color]
      lines?[color=blue]
      > When I *must* use first line and when second?
      > -------------
      > virtual int a(int b) = 0;
      > virtual int a(int b);
      > -------------
      >
      > thank you
      >
      >[/color]

      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
      {

      Comment

      • Kevin Goodsell

        #4
        Re: pure virtual

        Siana wrote:
        [color=blue]
        > On Sat, 27 Sep 2003 20:13:31 +0300, "<- Chameleon ->"
        > <cham_gss@hotma il.NOSPAM.com> wrote:
        >
        >[color=green]
        >>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
        >>[/color]
        >
        >
        > The first line declares a function a in some class that has no
        > definition[/color]

        That is not true in general. A pure virtual function certainly may have
        a definition.
        [color=blue]
        > - 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.
        >[/color]

        -Kevin
        --
        My email address is valid, but changes periodically.
        To contact me please use the address from a recent posting.

        Comment

        • Ron Natalie

          #5
          Re: pure virtual


          "Siana" <siana_canadaRE MOVETHISTOSEND@ yahoo.ca> wrote in message news:vhibnvc0aj u7uafqht064ha1q pf937kjcs@4ax.c om...
          [color=blue]
          > The first line declares a function a in some class that has no
          > definition[/color]

          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.
          [color=blue]
          > - it is there only to be overridden by derived classes for
          > the purposes of polymorphism.[/color]

          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).
          [color=blue]
          > 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 .[/color]

          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.


          Comment

          • jeffc

            #6
            Re: pure virtual


            "<- Chameleon ->" <cham_gss@hotma il.NOSPAM.com> wrote in message
            news:bl4gcb$ar2 $1@nic.grnet.gr ...[color=blue]
            > I don't understand. What's the difference in practice between 2 next[/color]
            lines?[color=blue]
            > When I *must* use first line and when second?
            > -------------
            > virtual int a(int b) = 0;
            > virtual int a(int b);
            > -------------[/color]

            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.


            Comment

            • jeffc

              #7
              Re: pure virtual


              "Thomas Wintschel" <thomwint@telus .net> wrote in message
              news:KYkdb.1203 7$o21.6208@edtn ps84...[color=blue]
              >
              > The first one would be used when defining a pure virtual base class.[/color]

              You mean an abstract class.


              Comment

              • Ron Natalie

                #8
                Re: pure virtual


                "jeffc" <nobody@nowhere .com> wrote in message news:3f784563_2 @news1.prserv.n et...[color=blue]
                >
                > "Thomas Wintschel" <thomwint@telus .net> wrote in message
                > news:KYkdb.1203 7$o21.6208@edtn ps84...[color=green]
                > >
                > > The first one would be used when defining a pure virtual base class.[/color]
                >
                > You mean an abstract class.
                >[/color]
                Now you're being pedantic :-)


                Comment

                • jeffc

                  #9
                  Re: pure virtual


                  "Ron Natalie" <ron@sensor.com > wrote in message
                  news:3f7849ee$0 $36943$9a6e19ea @news.newshosti ng.com...[color=blue]
                  >
                  > "jeffc" <nobody@nowhere .com> wrote in message[/color]
                  news:3f784563_2 @news1.prserv.n et...[color=blue][color=green]
                  > >
                  > > "Thomas Wintschel" <thomwint@telus .net> wrote in message
                  > > news:KYkdb.1203 7$o21.6208@edtn ps84...[color=darkred]
                  > > >
                  > > > The first one would be used when defining a pure virtual base class.[/color]
                  > >
                  > > You mean an abstract class.
                  > >[/color]
                  > Now you're being pedantic :-)[/color]

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


                  Comment

                  • Ron Natalie

                    #10
                    Re: pure virtual


                    "jeffc" <nobody@nowhere .com> wrote in message news:3f785221_2 @news1.prserv.n et...[color=blue]
                    >
                    > "Ron Natalie" <ron@sensor.com > wrote in message
                    > news:3f7849ee$0 $36943$9a6e19ea @news.newshosti ng.com...[color=green]
                    > >
                    > > "jeffc" <nobody@nowhere .com> wrote in message[/color]
                    > news:3f784563_2 @news1.prserv.n et...[color=green][color=darkred]
                    > > >
                    > > > "Thomas Wintschel" <thomwint@telus .net> wrote in message
                    > > > news:KYkdb.1203 7$o21.6208@edtn ps84...
                    > > > >
                    > > > > The first one would be used when defining a pure virtual base class.
                    > > >
                    > > > You mean an abstract class.
                    > > >[/color]
                    > > Now you're being pedantic :-)[/color]
                    >
                    > I knew somone was going to say that :-)[/color]

                    I was just ribbing ya.


                    Comment

                    Working...