Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Lec 42.43 - virtual.functions

3,735 views

Published on

  • useful to me.. thanks
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Lec 42.43 - virtual.functions

  1. 1. Chapter: 11 Virtual Functions Lecture: 42 and 43 Date: 23.10.2012 and 31.10.2012
  2. 2. Advanced C++ Topics Virtual functions Friend functions Static function Overloaded “= ” operator Overloaded Copy Constructor “this” pointer
  3. 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. 4. 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;
  5. 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. 6. Non-Virtual Pointer Access
  7. 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. 8. Non-Virtual Pointer Access
  9. 9. 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.
  10. 10. 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.
  11. 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. 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. 13. Virtual Function and person Class C++ program example, Robert Lafore, 4th ed. p.511.
  14. 14. Virtual Base ClassesMultiple Inheritance
  15. 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. 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. 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.

×