Virtual Function override

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Suresh369
    New Member
    • Sep 2006
    • 10

    Virtual Function override

    I am still a beginner in C++:

    Lets say i need to derive a class D from a base class B. And I have a need to override B's function called vulnerable()

    There are two ways I can do it:

    1. Normal override - Just re-define the function in class D
    2. Virtual Function Override - Delcare it as virtual in B and then override in B

    What is the advantage of one over the other?
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Originally posted by Suresh369
    I am still a beginner in C++:

    Lets say i need to derive a class D from a base class B. And I have a need to override B's function called vulnerable()

    There are two ways I can do it:

    1. Normal override - Just re-define the function in class D
    2. Virtual Function Override - Delcare it as virtual in B and then override in D

    What is the advantage of one over the other?
    The difference comes when you call the function via the base class. In 1 if you call the function via the base class the function in the base class is called, in 2 if you call the function via the base class the function in the derived class is called.

    And that is basically it.

    Comment

    • Suresh369
      New Member
      • Sep 2006
      • 10

      #3
      Originally posted by Banfa
      The difference comes when you call the function via the base class. In 1 if you call the function via the base class the function in the base class is called, in 2 if you call the function via the base class the function in the derived class is called.

      And that is basically it.
      But what if I have multiple derived classes which override the function, how will 2 work?

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        If you have C derived from B derived from A

        And if the is a function in all classes fn that is not virtual and a function in all classes vfn that is virtual (in all classes)

        If you have a single instance of class C ic then

        If you call fn in the context of B b::fn will be called, if you call fn in the context of A A::fn will be called.

        If you call vfn in the context of B C::vfn will be called if you call vfn in the context of A C::vfn will be called

        If you have a single instance of class B ib then

        If you call fn in the context of B b::fn will be called, if you call fn in the context of A A::fn will be called. (you can't call fn in the context of C)

        If you call vfn in the context of B B::vfn will be called if you call vfn in the context of A B::vfn will be called


        An instance of a class includes a virtual function table which points to any virtual function overrides for the class so that no matter what level of base class calls the virtual function the function in the actual class instance is called.

        Comment

        • Suresh369
          New Member
          • Sep 2006
          • 10

          #5
          Thanks! But sorry, the more I read about virtual functions, the more confused I am

          Lets take this example:
          class Window // Base class for C++ virtual function example
          {
          public:
          virtual void Create() // virtual function for C++ virtual function example
          {
          cout <<"Base class Window"<<endl;
          }
          };

          class CommandButton : public Window
          {
          public:
          void Create()
          {
          cout<<"Derived class Command Button - Overridden C++ virtual function"<<endl ;
          }
          };

          void main()
          {
          Window *x, *y;

          x = new Window();
          x->Create();

          y = new CommandButton() ;
          y->Create();
          }


          In function main, for x->Create(), we are calling Create() in the context of x and since Create is a virtual Function re-defined in CommandButton class, is the output "Derived class Command Button - Overridden C++ virtual function" ?
          Can you please explain?

          Comment

          • dynamicleo
            New Member
            • Oct 2006
            • 14

            #6
            base pointer can point to derive instant, but derive pointer can not point to base instant
            eg: base * bptr = new derive( ); // valid
            derive * dptr = new base( ); // invalid

            you can write these ways:

            base * bptr = new bptr( );
            base * bptr = new dptr( );
            derive * dptr = new dptr( );

            you have these inheritance as following:

            class base {
            public:
            base ( ) { }
            void iFunction( ){
            .....
            }
            }

            class derive : base {
            public:
            derive ( ) { }
            void iFunction( ){
            ......
            }
            }

            void main( ) {
            base * bptr = new base( );
            bptr->iFunction( ); // base::iFunction is called

            ///////////////////////////////////////////////////////
            bptr = new derive( );
            bptr->iFunction( ); // base::iFunction is called
            ///////////////////////////////////////////////////////

            derive * dptr = new derive( );
            dptr->iFunction( ); // derive::iFuncti on is called
            }

            but if you define virtual void iFunction( ) in your base class

            /////////////////////////////////////////////////////
            bptr = new derive( );
            bptr->iFunction( ); // base::iFunction is called
            ////////////////////////////////////////////////////

            so, virtual is used for above case.

            bptr = new base( );
            dptr = new derive( );

            these above case have not need virtual.

            Comment

            Working...