Chapter: 11


    Virtual Functions
             Lecture: 42 and 43
      Date: 23.10.2012 and 31.10.2012
Advanced C++ Topics

   Virtual functions
   Friend functions
   Static function
   Overloaded “= ” operator
   Overloaded Copy Constructor
   “this” pointer
Motivation: Virtual Functions

    a hierarchy of                                Line   draw()

    geometric shape
    classes                        Rectangle                 Circle
                                                                  draw()
    −   draws circles, ellipses,         draw()

        rectangles etc              Square                  Ellipse
                                         draw()                   draw()

    just use method
    draw throughout the
    hierarchy
Pointers to Derived Classes
   C++ allows base class pointers to point to
    derived class objects.
   Let we have
      class base { … };

      class derived : public base { … };

   Then we can write:
       base *p1;
       derived d_obj;
       p1 = &d_obj;
   class Base {                     void main()
   public:                          {
      void show() {
        cout << “basen”;           Derv1 dv1;
      }                             Derv2 dv2;
   };
   class Derv1 : public base {      Base *ptr
   public:                          ptr = &dv1;
      void show() {                 ptr->show();
        cout << “derived1n”;
      }                             ptr = &dv2;
   class Derv2 : public base {      ptr ->show();
   public:
      void show() {                 return 0;
        cout << “derived2n”;       }
      }
   };
Non-Virtual Pointer Access
   class Base {                     void main()
   public:                          {
    virtual void show() {
        cout << “basen”;           Derv1 dv1;
      }                             Derv2 dv2;
   };
   class Derv1 : public base {      Base *ptr
   public:                          ptr = &dv1;
      void show() {                 ptr->show();
        cout << “derived1n”;
      }                             ptr = &dv2;
   class Derv2 : public base {      ptr ->show();
   public:
      void show() {                 return 0;
        cout << “derived2n”;       }
      }
   };
Non-Virtual Pointer Access
Introduction to Virtual Functions
   A virtual function is a member function that is declared
    within a base class and redefined (called overriding) by a
    derived class.

   It implements the “one interface, multiple methods”
    philosophy that underlies polymorphism.

   The keyword virtual is used to designate a member
    function as virtual.

   Supports run-time polymorphism with the help of base
    class pointers.
Introduction to Virtual Functions
                  (contd.)
   While redefining a virtual function in a derived
    class, the function signature must match the original
    function present in the base class.
   So, we call it overriding, not overloading.
   When a virtual function is redefined by a derived
    class, the keyword virtual is not needed (but can be
    specified if the programmer wants).
   The “virtual”-ity of the member function continues
    along the inheritance chain.
   A class that contains a virtual function is referred to
    as a polymorphic class.
Abstract Classes and
             Pure Virtual Functions
   A base class whose objects are never instantiated is
    called an abstract class. Such a class exists only to
    act as a parent of derived classes that will be used to
    instantiate objects.
   A base class made as an abstract class must contain
    at least one pure virtual function.
   A pure virtual function is one with the expression
    “=0” added to the function declaration.
    e.g.,
         virtual void show() = 0;
   class Base {                     void main()
   public:                          {
    virtual void show() = 0;
   };                               Base bad; //ERROR

   class Derv1 : public base {      Derv1 dv1;
   public:                          Derv2 dv2;
     void show() {
       cout << “derived1n”;        Base *ptr
     }                              ptr = &dv1;
                                     ptr->show();
   class Derv2 : public base {
   public:                          ptr = &dv2;
      void show() {                 ptr ->show();
        cout << “derived2n”;
      }                             return 0;
   };                               }
Virtual Function and
                  person Class


   C++ program example, Robert Lafore, 4th ed. p.511.
Virtual Base Classes
Multiple Inheritance
Virtual Base Classes
         class Parent {
         protected:
              int basedata;
         };

         class Child1 : public parent {
           {    };

         class Child2 : public base {
           {   };

         class GrandChild:
          public Child1, public Child2
         { public:
           int getdata() { return basedata; } //ERROR
         };
Virtual Base Classes
   class Parent {                                 class Parent {
   protected:                                     protected:
         int basedata;                                  int basedata;
   };
                                                   };

                                                   class Child1 : virtual public parent {
   class Child1 : public parent {                   {      };
     {     };
                                                   class Child2 : virtual public base {
   class Child2 : public base {                     {     };
     {     };
                                                   class GrandChild:
   class GrandChild:                               public Child1, public Child2
                                                   {
    public Child1, public Child2                   public:
   {
                                                       int getdata()
   public:
         int getdata()                                      { return basedata; } //OK
               { return basedata; }   //ERROR         };
   };
Friend Functions
   Encapsulation and data-hiding dictate that
    nonmember functions should not be able to access
    an object’s private or protected data.

   Imagine you need a function to operate on objects
    of two different classes. Perhaps the function will
    take objects of the two classes as arguments, and
    operate on their private data.
     In such satiation friend functions are used…
     Precisely, a friend function is one declared outside of
      classes and still operates on their private data.

Lec 42.43 - virtual.functions

  • 1.
    Chapter: 11 Virtual Functions Lecture: 42 and 43 Date: 23.10.2012 and 31.10.2012
  • 2.
    Advanced C++ Topics  Virtual functions  Friend functions  Static function  Overloaded “= ” operator  Overloaded Copy Constructor  “this” pointer
  • 3.
    Motivation: Virtual Functions  a hierarchy of Line draw() geometric shape classes Rectangle Circle draw() − draws circles, ellipses, draw() rectangles etc Square Ellipse draw() draw()  just use method draw throughout the hierarchy
  • 4.
    Pointers to DerivedClasses  C++ allows base class pointers to point to derived class objects.  Let we have  class base { … };  class derived : public base { … };  Then we can write: base *p1; derived d_obj; p1 = &d_obj;
  • 5.
    class Base {  void main()  public:  {  void show() {  cout << “basen”;  Derv1 dv1;  }  Derv2 dv2;  };  class Derv1 : public base {  Base *ptr  public:  ptr = &dv1;  void show() {  ptr->show();  cout << “derived1n”;  }  ptr = &dv2;  class Derv2 : public base {  ptr ->show();  public:  void show() {  return 0;  cout << “derived2n”;  }  }  };
  • 6.
  • 7.
    class Base {  void main()  public:  {  virtual void show() {  cout << “basen”;  Derv1 dv1;  }  Derv2 dv2;  };  class Derv1 : public base {  Base *ptr  public:  ptr = &dv1;  void show() {  ptr->show();  cout << “derived1n”;  }  ptr = &dv2;  class Derv2 : public base {  ptr ->show();  public:  void show() {  return 0;  cout << “derived2n”;  }  }  };
  • 8.
  • 9.
    Introduction to VirtualFunctions  A virtual function is a member function that is declared within a base class and redefined (called overriding) by a derived class.  It implements the “one interface, multiple methods” philosophy that underlies polymorphism.  The keyword virtual is used to designate a member function as virtual.  Supports run-time polymorphism with the help of base class pointers.
  • 10.
    Introduction to VirtualFunctions (contd.)  While redefining a virtual function in a derived class, the function signature must match the original function present in the base class.  So, we call it overriding, not overloading.  When a virtual function is redefined by a derived class, the keyword virtual is not needed (but can be specified if the programmer wants).  The “virtual”-ity of the member function continues along the inheritance chain.  A class that contains a virtual function is referred to as a polymorphic class.
  • 11.
    Abstract Classes and Pure Virtual Functions  A base class whose objects are never instantiated is called an abstract class. Such a class exists only to act as a parent of derived classes that will be used to instantiate objects.  A base class made as an abstract class must contain at least one pure virtual function.  A pure virtual function is one with the expression “=0” added to the function declaration. e.g., virtual void show() = 0;
  • 12.
    class Base {  void main()  public:  {  virtual void show() = 0;  };  Base bad; //ERROR  class Derv1 : public base {  Derv1 dv1;  public:  Derv2 dv2;  void show() {  cout << “derived1n”;  Base *ptr  }  ptr = &dv1;  ptr->show();  class Derv2 : public base {  public:  ptr = &dv2;  void show() {  ptr ->show();  cout << “derived2n”;  }  return 0;  };  }
  • 13.
    Virtual Function and person Class  C++ program example, Robert Lafore, 4th ed. p.511.
  • 14.
  • 15.
    Virtual Base Classes  class Parent {  protected:  int basedata;  };  class Child1 : public parent { { };  class Child2 : public base { { };  class GrandChild: public Child1, public Child2  { public: int getdata() { return basedata; } //ERROR  };
  • 16.
    Virtual Base Classes  class Parent {  class Parent {  protected:  protected:  int basedata;  int basedata;  };  };  class Child1 : virtual public parent {  class Child1 : public parent { { }; { };  class Child2 : virtual public base {  class Child2 : public base { { }; { };  class GrandChild:  class GrandChild: public Child1, public Child2  { public Child1, public Child2  public:  { int getdata()  public: int getdata() { return basedata; } //OK { return basedata; } //ERROR };  };
  • 17.
    Friend Functions  Encapsulation and data-hiding dictate that nonmember functions should not be able to access an object’s private or protected data.  Imagine you need a function to operate on objects of two different classes. Perhaps the function will take objects of the two classes as arguments, and operate on their private data.  In such satiation friend functions are used…  Precisely, a friend function is one declared outside of classes and still operates on their private data.

Editor's Notes