0% found this document useful (0 votes)
13 views

Lecture 10-Week10_Pure Virtual, abstract class

The document discusses Object Oriented Programming concepts, specifically focusing on Abstract Base Classes and Pure Virtual Functions. It explains the necessity of abstract classes to enforce the implementation of certain functions in derived classes, highlighting the role of pure virtual functions and interfaces. Additionally, it provides examples of geometric shapes like Rectangle and Square to illustrate these concepts.

Uploaded by

abdularham1385
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Lecture 10-Week10_Pure Virtual, abstract class

The document discusses Object Oriented Programming concepts, specifically focusing on Abstract Base Classes and Pure Virtual Functions. It explains the necessity of abstract classes to enforce the implementation of certain functions in derived classes, highlighting the role of pure virtual functions and interfaces. Additionally, it provides examples of geometric shapes like Rectangle and Square to illustrate these concepts.

Uploaded by

abdularham1385
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Object Oriented Programming

(CS1143)
Week 10

Department of Computer Science


Capital University of Science and Technology (CUST)
Outline

 Abstract Base Classes and Pure Virtual Functions


 Virtual Inheritance. Diamond Problem and its solution

2
Concrete Class and Abstract Class

 The classes we have designed so far are called concrete classes.


 A concrete class can be instantiated and create objects of its type.
 When we create a set of classes, sometimes we find that there is a
list of behaviors that are identical to all classes.
 For example, assume we define two classes named Rectangle and
Square. Both of these classes have at least two common behaviors:
getArea() and getPerimeter().
 How can we force the creator of these two classes to provide the
definition of both member functions for each class?
 We know that the formulas to find the area and perimeter of these
geometrical shapes differ, which means that each class must create
its own version of getArea and getPerimeter.

3
Abstract Class

 The solution in object-oriented programming is to create an abstract


class, which forces the designers of all derived classes to add these
two definitions to their classes.
 An abstract class is a class with at least one pure virtual function.

4
Pure Virtual Function

 A pure virtual function is a virtual function in which the declaration is


set to zero and there is no definition in the abstract class.
 virtual double getArea() =0;

5
No Instantiation of Objects for abstract class

 For an object to be instantiated from a class, the class must have the
definitions of all member functions.
 We cannot instantiate an object from an abstract class because it
does not have the definition of its pure virtual functions.

6
Definition of Pure Virtual Functions

 The abstract class does not define its pure virtual function, but every
class that inherits from the abstract class must provide the definition
of each pure virtual function or declare it as a pure virtual to be
defined in the next lower level of the hierarchy.
 If a derived class does not provide the definition of pure virtual
functions and also does not declare them as pure virtual, it also
becomes abstract. It will not give any error but it means now you
cannot create an object of this class also.

7
Interfaces

 An abstract class can have both virtual and pure virtual functions.
 In some cases, however, we may need to create a blueprint for
inherited classes.
 We can define a class with all pure virtual functions.
 This class is sometimes referred to as an interface; we cannot create
any implementation file from this class, only the interface file.

8
Example: Shape Class

9
10
Shape Class

11
Description

 Note that we have no data members and only pure virtual member
functions in this program.
 The reason is that we do not want objects instantiated from this
class; its purpose is only to force the derived classes to implement
the pure virtual member functions.
 The two pure member functions force each derived class to have at
least two member functions to calculate the area and perimeter of
the corresponding shape.

12
Square

13
Rectangle

14
Triangle

15
Area of triangle with 3 sides

 https://byjus.com/maths/area-of-triangle-with-3-sides/

16
Circle

17
Main

18

You might also like