polymorphism (oop) lecture29
TRANSCRIPT
![Page 1: Polymorphism (oop) lecture29](https://reader035.vdocuments.us/reader035/viewer/2022062303/554fb184b4c90586258b5195/html5/thumbnails/1.jpg)
Object-Oriented Programming (OOP)
Lecture No. 29
![Page 2: Polymorphism (oop) lecture29](https://reader035.vdocuments.us/reader035/viewer/2022062303/554fb184b4c90586258b5195/html5/thumbnails/2.jpg)
Abstract Class
Shape
Line Circle Triangle
drawcalcArea
drawcalcArea
drawcalcArea
drawcalcArea
![Page 3: Polymorphism (oop) lecture29](https://reader035.vdocuments.us/reader035/viewer/2022062303/554fb184b4c90586258b5195/html5/thumbnails/3.jpg)
Abstract Class
• Implements an abstract concept
• Cannot be instantiated
• Used for inheriting interface and/or implementation
![Page 4: Polymorphism (oop) lecture29](https://reader035.vdocuments.us/reader035/viewer/2022062303/554fb184b4c90586258b5195/html5/thumbnails/4.jpg)
Concrete Class
• Implements a concrete concept
• Can be instantiated
• May inherit from an abstract class or another concrete class
![Page 5: Polymorphism (oop) lecture29](https://reader035.vdocuments.us/reader035/viewer/2022062303/554fb184b4c90586258b5195/html5/thumbnails/5.jpg)
Abstract Classes in C++
• In C++, we can make a class abstract by making its function(s) pure virtual
• Conversely, a class with no pure virtual function is a concrete class
![Page 6: Polymorphism (oop) lecture29](https://reader035.vdocuments.us/reader035/viewer/2022062303/554fb184b4c90586258b5195/html5/thumbnails/6.jpg)
Pure Virtual Functions function
• A pure virtual represents an abstract behavior and therefore may not have its implementation (body)
• A function is declared pure virtual by following its header with “= 0”
virtual void draw() = 0;
![Page 7: Polymorphism (oop) lecture29](https://reader035.vdocuments.us/reader035/viewer/2022062303/554fb184b4c90586258b5195/html5/thumbnails/7.jpg)
… Pure Virtual Functions
• A class having pure virtual function(s) becomes abstract
class Shape {…
public:
virtual void draw() = 0;
}…
Shape s; // Error!
![Page 8: Polymorphism (oop) lecture29](https://reader035.vdocuments.us/reader035/viewer/2022062303/554fb184b4c90586258b5195/html5/thumbnails/8.jpg)
… Pure Virtual Functions
• A derived class of an abstract class remains abstract until it provides implementation for all pure virtual functions
![Page 9: Polymorphism (oop) lecture29](https://reader035.vdocuments.us/reader035/viewer/2022062303/554fb184b4c90586258b5195/html5/thumbnails/9.jpg)
Shape Hierarchy
Shape
Line Circle Quadrilateral
draw = 0
draw draw
Rectangledraw
![Page 10: Polymorphism (oop) lecture29](https://reader035.vdocuments.us/reader035/viewer/2022062303/554fb184b4c90586258b5195/html5/thumbnails/10.jpg)
… Pure Virtual Functions
class Quadrilateral : public Shape {…// No overriding draw() method
}…Quadrilateral q; // Error!
![Page 11: Polymorphism (oop) lecture29](https://reader035.vdocuments.us/reader035/viewer/2022062303/554fb184b4c90586258b5195/html5/thumbnails/11.jpg)
… Pure Virtual Functions
class Rectangle:public Quadrilateral{…public:// void draw()virtual void draw() {
… // function body}
}…Rectangle r; // OK
![Page 12: Polymorphism (oop) lecture29](https://reader035.vdocuments.us/reader035/viewer/2022062303/554fb184b4c90586258b5195/html5/thumbnails/12.jpg)
Virtual Destructors
class Shape {
…
public:
~Shape() {
cout << “Shape destructor called\n”;
}
}
![Page 13: Polymorphism (oop) lecture29](https://reader035.vdocuments.us/reader035/viewer/2022062303/554fb184b4c90586258b5195/html5/thumbnails/13.jpg)
…Virtual Destructors
class Quadrilateral : public Shape {
…
public:
~Quadrilateral() {
cout << “Quadrilateral destructor called\n”;
}
}
![Page 14: Polymorphism (oop) lecture29](https://reader035.vdocuments.us/reader035/viewer/2022062303/554fb184b4c90586258b5195/html5/thumbnails/14.jpg)
…Virtual Destructors
class Rectangle : public Quadrilateral {
…
public:
~Rectangle() {
cout << “Rectangle destructor called\n”;
}
}
![Page 15: Polymorphism (oop) lecture29](https://reader035.vdocuments.us/reader035/viewer/2022062303/554fb184b4c90586258b5195/html5/thumbnails/15.jpg)
…Virtual Destructors
• When delete operator is applied to a base class pointer, base class destructor is called regardless of the object type
![Page 16: Polymorphism (oop) lecture29](https://reader035.vdocuments.us/reader035/viewer/2022062303/554fb184b4c90586258b5195/html5/thumbnails/16.jpg)
…Virtual Destructors
int main() {
Shape* pShape = new Rectangle();
delete pShape;
return 0;
}
• OutputShape destructor called
![Page 17: Polymorphism (oop) lecture29](https://reader035.vdocuments.us/reader035/viewer/2022062303/554fb184b4c90586258b5195/html5/thumbnails/17.jpg)
Result
Shape PartQuad PartRect Part
pShape
Quad PartRect Part
Before After
pShape
![Page 18: Polymorphism (oop) lecture29](https://reader035.vdocuments.us/reader035/viewer/2022062303/554fb184b4c90586258b5195/html5/thumbnails/18.jpg)
Virtual Destructors
• Make the base class destructor virtual
class Shape {…public:virtual ~Shape() {
cout << “Shape destructor called\n”; }
}
![Page 19: Polymorphism (oop) lecture29](https://reader035.vdocuments.us/reader035/viewer/2022062303/554fb184b4c90586258b5195/html5/thumbnails/19.jpg)
…Virtual Destructors
class Quadrilateral : public Shape {
…
public:
virtual ~Quadrilateral() {
cout << “Quadrilateral destructor called\n”;
}
}
![Page 20: Polymorphism (oop) lecture29](https://reader035.vdocuments.us/reader035/viewer/2022062303/554fb184b4c90586258b5195/html5/thumbnails/20.jpg)
…Virtual Destructors
class Rectangle : public Quadrilateral {
…
public:
virtual ~Rectangle() {
cout << “Rectangle destructor called\n”;
}
}
![Page 21: Polymorphism (oop) lecture29](https://reader035.vdocuments.us/reader035/viewer/2022062303/554fb184b4c90586258b5195/html5/thumbnails/21.jpg)
…Virtual Destructors
• Now base class destructor will run after the derived class destructor
![Page 22: Polymorphism (oop) lecture29](https://reader035.vdocuments.us/reader035/viewer/2022062303/554fb184b4c90586258b5195/html5/thumbnails/22.jpg)
…Virtual Destructors
int main() {Shape* pShape = new Recrangle();delete pShape;return 0;
}
• OutputRectangle destructor calledQuadilateral destructor calledShape destructor called
![Page 23: Polymorphism (oop) lecture29](https://reader035.vdocuments.us/reader035/viewer/2022062303/554fb184b4c90586258b5195/html5/thumbnails/23.jpg)
Result
Shape PartQuad PartRect Part
pShape
Before After
pShape
![Page 24: Polymorphism (oop) lecture29](https://reader035.vdocuments.us/reader035/viewer/2022062303/554fb184b4c90586258b5195/html5/thumbnails/24.jpg)
Virtual Functions – Usage
• Inherit interface and implementation
• Just inherit interface (Pure Virtual)
![Page 25: Polymorphism (oop) lecture29](https://reader035.vdocuments.us/reader035/viewer/2022062303/554fb184b4c90586258b5195/html5/thumbnails/25.jpg)
Inherit interface and implementation
Shape
Line Circle Triangle
draw = 0calcArea
draw drawcalcArea
drawcalcArea
![Page 26: Polymorphism (oop) lecture29](https://reader035.vdocuments.us/reader035/viewer/2022062303/554fb184b4c90586258b5195/html5/thumbnails/26.jpg)
…Inherit interface and implementation
class Shape {…virtual void draw() = 0;
virtual float calcArea() {return 0;
}}
![Page 27: Polymorphism (oop) lecture29](https://reader035.vdocuments.us/reader035/viewer/2022062303/554fb184b4c90586258b5195/html5/thumbnails/27.jpg)
…Inherit interface and implementation
• Each derived class of Shape inherits default implementation of calcArea()
• Some may override this, such as Circle and Triangle
• Others may not, such as Point and Line
![Page 28: Polymorphism (oop) lecture29](https://reader035.vdocuments.us/reader035/viewer/2022062303/554fb184b4c90586258b5195/html5/thumbnails/28.jpg)
…Inherit interface and implementation
• Each derived class of Shape inherits interface (prototype) of draw()
• Each concrete derived class has to provide body of draw() by overriding it
![Page 29: Polymorphism (oop) lecture29](https://reader035.vdocuments.us/reader035/viewer/2022062303/554fb184b4c90586258b5195/html5/thumbnails/29.jpg)
V Table
• Compiler builds a virtual function table (vTable) for each class having virtual functions
• A vTable contains a pointer for each virtual function
![Page 30: Polymorphism (oop) lecture29](https://reader035.vdocuments.us/reader035/viewer/2022062303/554fb184b4c90586258b5195/html5/thumbnails/30.jpg)
Example – V Table
int main() {Point p1( 10, 10 ), p2( 30, 30 );Shape* pShape;
pShape = new Line( p1, p2 );pShape->draw();pShape->calcArea();
}
![Page 31: Polymorphism (oop) lecture29](https://reader035.vdocuments.us/reader035/viewer/2022062303/554fb184b4c90586258b5195/html5/thumbnails/31.jpg)
Example – V Table
calcArea
draw
…Shape vTable
draw…
Line vTablecalcArea
Line object0
pShape
Shape …
point1 = p1
point2 = p2
![Page 32: Polymorphism (oop) lecture29](https://reader035.vdocuments.us/reader035/viewer/2022062303/554fb184b4c90586258b5195/html5/thumbnails/32.jpg)
Dynamic Dispatch
• For non-virtual functions, compiler just generates code to call the function
• In case of virtual functions, compiler generates code to– access the object– access the associated vTable– call the appropriate function
![Page 33: Polymorphism (oop) lecture29](https://reader035.vdocuments.us/reader035/viewer/2022062303/554fb184b4c90586258b5195/html5/thumbnails/33.jpg)
Conclusion
• Polymorphism adds– Memory overhead due to vTables– Processing overhead due to extra pointer
manipulation
• However, this overhead is acceptable for many of the applications
• Moral: “Think about performance requirements before making a function virtual”