inheritance - 3. virtual functions functions defined as virtual are ones that the base expects its...
DESCRIPTION
Virtual functions … Invoking virtual functions through a base class pointer (or reference): Invoking virtual functions through a base class pointer (or reference): ALWAYS invokes the function defined by the actual type of the object to which the pointer (or reference) points to. ALWAYS invokes the function defined by the actual type of the object to which the pointer (or reference) points to. The type of the pointer itself is not so significant. The type of the pointer itself is not so significant. If the base class pointer is pointing to a base object it will invoke base class method If the base class pointer is pointing to a base object it will invoke base class method If the base class pointer is pointing to a derived class object (and the derived class redefines the virtual function), it will invoke the derived class method. If the base class pointer is pointing to a derived class object (and the derived class redefines the virtual function), it will invoke the derived class method. See shapes2.cpp See shapes2.cppTRANSCRIPT
Inheritance - 3Inheritance - 3
Virtual FunctionsVirtual Functions Functions defined as virtual are ones that the Functions defined as virtual are ones that the
base expects its derived classes may redefine.base expects its derived classes may redefine. But it is not mandatory for a derived class to But it is not mandatory for a derived class to
redefine a virtual function.redefine a virtual function. Functions NOT defined as virtual are ones that Functions NOT defined as virtual are ones that
the base expects its derived classes may NOT the base expects its derived classes may NOT redefine.redefine.
But a derived class could redefine a non But a derived class could redefine a non virtual function.virtual function.
Virtual functions …Virtual functions … Invoking virtual functions through a base class Invoking virtual functions through a base class
pointer (or reference):pointer (or reference): ALWAYS invokes the function defined by the actual type ALWAYS invokes the function defined by the actual type
of the object to which the pointer (or reference) points to.of the object to which the pointer (or reference) points to. The type of the pointer itself is not so significant.The type of the pointer itself is not so significant. If the base class pointer is pointing to a base object it will If the base class pointer is pointing to a base object it will
invoke base class methodinvoke base class method If the base class pointer is pointing to a derived class object If the base class pointer is pointing to a derived class object
(and the derived class redefines the virtual function), it will (and the derived class redefines the virtual function), it will invoke the derived class method.invoke the derived class method.
See shapes2.cppSee shapes2.cpp
Inheritance HierarchyInheritance Hierarchy Classes related by inheritance are said to form Classes related by inheritance are said to form
an inheritance hierarchy.an inheritance hierarchy. There are no limits (at least theoretically) on There are no limits (at least theoretically) on
number of levels in the hierarchy.number of levels in the hierarchy. Redefinition of virtual and non virtual Redefinition of virtual and non virtual
functions can occur at any level in the functions can occur at any level in the hierarchy.hierarchy.
Inheritance Hierarchy …Inheritance Hierarchy … class Base { /* ... */ };class Base { /* ... */ }; class D1: public Base { /* ... */ };class D1: public Base { /* ... */ }; class D2: public D1 { /* ... */ }; class D2: public D1 { /* ... */ }; The most derived type inherits the members of The most derived type inherits the members of
its base, which in turn inherits the members of its base, which in turn inherits the members of its base and so on up the inheritance chain.its base and so on up the inheritance chain.
Effectively, the most derived object contains a Effectively, the most derived object contains a subobject for each of its immediate-base and subobject for each of its immediate-base and indirect-base classes.indirect-base classes.
Virtual functions …Virtual functions … For a virtual function invocation via a base class For a virtual function invocation via a base class
pointer or reference, the compiler DOES NOT do pointer or reference, the compiler DOES NOT do static binding.static binding.
It instead outputs code that at run-time:It instead outputs code that at run-time: Checks the type of the object that the pointer is pointing toChecks the type of the object that the pointer is pointing to Calls the virtual override (redefinition) that is defined by Calls the virtual override (redefinition) that is defined by
the object.the object. Note that the final object could be many class levels away Note that the final object could be many class levels away
from the base (say 10) and all or some of the classes could from the base (say 10) and all or some of the classes could be redefining the virtual function.be redefining the virtual function.
Dynamic BindingDynamic Binding Since the binding of the virtual function is Since the binding of the virtual function is
done at run-time for virtual functions, this type done at run-time for virtual functions, this type of binding is known as Dynamic Bindingof binding is known as Dynamic Binding
As we already studied, for non virtual As we already studied, for non virtual functions the binding is done at compile time functions the binding is done at compile time itself and is called static bindingitself and is called static binding
PolymorphismPolymorphism double printShapeAreas(shape *sp[]) {double printShapeAreas(shape *sp[]) { …….. cout << ", Area: " << sp[ix]->getarea() << endl;cout << ", Area: " << sp[ix]->getarea() << endl; Which getarea() does the statement call???Which getarea() does the statement call???
shape?, rect?, circle?shape?, rect?, circle? Known only at runtimeKnown only at runtime In the loop, the same statement will at times invoke getarea() In the loop, the same statement will at times invoke getarea()
of rect and at times invoke getarea() of shape.of rect and at times invoke getarea() of shape. Same statement, different results!!!Same statement, different results!!! Or same statement, different forms -> known as Or same statement, different forms -> known as
PolymorphismPolymorphism
Dynamic Binding/PolymorphismDynamic Binding/Polymorphism By default, in C++, function calls DO NOT use By default, in C++, function calls DO NOT use
dynamic bindingdynamic binding To trigger dynamic binding two conditions must be To trigger dynamic binding two conditions must be
met:met: First, only member functions that are specified as First, only member functions that are specified as
virtual can be dynamically bound (by default, virtual can be dynamically bound (by default, member functions are not virtual; nonvirtual member functions are not virtual; nonvirtual functions are not dynamically bound)functions are not dynamically bound)
Second, the call must be made through a reference or Second, the call must be made through a reference or a pointer to a base-class type.a pointer to a base-class type.
Dynamic Binding/Polymorphism …Dynamic Binding/Polymorphism …
The crucial point about references and pointers The crucial point about references and pointers to base-class types is that:to base-class types is that: The The static type: static type: the type of the reference or the type of the reference or
pointer, which is knowable at compile timepointer, which is knowable at compile time and the and the dynamic type: dynamic type: the type of the object to the type of the object to
which the pointer or reference is bound, which is which the pointer or reference is bound, which is knowable only at run timeknowable only at run time
may differ.may differ.
Dynamic Binding/Polymorphism …Dynamic Binding/Polymorphism … Binding a base-type reference or pointer to a derived object Binding a base-type reference or pointer to a derived object
has no effect on the underlying object.has no effect on the underlying object. The object itself is unchanged and remains a derived object.The object itself is unchanged and remains a derived object. The fact that the actual type of the object might differ from the The fact that the actual type of the object might differ from the
static type of the reference or pointer addressing that object is static type of the reference or pointer addressing that object is the key to dynamic binding in C++.the key to dynamic binding in C++.
When a virtual function is called through a reference or When a virtual function is called through a reference or pointer, the compiler generates code to decide at run time pointer, the compiler generates code to decide at run time which function to call.which function to call.
The function that is called is the one that corresponds to the The function that is called is the one that corresponds to the dynamic type. dynamic type.
See code of topic ‘Calls to virtual Functions May be resolved See code of topic ‘Calls to virtual Functions May be resolved at run time’ in Section 15.2at run time’ in Section 15.2
Pure Virtual functionsPure Virtual functions Shape class objects do not make sense as shape is an Shape class objects do not make sense as shape is an
abstract concept and not a real geometric figureabstract concept and not a real geometric figure Only objects derived from shape like rect and circle Only objects derived from shape like rect and circle
are meaningful. They are concrete geometric figures are meaningful. They are concrete geometric figures (not abstract).(not abstract).
Ideally we should prevent users from creating a shape Ideally we should prevent users from creating a shape object.object.
That can be achieved using pure virtual functions.That can be achieved using pure virtual functions. See shapes3.cppSee shapes3.cpp
Pure Virtual functions …Pure Virtual functions … virtual double getarea() const = 0; // Pure virtual double getarea() const = 0; // Pure
virtualvirtual Makes getarea() a pure virtual function.Makes getarea() a pure virtual function. And makes shape an abstract class.And makes shape an abstract class. Objects cannot be created from an abstract Objects cannot be created from an abstract
class.class. shape s;shape s; // Compiler will flag as error// Compiler will flag as error
Assignment 8BAssignment 8B The same as given earlier (employee, The same as given earlier (employee,
salesman) but now using CalculateSalary() as salesman) but now using CalculateSalary() as a virtual function.a virtual function.
More realistic shapes exampleMore realistic shapes example Shapes3.cpp has limitation of fixed number of Shapes3.cpp has limitation of fixed number of
shapes.shapes. Consider program like Microsoft Paint.Consider program like Microsoft Paint. It may use a class hierarchy to keep track of various It may use a class hierarchy to keep track of various
shapes drawn on bitmap.shapes drawn on bitmap. The various shapes will have to be kept in some The various shapes will have to be kept in some
collection.collection. But cannot use fixed size array.But cannot use fixed size array. So, what’s the solution??So, what’s the solution?? See vecshapes1.cppSee vecshapes1.cpp
Book Source Code Copyright NoteBook Source Code Copyright Note
The course book is C++ Primer, 4th Edition by The course book is C++ Primer, 4th Edition by Lippman, Lajoie and Moo. Any references in earlier Lippman, Lajoie and Moo. Any references in earlier files to source files, and use of code within those files, files to source files, and use of code within those files, are of example code given in and/or along with the are of example code given in and/or along with the book. As these slides are freely accessible on the book. As these slides are freely accessible on the Internet, not-for-profit, and for educational purposes, Internet, not-for-profit, and for educational purposes, based on the permission related statements in the based on the permission related statements in the source code, I have considered that permission has source code, I have considered that permission has been granted to use them in these slides.been granted to use them in these slides.