Docsity

Concrete Class - Object Oriented Programming - Lecture Slides, Slides of Object Oriented Programming

Partial preview of the text

Download Concrete Class - Object Oriented Programming - Lecture Slides and more Slides Object Oriented Programming in PDF only on Docsity!

Object-Oriented Programming

(OOP)

Lecture No. 29

Abstract Class

Shape

Line Circle Triangle

draw

calcArea

draw

calcArea

draw

calcArea

draw

calcArea

Concrete Class

  • Implements a concrete concept
  • Can be instantiated
  • May inherit from an abstract class or

another concrete class

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

… Pure Virtual Functions

  • A class having pure virtual function(s)

becomes abstract

class Shape {

…

public:

virtual void draw() = 0;

} …

Shape s; // Error!

… Pure Virtual Functions

  • A derived class of an abstract class

remains abstract until it provides

implementation for all pure virtual

functions

… Pure Virtual Functions

class Quadrilateral : public Shape { … // No overriding draw() method } … Quadrilateral q; // Error!

… Pure Virtual Functions

class Rectangle:public Quadrilateral{ … public: // void draw() virtual void draw() { … // function body } } … Rectangle r; // OK

…Virtual Destructors

class Quadrilateral : public Shape {

… public: ~Quadrilateral() { cout << “Quadrilateral destructor called\n”; }

}

…Virtual Destructors

class Rectangle : public Quadrilateral { … public: ~Rectangle() { cout << “Rectangle destructor called\n”; }

}

…Virtual Destructors

int main() {

Shape* pShape = new Rectangle(); delete pShape; return 0;

}

  • Output

Shape destructor called

Result

Shape Part

Quad Part

Rect Part

pShape

Quad Part Rect Part

Before After

pShape

…Virtual Destructors

class Quadrilateral : public Shape {

… public: virtual ~Quadrilateral() { cout << “Quadrilateral destructor called\n”; }

}

…Virtual Destructors

class Rectangle : public Quadrilateral { … public: virtual ~Rectangle() { cout << “Rectangle destructor called\n”; }

}