Download presentation
Presentation is loading. Please wait.
Published byVirgil Grant Modified over 6 years ago
1
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Consider the following example: class B { public: void print(){ cout << “This is an object of type B”;} }; class D: public B { void print(){cout << “This is an object of type D”;} } B x; // instantiate object x D y; // instantiate object y B* ptr_B = &y; // make ptr_B point to y, is this possible? y.print(); // will this print “This is an object of type D” ? ptr_B->print(); // will this print “This is an object of type B” ? Can be used to develop a hierarchy of classes based on abstractions in a top down fashion, e.g. the Location-Point-Circle-Arch hierarchy introduced before It is also needed for code refinement, expandability, and reuse by a) Refining the implementation of a class by building a refined derived class, were some members functions are redefined, b) Expanding the functionality of the a class by adding new functions c) Reusing the code in a base class in several derived classes
2
Inheritance Virtual Functions , Dynamic Binding, and Polymorphism (cont.) A virtual function is a special member function invoked through a public base class reference or pointer, it is bound dynamically at run-time, class B { public: virtual void print(){ cout << “This is an object of type B”;}}; class D: public B {// is exactly the same as in the previous slide }; B x; // instantiate object x D y; // instantiate object y B* ptr_B = &y; // make ptr_B point to y y.print(); // will print “This is an object of type D” ptr_B->print(); // will also print “This is an object of type D” // The invocation of print() is bound to the object type not the pointer type
3
Inheritance Virtual Functions , Dynamic Binding, and Polymorphism (cont.) Dynamic binding is introduced using virtual functions The keyword virtual is a function specifier for member functions of a base class When a function at the base class class is declared virtual, all its redefined versions in the derived classes are bound dynamically according to the object type for which the function is invoked, not the pointer or reference type Polymorphism is a language mechanism that permits the same code expression to invoke different functions depending upon the type of object using the code, see the example in the next slide
4
Inheritance Polymorphism (cont.) Example:
Consider the following example, suppose we have several classes derived from class B above class D1 : public B { public: void print();}; class D2 : public B { public: void print();}; …. Class Dn-1 : public B { public: void print();}; Also lets assume the existence of an array of pointers to objects as follows, B* objects_ptr[100]; // an array of pointers to objects of type B or // objects of type derived from B object_ptr[0] = new D, object_ptr[1] = new D1; object_ptr[2] = new D2; for(int j = 0 ; j < n ; j++) object_ptr[j]->print(); // different print() functions will be invoked // although we are iterating over the same code expression.
5
Inheritance Abstract Classes and Pure Virtual Functions
An abstract class is a class for which no objects are created, it is only used as a base class in order to use virtual functions and dynamic binding for the objects of its derived classes, e.g. class Shape {// no objects of type Shape will be instantiated…}; class rectangle: public Shape {// this is a concrete class…..}; class Triangle: public Shape {// this is another concrete class….}; Shape* ptr = new rectangle; ptr = new Triangle; An abstract class must have at least one pure virtual function declared as follows, class Shape { public: virtual void rotate() = 0; // the equal 0 means the function // is a pure virtual function and it has no body code } The function rotate() only specifies a typical operation for the types of objects derived from Shape
6
Inheritance Abstract Classes and Pure Virtual Functions (cont.)
An abstract class can not be used as an argument type or as a function return type, Shape a; // error, you can not instantiate objects of type Shape void fun(Shape x); // error void fun(Shape& x);// OK Shape fun1(void); // error Shape& fun1(void); // OK A reference or a pointer to Shape is of course allowed since they can be used to refer to an object of a class derived from Shape A derived class from an abstract base class also becomes abstract if the class does not redefine all the pure virtual functions defined at its base classes class X{ virtual void g() = 0; virtual void f() = 0; }; class Y : public X { void g(){// body code of g}// g is redefined // if f() is not redefined with code, class Y becomes also an abstract class
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.