virtual function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ashishnh33

    virtual function

    i want to know about the concept behind the virtual functions.

  • Jim Langston

    #2
    Re: virtual function

    "ashishnh33 " <kashish.ashish @gmail.comwrote in message
    news:1158294416 .957415.192670@ b28g2000cwb.goo glegroups.com.. .
    >i want to know about the concept behind the virtual functions.
    Concept. You should read a book for concepts really. We can tell you what
    they look like and such and maybe give a little sample code, but as far as
    teaching concepts it's better left to books (either online or dead tree).


    Comment

    • Hooyoo

      #3
      Re: virtual function


      Jim Langston wrote:
      "ashishnh33 " <kashish.ashish @gmail.comwrote in message
      news:1158294416 .957415.192670@ b28g2000cwb.goo glegroups.com.. .
      i want to know about the concept behind the virtual functions.
      >
      Concept. You should read a book for concepts really. We can tell you what
      they look like and such and maybe give a little sample code, but as far as
      teaching concepts it's better left to books (either online or dead tree).
      You can't use virtual function directly,and you can override it in the
      derived class

      Comment

      • ashishnh33

        #4
        Re: virtual function


        Jim Langston wrote:
        "ashishnh33 " <kashish.ashish @gmail.comwrote in message
        news:1158294416 .957415.192670@ b28g2000cwb.goo glegroups.com.. .
        i want to know about the concept behind the virtual functions.
        >
        Concept. You should read a book for concepts really. We can tell you what
        they look like and such and maybe give a little sample code, but as far as
        teaching concepts it's better left to books (either online or dead tree).
        thanks..

        Comment

        • Thomas Tutone

          #5
          Re: virtual function

          ashishnh33 wrote:
          i want to know about the concept behind the virtual functions.
          Naturally, you read the FAQ on this topic before posting, right? Just
          in case, why don't you review it again:



          Now, after reviewing that, what questions do you still have?

          Best regards,

          Tom

          Comment

          • Marcus Kwok

            #6
            Re: virtual function

            Hooyoo <zhao_huyong@12 6.comwrote:
            You can't use virtual function directly,and you can override it in the
            derived class
            You can use virtual functions directly. However, you cannot use a
            *pure* virtual function directly; a pure virtual must be overridden in a
            derived class.

            --
            Marcus Kwok
            Replace 'invalid' with 'net' to reply

            Comment

            • gw7rib@aol.com

              #7
              Re: virtual function


              ashishnh33 wrote:
              i want to know about the concept behind the virtual functions.
              I wrote a program recently to illustrate virtual functions. Here it is
              - it's a bit long but if you persevere to the end you should get the
              idea. I'm assuming that you already have some idea about inheritance,
              and also some idea about normal member functions. If you don't know
              either of these then perhaps you ought to read up about them first.

              Comments on the code would be helpful, as indeed would any comments
              from the experts (other than "Don't use printf") as to whether this is
              a good teaching example.

              Regards,
              Paul.

              /* An example of using virtual functions */

              #include <stdio.h>

              /* Define a class Dog. It has two functions - bark and growl.
              Bark is virtual, growl is not, to show the difference between
              the two types; in particular, you will probably want to use
              virtual functions. */

              class Dog {
              public:
              virtual void bark(void);
              void growl(void);
              };

              /* A real class would probably have some data members as well -
              omitted here for clarity. */

              /* Define the two functions */

              void Dog::bark(void) { printf("Woof!\n "); }

              void Dog::growl(void ) { printf("Growl.. .\n"); }

              /* Now derive a class Dalmation. We don't mention bark and growl, so
              they work the same as for Dogs. */

              class Dalmation : public Dog { };

              /* And now define a class Terrier, in which the functions are
              over-ridden. */

              class Terrier : public Dog {
              public:
              virtual void bark(void);
              void growl(void);
              };

              /* The word "virtual" is optional before "void bark", the function
              bark is already virtual as it was declared so in Dog */

              void Terrier::bark(v oid) { printf("Yip!\n" ); }

              void Terrier::growl( void) { printf("Gr...\n "); }

              int main(void) {

              /* Declare variables */

              Dog *rover;
              Dalmation *fido;
              Terrier *max;
              Dog *adog;

              /* adog is a pointer to Dog, so can point at any Dog, including
              Dalmations and Terriers as they are Dogs. */

              /* Create dogs */

              rover = new Dog;
              fido = new Dalmation;
              max = new Terrier;

              /* Make them bark. Bark is a clever virtual function and will
              make each dog bark the way it should do, depending on its
              actual type */

              rover -bark();
              /* This says "Woof!" as that is how Dogs bark */

              max -bark();
              /* This says "Yip!" as that is how Terriers bark */

              fido -bark();
              /* This says "Woof!" as that is how Dalmations bark - the
              same as Dogs because we didn't change it */

              adog = rover;
              adog -bark();
              /* Again, this says "Woof!" as that is how Dogs bark and
              rover points to a Dog */

              adog = max;
              adog -bark();
              /* Again, this says "Yip!" as that is how Terriers bark */

              adog = fido;
              adog -bark();
              /* And this says "Woof!" as that is how Dalmations bark */

              /* Make them growl. Growl is not a virtual function and so
              it has to work out which function to call from the type
              of the pointer */

              rover -growl();
              /* This says "Growl" as rover is of type pointer to Dog */

              max -growl();
              /* This says "Gr..." as max is of type pointer to Terrier */

              fido -growl();
              /* This says "Growl" as fido is of type pointer to Dalmation */

              adog = rover;
              adog -growl();
              /* This says "Growl" as adog is of type pointer to Dog */

              adog = max;
              adog -growl();
              /* This again says "Growl" as adog is of type pointer to Dog
              - this may not be what you expect */

              adog = fido;
              adog -growl();
              /* And this says "Growl" for the same reason as in the last comment
              */

              /* Delete dogs, as we're done */

              delete rover;
              delete fido;
              delete max;

              return 0;
              }

              Comment

              • Thomas Tutone

                #8
                Re: virtual function

                gw7rib@aol.com wrote:

                I wrote a program recently to illustrate virtual functions. Here it is
                - it's a bit long but if you persevere to the end you should get the
                idea. I'm assuming that you already have some idea about inheritance,
                and also some idea about normal member functions. If you don't know
                either of these then perhaps you ought to read up about them first.
                >
                Comments on the code would be helpful, as indeed would any comments
                from the experts (other than "Don't use printf") as to whether this is
                a good teaching example.
                /* An example of using virtual functions */
                >
                #include <stdio.h>
                >
                /* Define a class Dog. It has two functions - bark and growl.
                Bark is virtual, growl is not, to show the difference between
                the two types; in particular, you will probably want to use
                virtual functions. */
                >
                class Dog {
                public:
                virtual void bark(void);
                void growl(void);
                };
                A base class with virtual functions should almost always have a virtual
                destructor as well. Otherwise, deleting a Dog pointer may lead to
                undefined behavior, which is a bad thing.

                The use of "void" to indicate that the function takes no arguments is
                legal, but is also very much a C-ism. Google for "Stroustrup
                abomination" (without the quotes).

                /* A real class would probably have some data members as well -
                omitted here for clarity. */
                >
                /* Define the two functions */
                >
                void Dog::bark(void) { printf("Woof!\n "); }
                void Dog::growl(void ) { printf("Growl.. .\n"); }
                >
                /* Now derive a class Dalmation. We don't mention bark and growl, so
                they work the same as for Dogs. */
                >
                class Dalmation : public Dog { };
                >
                /* And now define a class Terrier, in which the functions are
                over-ridden. */
                >
                class Terrier : public Dog {
                public:
                virtual void bark(void);
                void growl(void);
                };
                Although I realize you do it to illustrate a point, hiding a base
                nonvirtual function by having a derived class use the same function
                signature is typically a design flaw and a bad idea.
                >
                /* The word "virtual" is optional before "void bark", the function
                bark is already virtual as it was declared so in Dog */
                >
                void Terrier::bark(v oid) { printf("Yip!\n" ); }
                >
                void Terrier::growl( void) { printf("Gr...\n "); }
                >
                int main(void) {
                >
                /* Declare variables */
                >
                Dog *rover;
                Dalmation *fido;
                Terrier *max;
                Dog *adog;
                Although the above is legal, in general in C++ you should, if possible,
                postpone declaring a variable until you can initialize it. What's
                wrong with:

                Dog* rover = new Dog;

                ?
                /* adog is a pointer to Dog, so can point at any Dog, including
                Dalmations and Terriers as they are Dogs. */
                >
                /* Create dogs */
                >
                rover = new Dog;
                fido = new Dalmation;
                max = new Terrier;
                In addition, many would counsel against the use of raw pointers except
                in very low-level code. Your code illustrates that point nicely - adog
                points to an object that other objects point to as well. Someone else
                might come along to edit your code, and insert a "delete adog" in there
                out of ignorance, leading to the possible double deletion of an object
                (not to mention the lack of a virtual destructor in Dog). Using an
                appropriate smart pointer would eliminate that problem.

                Best regards,

                Tom

                Comment

                Working...