Presentation is loading. Please wait.

Presentation is loading. Please wait.

Inheritance - 3. Virtual Functions Functions defined as virtual are ones that the base expects its derived classes may redefine. Functions defined as.

Similar presentations


Presentation on theme: "Inheritance - 3. Virtual Functions Functions defined as virtual are ones that the base expects its derived classes may redefine. Functions defined as."— Presentation transcript:

1 Inheritance - 3

2 Virtual Functions Functions defined as virtual are ones that the base expects its derived classes may redefine. Functions defined as virtual are ones that the base expects its derived classes may redefine. But it is not mandatory for a derived class to redefine a virtual function. But it is not mandatory for a derived class to redefine a virtual function. Functions NOT defined as virtual are ones that the base expects its derived classes may NOT redefine. Functions NOT defined as virtual are ones that the base expects its derived classes may NOT redefine. But a derived class could redefine a non virtual function. But a derived class could redefine a non virtual function.

3 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.cpp

4 Inheritance Hierarchy Classes related by inheritance are said to form an inheritance hierarchy. Classes related by inheritance are said to form an inheritance hierarchy. There are no limits (at least theoretically) on number of levels in the hierarchy. There are no limits (at least theoretically) on number of levels in the hierarchy. Redefinition of virtual and non virtual functions can occur at any level in the hierarchy. Redefinition of virtual and non virtual functions can occur at any level in the hierarchy.

5 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 its base, which in turn inherits the members of its base and so on up the inheritance chain. The most derived type inherits the members of its base, which in turn inherits the members of its base and so on up the inheritance chain. Effectively, the most derived object contains a subobject for each of its immediate-base and indirect-base classes. Effectively, the most derived object contains a subobject for each of its immediate-base and indirect-base classes.

6 Virtual functions … For a virtual function invocation via a base class pointer or reference, the compiler DOES NOT do static binding. For a virtual function invocation via a base class pointer or reference, the compiler DOES NOT do 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 to Checks the type of the object that the pointer is pointing to Calls the virtual override (redefinition) that is defined by the object. Calls the virtual override (redefinition) that is defined by the object. Note that the final object could be many class levels away from the base (say 10) and all or some of the classes could be redefining the virtual function. Note that the final object could be many class levels away from the base (say 10) and all or some of the classes could be redefining the virtual function.

7 Dynamic Binding Since the binding of the virtual function is done at run-time for virtual functions, this type of binding is known as Dynamic Binding Since the binding of the virtual function is done at run-time for virtual functions, this type of binding is known as Dynamic Binding As we already studied, for non virtual functions the binding is done at compile time itself and is called static binding As we already studied, for non virtual functions the binding is done at compile time itself and is called static binding

8 Polymorphism double printShapeAreas(shape *sp[]) { double printShapeAreas(shape *sp[]) { …. …. cout getarea() getarea() << endl; Which getarea() does the statement call??? Which getarea() does the statement call??? shape?, rect?, circle? shape?, rect?, circle? Known only at runtime Known only at runtime In the loop, the same statement will at times invoke getarea() of rect and at times invoke getarea() of shape. In the loop, the same statement will at times invoke getarea() of rect and at times invoke getarea() of shape. Same statement, different results!!! Same statement, different results!!! Or same statement, different forms -> known as Polymorphism Or same statement, different forms -> known as Polymorphism

9 Dynamic Binding/Polymorphism By default, in C++, function calls DO NOT use dynamic binding By default, in C++, function calls DO NOT use dynamic binding To trigger dynamic binding two conditions must be met: To trigger dynamic binding two conditions must be met: First, only member functions that are specified as virtual can be dynamically bound (by default, member functions are not virtual; nonvirtual functions are not dynamically bound) First, only member functions that are specified as virtual can be dynamically bound (by default, member functions are not virtual; nonvirtual functions are not dynamically bound) Second, the call must be made through a reference or a pointer to a base-class type. Second, the call must be made through a reference or a pointer to a base-class type.

10 Dynamic Binding/Polymorphism … The crucial point about references and pointers to base-class types is that: The crucial point about references and pointers to base-class types is that: The static type: the type of the reference or pointer, which is knowable at compile time The static type: the type of the reference or pointer, which is knowable at compile timestatic typestatic type and the dynamic type: the type of the object to which the pointer or reference is bound, which is knowable only at run time and the dynamic type: the type of the object to which the pointer or reference is bound, which is knowable only at run timedynamic typedynamic type may differ. may differ.

11 Dynamic Binding/Polymorphism … Binding a base-type reference or pointer to a derived object has no effect on the underlying object. Binding a base-type reference or pointer to a derived 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 static type of the reference or pointer addressing that object is the key to dynamic binding in C++. The fact that the actual type of the object might differ from the static type of the reference or pointer addressing that object is the key to dynamic binding in C++. When a virtual function is called through a reference or pointer, the compiler generates code to decide at run time which function to call. When a virtual function is called through a reference or pointer, the compiler generates code to decide at run time which function to call. The function that is called is the one that corresponds to the dynamic type. The function that is called is the one that corresponds to the dynamic type. See code of topic ‘Calls to virtual Functions May be resolved at run time’ in Section 15.2 See code of topic ‘Calls to virtual Functions May be resolved at run time’ in Section 15.2

12 Pure Virtual functions Shape class objects do not make sense as shape is an abstract concept and not a real geometric figure Shape class objects do not make sense as shape is an abstract concept and not a real geometric figure Only objects derived from shape like rect and circle are meaningful. They are concrete geometric figures (not abstract). Only objects derived from shape like rect and circle are meaningful. They are concrete geometric figures (not abstract). Ideally we should prevent users from creating a shape object. Ideally we should prevent users from creating a shape object. That can be achieved using pure virtual functions. That can be achieved using pure virtual functions. See shapes3.cpp See shapes3.cpp

13 Pure Virtual functions … virtual double getarea() const = 0; // Pure virtual virtual double getarea() const = 0; // Pure virtual 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 class. Objects cannot be created from an abstract class. shape s;// Compiler will flag as error shape s;// Compiler will flag as error

14 Assignment 8B The same as given earlier (employee, salesman) but now using CalculateSalary() as a virtual function. The same as given earlier (employee, salesman) but now using CalculateSalary() as a virtual function.

15 More realistic shapes example Shapes3.cpp has limitation of fixed number of shapes. Shapes3.cpp has limitation of fixed number of shapes. Consider program like Microsoft Paint. Consider program like Microsoft Paint. It may use a class hierarchy to keep track of various shapes drawn on bitmap. It may use a class hierarchy to keep track of various shapes drawn on bitmap. The various shapes will have to be kept in some collection. The various shapes will have to be kept in some 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.cpp See vecshapes1.cpp

16 Book Source Code Copyright Note The course book is C++ Primer, 4th Edition by Lippman, Lajoie and Moo. Any references in earlier files to source files, and use of code within those files, are of example code given in and/or along with the book. As these slides are freely accessible on the Internet, not-for-profit, and for educational purposes, based on the permission related statements in the source code, I have considered that permission has been granted to use them in these slides. The course book is C++ Primer, 4th Edition by Lippman, Lajoie and Moo. Any references in earlier files to source files, and use of code within those files, are of example code given in and/or along with the book. As these slides are freely accessible on the Internet, not-for-profit, and for educational purposes, based on the permission related statements in the source code, I have considered that permission has been granted to use them in these slides.


Download ppt "Inheritance - 3. Virtual Functions Functions defined as virtual are ones that the base expects its derived classes may redefine. Functions defined as."

Similar presentations


Ads by Google