Object-Oriented Programming
(OOP)
Lecture No. 29
Abstract Class
Shape
Line Circle Triangle
draw
calcArea
draw
calcArea
draw
calcArea
draw
calcArea
Abstract Class
• Implements an abstract concept
• Cannot be instantiated
• Used for inheriting interface and/or
implementa...
Concrete Class
• Implements a concrete concept
• Can be instantiated
• May inherit from an abstract class or
another concr...
Abstract Classes in C++
• In C++, we can make a class abstract by
making its function(s) pure virtual
• Conversely, a clas...
Pure Virtual Functions function
• A pure virtual represents an abstract
behavior and therefore may not have its
implementa...
… Pure Virtual Functions
• A class having pure virtual function(s)
becomes abstract
class Shape {
…
public:
virtual void d...
… Pure Virtual Functions
• A derived class of an abstract class
remains abstract until it provides
implementation for all ...
Shape Hierarchy
Shape
Line Circle Quadrilateral
draw = 0
draw draw
Rectangle
draw
… Pure Virtual Functions
class Quadrilateral : public Shape {
…
// No overriding draw() method
}
…
Quadrilateral q; // Err...
… Pure Virtual Functions
class Rectangle:public Quadrilateral{
…
public:
// void draw()
virtual void draw() {
… // functio...
Virtual Destructors
class Shape {
…
public:
~Shape() {
cout << “Shape destructor
calledn”;
}
}
…Virtual Destructors
class Quadrilateral : public Shape {
…
public:
~Quadrilateral() {
cout << “Quadrilateral destructor
c...
…Virtual Destructors
class Rectangle : public
Quadrilateral {
…
public:
~Rectangle() {
cout << “Rectangle destructor
calle...
…Virtual Destructors
• When delete operator is applied to a base
class pointer, base class destructor is
called regardless...
…Virtual Destructors
int main() {
Shape* pShape = new Rectangle();
delete pShape;
return 0;
}
• Output
Shape destructor ca...
Result
Shape Part
Quad Part
Rect Part
pShape
Quad Part
Rect Part
Before After
pShape
Virtual Destructors
• Make the base class destructor virtual
class Shape {
…
public:
virtual ~Shape() {
cout << “Shape des...
…Virtual Destructors
class Quadrilateral : public Shape {
…
public:
virtual ~Quadrilateral() {
cout << “Quadrilateral dest...
…Virtual Destructors
class Rectangle : public
Quadrilateral {
…
public:
virtual ~Rectangle() {
cout << “Rectangle destruct...
…Virtual Destructors
• Now base class destructor will run after
the derived class destructor
…Virtual Destructors
int main() {
Shape* pShape = new Recrangle();
delete pShape;
return 0;
}
• Output
Rectangle destructo...
Result
Shape Part
Quad Part
Rect Part
pShape
Before After
pShape
Virtual Functions – Usage
• Inherit interface and implementation
• Just inherit interface (Pure Virtual)
Inherit interface and
implementation
Shape
Line Circle Triangle
draw = 0
calcArea
draw draw
calcArea
draw
calcArea
…Inherit interface and
implementation
class Shape {
…
virtual void draw() = 0;
virtual float calcArea() {
return 0;
}
}
…Inherit interface and
implementation
• Each derived class of Shape inherits
default implementation of calcArea()
• Some m...
…Inherit interface and
implementation
• Each derived class of Shape inherits
interface (prototype) of draw()
• Each concre...
V Table
• Compiler builds a virtual function table
(vTable) for each class having virtual
functions
• A vTable contains a ...
Example – V Table
int main() {
Point p1( 10, 10 ), p2( 30, 30 );
Shape* pShape;
pShape = new Line( p1, p2 );
pShape->draw(...
Example – V Table
calcArea
draw
…
Shape vTable
draw…
Line vTable
calcArea
Line object0
pShape
Shape …
point1 = p1
point2 =...
Dynamic Dispatch
• For non-virtual functions, compiler just
generates code to call the function
• In case of virtual funct...
Conclusion
• Polymorphism adds
– Memory overhead due to vTables
– Processing overhead due to extra pointer
manipulation
• ...
of 33

Polymorphism (oop) lecture29

Published on: Mar 4, 2016
Published in: Technology      
Source: www.slideshare.net


Transcripts - Polymorphism (oop) lecture29

  • 1. Object-Oriented Programming (OOP) Lecture No. 29
  • 2. Abstract Class Shape Line Circle Triangle draw calcArea draw calcArea draw calcArea draw calcArea
  • 3. Abstract Class • Implements an abstract concept • Cannot be instantiated • Used for inheriting interface and/or implementation
  • 4. Concrete Class • Implements a concrete concept • Can be instantiated • May inherit from an abstract class or another concrete class
  • 5. 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
  • 6. 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;
  • 7. … Pure Virtual Functions • A class having pure virtual function(s) becomes abstract class Shape { … public: virtual void draw() = 0; } … Shape s; // Error!
  • 8. … Pure Virtual Functions • A derived class of an abstract class remains abstract until it provides implementation for all pure virtual functions
  • 9. Shape Hierarchy Shape Line Circle Quadrilateral draw = 0 draw draw Rectangle draw
  • 10. … Pure Virtual Functions class Quadrilateral : public Shape { … // No overriding draw() method } … Quadrilateral q; // Error!
  • 11. … Pure Virtual Functions class Rectangle:public Quadrilateral{ … public: // void draw() virtual void draw() { … // function body } } … Rectangle r; // OK
  • 12. Virtual Destructors class Shape { … public: ~Shape() { cout << “Shape destructor calledn”; } }
  • 13. …Virtual Destructors class Quadrilateral : public Shape { … public: ~Quadrilateral() { cout << “Quadrilateral destructor calledn”; } }
  • 14. …Virtual Destructors class Rectangle : public Quadrilateral { … public: ~Rectangle() { cout << “Rectangle destructor calledn”; } }
  • 15. …Virtual Destructors • When delete operator is applied to a base class pointer, base class destructor is called regardless of the object type
  • 16. …Virtual Destructors int main() { Shape* pShape = new Rectangle(); delete pShape; return 0; } • Output Shape destructor called
  • 17. Result Shape Part Quad Part Rect Part pShape Quad Part Rect Part Before After pShape
  • 18. Virtual Destructors • Make the base class destructor virtual class Shape { … public: virtual ~Shape() { cout << “Shape destructor calledn”; } }
  • 19. …Virtual Destructors class Quadrilateral : public Shape { … public: virtual ~Quadrilateral() { cout << “Quadrilateral destructor calledn”; } }
  • 20. …Virtual Destructors class Rectangle : public Quadrilateral { … public: virtual ~Rectangle() { cout << “Rectangle destructor calledn”; } }
  • 21. …Virtual Destructors • Now base class destructor will run after the derived class destructor
  • 22. …Virtual Destructors int main() { Shape* pShape = new Recrangle(); delete pShape; return 0; } • Output Rectangle destructor called Quadilateral destructor called Shape destructor called
  • 23. Result Shape Part Quad Part Rect Part pShape Before After pShape
  • 24. Virtual Functions – Usage • Inherit interface and implementation • Just inherit interface (Pure Virtual)
  • 25. Inherit interface and implementation Shape Line Circle Triangle draw = 0 calcArea draw draw calcArea draw calcArea
  • 26. …Inherit interface and implementation class Shape { … virtual void draw() = 0; virtual float calcArea() { return 0; } }
  • 27. …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
  • 28. …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
  • 29. V Table • Compiler builds a virtual function table (vTable) for each class having virtual functions • A vTable contains a pointer for each virtual function
  • 30. Example – V Table int main() { Point p1( 10, 10 ), p2( 30, 30 ); Shape* pShape; pShape = new Line( p1, p2 ); pShape->draw(); pShape->calcArea(); }
  • 31. Example – V Table calcArea draw … Shape vTable draw… Line vTable calcArea Line object0 pShape Shape … point1 = p1 point2 = p2
  • 32. 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
  • 33. 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”

Related Documents