Understanding Abstract Classes, Virtual Functions, and Destructors in OOP, Slides of Object Oriented Programming

An in-depth exploration of object-oriented programming (oop) concepts, focusing on abstract classes, pure virtual functions, and virtual destructors in c++. Learn how abstract classes implement abstract concepts, cannot be instantiated, and are used for inheriting interfaces and/or implementation. Discover the role of pure virtual functions in making a class abstract and the difference between abstract and concrete classes. Additionally, explore the concept of virtual destructors and their impact on base class destructors when delete operator is applied to a base class pointer.

Typology: Slides

2011/2012

Uploaded on 08/08/2012

anchita
anchita 🇮🇳

4.4

(7)

113 documents

1 / 17

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Object
Object-
-Oriented Programming
Oriented Programming
(OOP)
(OOP)
Lecture No. 29
Lecture No. 29
Abstract Class
Abstract Class
Shape
Line Circle Triangle
draw
calcArea
draw
calcArea
draw
calcArea
draw
calcArea
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Understanding Abstract Classes, Virtual Functions, and Destructors in OOP and more Slides Object Oriented Programming in PDF only on Docsity!

Object

Object

Oriented Programming

Oriented Programming

(OOP)

(OOP)

Lecture No. 29

Lecture No. 29

Abstract Class

Abstract Class

Implements an abstract concept

Implements an abstract concept

Cannot be instantiated

Cannot be instantiated

► ►

Used for inheriting interface and/or

Used for inheriting interface and/or

implementation

implementation

Pure Virtual Functions

Pure Virtual Functions

A class having pure virtual function(s)

A class having pure virtual function(s)

becomes abstract

becomes abstract

class Shape {

class Shape {

public:public:

virtual void draw() = 0;

virtual void draw() = 0;

Shape s;Shape s; // Error!// Error!

docsity.com

Shape Hierarchy

Shape Hierarchy

Shape

Line Circle Quadrilateral

draw = 0

draw draw

Rectangle

Virtual Destructors

Virtual Destructors

class Quadrilateral : public Shape { class Quadrilateral : public Shape {

public: public:

~Quadrilateral() {

~Quadrilateral() {

cout <<

cout << “

Quadrilateral destructor

Quadrilateral destructor

called

*called *

\

n

n ”

Virtual Destructors

Virtual Destructors

When delete operator is applied to a base

When delete operator is applied to a base

class pointer, base class destructor is called

class pointer, base class destructor is called

regardless of the object type

regardless of the object type

Virtual Destructors

Virtual Destructors

class Quadrilateral : public Shape { class Quadrilateral : public Shape {

public: public:

virtual ~Quadrilateral() {

virtual ~Quadrilateral() {

cout <<

cout << “

Quadrilateral destructor

Quadrilateral destructor

called

*called *

\

n

n ”

Virtual Destructors

Virtual Destructors

Now base class destructor will run after the

Now base class destructor will run after the

derived class destructor

derived class destructor

Inherit interface and

Inherit interface and

implementation

implementation

Shape

Line Circle Triangle

draw = 0

calcArea

draw draw

calcArea

draw

calcArea

docsity.com

Inherit interface and

Inherit interface and

implementation

implementation

Each derived class of

Each derived class of

Shape

Shape

inherits default

inherits default

implementation of implementation of calcArea()calcArea()

► ► Some may override this, such asSome may override this, such as CircleCircle

and and TriangleTriangle

► ► Others may not, such asOthers may not, such as PointPoint andand LineLine

Example

Example

V Table

V Table

calcArea

draw

Shape vTable

… draw

Line vTable

calcArea

Line object

Shape …

point1 = p

point2 = p

Conclusion

Conclusion

► ► Polymorphism addsPolymorphism adds

Memory overhead due to vTables

Memory overhead due to vTables

  Processing overhead due to extra pointerProcessing overhead due to extra pointer

manipulation

manipulation

► ► However, this overhead is acceptable forHowever, this overhead is acceptable for

many of the applications

many of the applications

Moral:

Moral:

Think about performance

Think about performance

requirements before making a function

requirements before making a function

virtual virtual””