Ad

Wednesday, August 08, 2007

Properties of Virtual Functions

From the previous two articles Introduction to Virtual Functions and Virtual Functions and Run-time Polymorphism, we have been discussing about Virtual Functions.

In this article we’ll be discussing about two important properties of Virtual Functions.

As properties can be better understood by examples, we’ll be using them more rather than text and definitions that could confuse you.

Property #1:


  // Properties of virtual functions
  #include <iostream.h>

  // base class
  class base
  {
  public:
    virtual void func()
    {
      cout<<"Base's func()\n";
    }
  };

  // derived class
  class derived1:public base
  {
  public:
    // this is a virtual function
    void func()
    {
      cout<<"Derived1's func()\n";
    }
  };

  // derived from another
  // derived class
  class derived2:public derived1
  {
  public:
    // still virtual
    void func()
    {
      cout<<"Derived2's func()\n";
    }
  };

  // main
  void main()
  {
    base b;
    derived1 d1;
    derived2 d2;

    b.func();
    d1.func();
    d2.func();
  }

OUTPUT:
Base's func()
Derived1's func()
Derived2's func()

The code above illustrates that when a class is derived from another derived class (which has inherited a virtual function from its base class) then also the virtual function can be overridden. So, it means that once declared virtual, a function (no matter how many times inherited in hierarchy) still remains Virtual and hence can be overridden.

Property #2: This is an extension of the previous property, as you know that a virtual function remains virtual no matter how many times it is inherited in a hierarchy, but what if one of the derived class doesn’t overrides it, what will happen then? The following code answers this!


  // Properties of virtual functions
  #include <iostream.h>

  // base class
  class base
  {
  public:
    virtual void func()
    {
      cout<<"Base's func()\n";
    }
  };

  // derived class
  class derived1:public base
  {
  public:
    // this is a virtual function
    void func()
    {
      cout<<"Derived1's func()\n";
    }
  };

  // derived from another
  // derived class
  class derived2:public derived1
  {
  public:
    // no overriding
  };

  // another derived class
  class derived3:public derived2
  {
  public:
    // can still be overridden
    void func()
    {
      cout<<"Derived3's func()\n";
    }
  };

  // main
  void main()
  {
    base b;
    derived1 d1;
    derived2 d2;
    derived3 d3;

    b.func();
    d1.func();

    // will call the function overridden
    // by its base class because it didn't
    // override the function
    d2.func();

    d3.func();
  }

OUTPUT:

  Base's func()
  Derived1's func()
  Derived1's func()
  Derived3's func() 

The above code is pretty much self-explanatory so I don’t think it needs further explanations.

Related Articles:

No comments:

Post a Comment

You are free to comment anything, although you can comment as 'Anonymous' it is strongly recommended that you supply your name. Thank You.

Please don't use abusive language.