C++ Function Overriding

Advertisements

C++ Function Overriding

Giving new implementation of base class method into derived class is called function overriding.

Signature of base class method and derived class must be same. Signature involves:

  • Number of arguments
  • Type of arguments
  • Sequence of arguments

Example of function overriding


       #include<iostream.h>
       #include<conio.h>

       class BaseClass
       {

              public:

              void Display()
              {
                    cout<<"\n\tThis is Display() method of BaseClass";
              }

              void Show()
              {
                    cout<<"\n\tThis is Show() method of BaseClass";
              }

       };

       class DerivedClass : public BaseClass
       {

              public:

              void Display()			//overriding method - new working of
              {                                         //base class's display method
                    cout<<"\n\tThis is Display() method of DerivedClass";
              }

       };

       void main()
       {

              DerivedClass Dr;

              Dr.Display();
              Dr.Show();

       }


   Output :

              This is Display() method of DerivedClass
              This is Show() method of BaseClass


				

Advertisement