Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 29
-
Upload
sameer-hane -
Category
Documents
-
view
224 -
download
5
Transcript of Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 29
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 29
1/33
Object-Oriented Programming
(OOP)
Lecture No. 29
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 29
2/33
Abstract Class
Shape
Line Circle Triangle
draw
calcArea
draw
calcArea
draw
calcArea
draw
calcArea
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 29
3/33
Abstract Class
Implements an abstract concept
Cannot be instantiated
Used for inheriting interface and/orimplementation
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 29
4/33
Concrete Class
Implements a concrete concept
Can be instantiated
May inherit from an abstract class oranother concrete class
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 29
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
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 29
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;
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 29
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/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 29
8/33
Pure Virtual Functions
A derived class of an abstract classremains abstract until it provides
implementation for all pure virtualfunctions
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 29
9/33
Shape Hierarchy
Shape
Line Circle Quadrilateral
draw = 0
draw draw
Rectangle
draw
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 29
10/33
Pure Virtual Functions
class Quadrilateral : public Shape {
// No overriding draw() method
}
Quadrilateral q; // Error!
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 29
11/33
Pure Virtual Functions
class Rectangle:public Quadrilateral{
public:
// void draw()virtual void draw() {
// function body
}
}
Rectangle r; // OK
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 29
12/33
Virtual Destructors
class Shape {
public:
~Shape() {
cout
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 29
13/33
Virtual Destructors
class Quadrilateral : public Shape {
public:
~Quadrilateral() {
cout
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 29
14/33
Virtual Destructors
class Rectangle : public
Quadrilateral {
public:
~Rectangle() {
cout
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 29
15/33
Virtual Destructors
When delete operator is applied to a base
class pointer, base class destructor is
called regardless of the object type
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 29
16/33
Virtual Destructors
int main() {
Shape* pShape = new Rectangle();
delete pShape;
return 0;
}
Output
Shape destructor called
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 29
17/33
Result
Shape Part
Quad Part
Rect Part
pShape
Quad Part
Rect Part
Before After
pShape
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 29
18/33
Virtual Destructors
Make the base class destructor virtual
class Shape {
public:
virtual ~Shape() {
cout
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 29
19/33
Virtual Destructors
class Quadrilateral : public Shape {
public:
virtual ~Quadrilateral() {
cout
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 29
20/33
Virtual Destructors
class Rectangle : public
Quadrilateral {
public:
virtual ~Rectangle() {
cout
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 29
21/33
Virtual Destructors
Now base class destructor will run after
the derived class destructor
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 29
22/33
Virtual Destructors
int main() {
Shape* pShape = new Recrangle();
delete pShape;
return 0;}
Output
Rectangle destructor calledQuadilateral destructor called
Shape destructor called
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 29
23/33
Result
Shape Part
Quad Part
Rect Part
pShape
Before After
pShape
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 29
24/33
Virtual FunctionsUsage
Inherit interface and implementation
Just inherit interface (Pure Virtual)
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 29
25/33
Inherit interface and
implementation
Shape
Line Circle Triangle
draw = 0
calcArea
draw draw
calcArea
draw
calcArea
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 29
26/33
Inherit interface and
implementation
class Shape {
virtual void draw() = 0;
virtual float calcArea() {
return 0;
}
}
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 29
27/33
Inherit interface and
implementation
Each derived class of Shapeinherits
default implementation of calcArea()
Some may override this, such as Circle
and Triangle
Others may not, such as Pointand Line
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 29
28/33
Inherit interface and
implementation
Each derived class of Shapeinherits
interface (prototype) of draw()
Each concrete derived class has toprovide body of draw() by overriding it
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 29
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
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 29
30/33
ExampleV Table
int main() {
Point p1( 10, 10 ), p2( 30, 30 );
Shape* pShape;
pShape = new Line( p1, p2 );
pShape->draw();
pShape->calcArea();
}
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 29
31/33
ExampleV Table
calcArea
draw
Shape vTable
draw
Line vTable
calcArea
Line object0
pShape
Shape
point1 = p1
point2 = p2
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 29
32/33
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
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 29
33/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 performancerequirements before making a functionvirtual