computer notes - abstract class
TRANSCRIPT
-
8/3/2019 Computer Notes - Abstract Class
1/33
Abstract Class
http://ecomputernotes.com
-
8/3/2019 Computer Notes - Abstract Class
2/33
Abstract Class
Shape
Line Circle Triangle
draw
calcArea
draw
calcArea
draw
calcArea
draw
calcArea
http://ecomputernotes.com
-
8/3/2019 Computer Notes - Abstract Class
3/33
Abstract Class
Implements an abstract concept
Cannot be instantiated
Used for inheriting interface and/orimplementation
http://ecomputernotes.com
-
8/3/2019 Computer Notes - Abstract Class
4/33
Concrete Class
Implements a concrete concept
Can be instantiated
May inherit from an abstract class or anotherconcrete class
http://ecomputernotes.com
-
8/3/2019 Computer Notes - Abstract Class
5/33
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
http://ecomputernotes.com
-
8/3/2019 Computer Notes - Abstract Class
6/33
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;
http://ecomputernotes.com
-
8/3/2019 Computer Notes - Abstract Class
7/33
Pure Virtual Functions A class having pure virtual function(s)
becomes abstract
class Shape {
public:
virtual void draw() = 0;
}
Shape s; // Error!
-
8/3/2019 Computer Notes - Abstract Class
8/33
Pure Virtual Functions
A derived class of an abstract class remainsabstract until it provides implementation for allpure virtual functions
http://ecomputernotes.com
-
8/3/2019 Computer Notes - Abstract Class
9/33
Shape HierarchyShape
Line Circle Quadrilateral
draw = 0
draw draw
Rectangle
drawhttp://ecomputernotes.com
-
8/3/2019 Computer Notes - Abstract Class
10/33
Pure Virtual Functions
class Quadrilateral : public Shape {
// No overriding draw() method}
Quadrilateral q; // Error!
http://ecomputernotes.com
-
8/3/2019 Computer Notes - Abstract Class
11/33
Pure Virtual Functionsclass Rectangle:public Quadrilateral{
public:
// void draw()
virtual void draw() { // function body
}
}
Rectangle r; // OK
http://ecomputernotes.com
-
8/3/2019 Computer Notes - Abstract Class
12/33
Virtual Destructorsclass Shape {
public:
~Shape() {
cout
-
8/3/2019 Computer Notes - Abstract Class
13/33
Virtual Destructorsclass Quadrilateral : public Shape {
public:
~Quadrilateral() {
cout
-
8/3/2019 Computer Notes - Abstract Class
14/33
Virtual Destructorsclass Rectangle : public
Quadrilateral {
public:
~Rectangle() {
cout
-
8/3/2019 Computer Notes - Abstract Class
15/33
Virtual Destructors
When delete operator is applied to a base
class pointer, base class destructor is called
regardless of the object type
http://ecomputernotes.com
-
8/3/2019 Computer Notes - Abstract Class
16/33
Virtual Destructorsint main() {
Shape* pShape = new Rectangle();
delete pShape;
return 0;
}
OutputShape destructor called
http://ecomputernotes.com
-
8/3/2019 Computer Notes - Abstract Class
17/33
Result
Shape Part
Quad Part
Rect Part
pShape
Quad Part
Rect Part
Before After
pShape
http://ecomputernotes.com
-
8/3/2019 Computer Notes - Abstract Class
18/33
Virtual Destructors Make the base class destructor virtual
class Shape {
public:
virtual ~Shape() {
cout
-
8/3/2019 Computer Notes - Abstract Class
19/33
Virtual Destructorsclass Quadrilateral : public Shape {
public:
virtual ~Quadrilateral() {
cout
-
8/3/2019 Computer Notes - Abstract Class
20/33
Virtual Destructorsclass Rectangle : public
Quadrilateral {
public:
virtual ~Rectangle() {
cout
-
8/3/2019 Computer Notes - Abstract Class
21/33
Virtual Destructors
Now base class destructor will run after thederived class destructor
http://ecomputernotes.com
-
8/3/2019 Computer Notes - Abstract Class
22/33
Virtual Destructorsint main() {
Shape* pShape = new Recrangle();delete pShape;
return 0;
}
OutputRectangle destructor called
Quadilateral destructor called
Shape destructor called
http://ecomputernotes.com
-
8/3/2019 Computer Notes - Abstract Class
23/33
Result
Shape Part
Quad Part
Rect Part
pShape
Before After
pShape
http://ecomputernotes.com
-
8/3/2019 Computer Notes - Abstract Class
24/33
Virtual Functions Usage
Inherit interface and implementation
Just inherit interface (Pure Virtual)
http://ecomputernotes.com
-
8/3/2019 Computer Notes - Abstract Class
25/33
Inherit interface and
implementation
Shape
Line Circle Triangle
draw = 0
calcArea
draw draw
calcArea
draw
calcArea
-
8/3/2019 Computer Notes - Abstract Class
26/33
Inherit interface and
implementation
class Shape {
virtual void draw() = 0;
virtual float calcArea() {
return 0;
}}
http://ecomputernotes.com
-
8/3/2019 Computer Notes - Abstract Class
27/33
Inherit interface and
implementation
Each derived class ofShape inherits defaultimplementation ofcalcArea()
Some may override this, such as Circle andTriangle
Others may not, such as Point and Line
http://ecomputernotes.com
-
8/3/2019 Computer Notes - Abstract Class
28/33
Inherit interface and
implementation
Each derived class ofShape inherits interface(prototype) ofdraw()
Each concrete derived class has to providebody ofdraw() by overriding it
http://ecomputernotes.com
-
8/3/2019 Computer Notes - Abstract Class
29/33
V Table
Compiler builds a virtual function table(vTable) for each class having virtual functions
A vTable contains a pointer for each virtual
function
http://ecomputernotes.com
-
8/3/2019 Computer Notes - Abstract Class
30/33
Example V Table
int main() {Point p1( 10, 10 ), p2( 30, 30 );
Shape* pShape;
pShape = new Line( p1, p2 );
pShape->draw();
pShape->calcArea();}
http://ecomputernotes.com
-
8/3/2019 Computer Notes - Abstract Class
31/33
Example V Table
calcAreadraw
Shape vTable
draw
Line vTable
calcArea
Line object0
pShape
Shape
point1 = p1
point2 = p2
http://ecomputernotes.com
-
8/3/2019 Computer Notes - Abstract Class
32/33
Dynamic Dispatch For non-virtual functions, compiler just
generates code to call the function
In case of virtual functions, compiler generatescode to
access the object
access the associated vTable call the appropriate function
http://ecomputernotes.com
-
8/3/2019 Computer Notes - Abstract Class
33/33
Conclusion Polymorphism adds
Memory overhead due to vTables Processing overhead due to extra pointer
manipulation
However, this overhead is acceptable formany of the applications
Moral: Think about performance requirements
before making a function virtual
http://ecomputernotes.com