Docsity

Polymorphism - Advanced Programming - Lecture Slides, Slides of Computer Science

Partial preview of the text

Download Polymorphism - Advanced Programming - Lecture Slides and more Slides Computer Science in PDF only on Docsity!

Chapter 20 - Polymorphism

Outline

20.5 Polymorphism

20.6 Case Study: A Payroll System Using Polymorphism

20.7 New Classes and Dynamic Binding

20.8 Virtual Destructors

20.9 Case Study: Inheriting Interface and Implementation

20.10 Polymorphism, virtual Functions and Dynamic

Binding “Under the Hood”

20.5 Polymorphism

  • Polymorphism:
    • Ability for objects of different classes to respond differently to the same function call
    • Base-class pointer (or reference) calls a virtual function
      • C++ chooses the correct overridden function in object
    • Suppose print not a virtual function

Employee e, *ePtr = &e;

HourlyWorker h, *hPtr = &h;

ePtr->print(); //call base-class print function

hPtr->print(); //call derived-class print function

ePtr=&h; //allowable implicit conversion

ePtr->print(); // still calls base-class print

20.7 Virtual Destructors

  • Problem:
    • If base-class pointer to a derived object is deleted, the base-class destructor will act on the object
  • Solution:
    • Declare a virtual base-class destructor
    • Now, the appropriate destructor will be called

20.8 Case Study: Inheriting Interface and Implementation

  • Re-examine the Point, Circle, Cylinder hierarchy - Use the abstract base class Shape to head the hierarchy

1. Point Definition

(derived class )

1.1 Function

Definitions

34 virtual void printShapeName() const { cout << "Point: "; } 35 virtual void print() const; 36 private: 37 int x, y; // x and y coordinates of Point 38 }; 39 40 #endif 41 // Fig. 20.1: point1.cpp 42 // Member function definitions for class Point 43 #include "point1.h" 44 45 Point::Point( int a, int b ) { setPoint( a, b ); } 46 47 void Point::setPoint( int a, int b ) 48 { 49 x = a; 50 y = b; 51 } 52 53 void Point::print() const 54 { cout << '[' << x << ", " << y << ']'; }

1. Circle Definition

(derived class)

55 // Fig. 20.1: circle1.h 56 // Definition of class Circle 57 #ifndef CIRCLE1_H 58 #define CIRCLE1_H 59 #include "point1.h" 60 61 class Circle : public Point { 62 public: 63 // default constructor 64 Circle( double r = 0.0, int x = 0, int y = 0 ); 65 66 void setRadius( double ); 67 double getRadius() const; 68 virtual double area() const; 69 virtual void printShapeName() const { cout << "Circle: "; } 70 virtual void print() const; 71 private: 72 double radius; // radius of Circle 73 }; 74 75 #endif

1. Cylinder Definition

(derived class )

100 // Fig. 20.1: cylindr1.h 101 // Definition of class Cylinder 102 #ifndef CYLINDR1_H 103 #define CYLINDR1_H 104 #include "circle1.h" 105 106 class Cylinder : public Circle { 107 public: 108 // default constructor 109 Cylinder( double h = 0.0, double r = 0.0, 110 int x = 0, int y = 0 ); 111 112 void setHeight( double ); 113 double getHeight(); 114 virtual double area() const; 115 virtual double volume() const; 116 virtual void printShapeName() const { cout << "Cylinder: "; } 117 virtual void print() const; 118 private: 119 double height; // height of Cylinder 120 }; 121 122 #endif

1.1 Function

Definitions

123 // Fig. 20.1: cylindr1.cpp 124 // Member and friend function definitions for class Cylinder 125 #include 126 127 using std::cout; 128 129 #include "cylindr1.h" 130 131 Cylinder::Cylinder( double h, double r, int x, int y ) 132 : Circle( r, x, y ) // call base-class constructor 133 { setHeight( h ); } 134 135 void Cylinder::setHeight( double h ) 136 { height = h > 0? h : 0; } 137 138 double Cylinder::getHeight() { return height; } 139 140 double Cylinder::area() const 141 { 142 // surface area of Cylinder 143 return 2 * Circle::area() + 144 2 * 3.14159 * getRadius() * height; 145 } 146 147 double Cylinder::volume() const 148 { return Circle::area() * height; } 149 150 void Cylinder::print() const 151 { 152 Circle::print(); 153 cout << "; Height = " << height; 154 }

Docsity.com

2. Function calls

186 point.print(); // static binding 187 cout << '\n'; 188 189 circle.printShapeName(); // static binding 190 circle.print(); // static binding 191 cout << '\n'; 192 193 cylinder.printShapeName(); // static binding 194 cylinder.print(); // static binding 195 cout << "\n\n"; 196 197 Shape *arrayOfShapes[ 3 ]; // array of base-class pointers 198 199 // aim arrayOfShapes[0] at derived-class Point object 200 arrayOfShapes[ 0 ] = &point; 201 202 // aim arrayOfShapes[1] at derived-class Circle object 203 arrayOfShapes[ 1 ] = &circle; 204 205 // aim arrayOfShapes[2] at derived-class Cylinder object 206 arrayOfShapes[ 2 ] = &cylinder; 207 208 // Loop through arrayOfShapes and call virtualViaPointer 209 // to print the shape name, attributes, area, and volume 210 // of each object using dynamic binding. 211 cout << "Virtual function calls made off " 212 << "base-class pointers\n"; 213 214 for ( int i = 0; i < 3; i++ ) 215 virtualViaPointer( arrayOfShapes[ i ] ); 216 217 // Loop through arrayOfShapes and call virtualViaReference 218 // to print the shape name, attributes, area, and volume 219 // of each object using dynamic binding.

Docsity.com

2. Function calls

3. Function Definitions

220 cout << "Virtual function calls made off " 221 << "base-class references\n"; 222 223 for ( int j = 0; j < 3; j++ ) 224 virtualViaReference( *arrayOfShapes[ j ] ); 225 226 return 0; 227 } 228 229 // Make virtual function calls off a base-class pointer 230 // using dynamic binding. 231 void virtualViaPointer( const Shape *baseClassPtr ) 232 { 233 baseClassPtr->printShapeName(); 234 baseClassPtr->print(); 235 cout << "\nArea = " << baseClassPtr->area() 236 << "\nVolume = " << baseClassPtr->volume() << "\n\n"; 237 } 238 239 // Make virtual function calls off a base-class reference 240 // using dynamic binding. 241 void virtualViaReference( const Shape &baseClassRef ) 242 { 243 baseClassRef.printShapeName(); 244 baseClassRef.print(); 245 cout << "\nArea = " << baseClassRef.area() 246 << "\nVolume = " << baseClassRef.volume() << "\n\n";

247 } Docsity.com

20.9 Polymorphism, virtual Functions and Dynamic Binding “Under the Hood”

  • When to use polymorphism
    • Polymorphism has a lot of overhead
    • Polymorphism is not used in STL (Standard Template Library) to optimize performance
  • virtual function table (vtable)
    • Every class with a virtual function has a vtable
    • For every virtual function, vtable has a pointer to the proper function - If a derived class has the same function as a base class, then the function

pointer points to the base-class function

  • Detailed explanation in Fig. 20.