0% found this document useful (0 votes)
35 views3 pages

Pure Virtual Function and Abstract Class

Pure virtual functions are functions declared in a base class without an implementation and denoted by = 0. They must be implemented by all derived classes. The Shape base class declares a pure virtual calculateArea() function to calculate the area of different shapes. The Square and Circle derived classes both implement calculateArea() to calculate their respective areas based on the dimension data member inherited from Shape. This makes Shape an abstract base class that cannot be instantiated and exists solely to be inherited from.

Uploaded by

Jatin Garg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views3 pages

Pure Virtual Function and Abstract Class

Pure virtual functions are functions declared in a base class without an implementation and denoted by = 0. They must be implemented by all derived classes. The Shape base class declares a pure virtual calculateArea() function to calculate the area of different shapes. The Square and Circle derived classes both implement calculateArea() to calculate their respective areas based on the dimension data member inherited from Shape. This makes Shape an abstract base class that cannot be instantiated and exists solely to be inherited from.

Uploaded by

Jatin Garg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

C++ Abstract Class and Pure Virtual

Function
C++ Pure Virtual Functions

Pure virtual functions are used

 if a function doesn't have any use in the base class

 but the function must be implemented by all its derived classes

Let's take an example,

Suppose, we have derived Triangle , Square and Circle classes from


the Shape class, and we want to calculate the area of all these shapes.
In this case, we can create a pure virtual function named calculateArea() in
the Shape . Since it's a pure virtual function, all derived
classes Triangle , Square and Circle must include the calculateArea() function
with implementation.
A pure virtual function doesn't have the function body and it must end with = 0.

For example,

class Shape {
public:

// creating a pure virtual function


virtual void calculateArea() = 0;
};

Note: The = 0 syntax doesn't mean we are assigning 0 to the function. It's just
the way we define pure virtual functions.
Abstract Class

A class that contains a pure virtual function is known as an abstract class. In


the above example, the class Shape is an abstract class.

We cannot create objects of an abstract class. However, we can derive


classes from them, and use their data members and member functions
(except pure virtual functions).

Example: C++ Abstract Class and Pure Virtual Function

/ C++ program to calculate the area of a square and a circle

#include <iostream>
using namespace std;

// Abstract class
class Shape {
protected:
float dimension;

public:
void getDimension() {
cin >> dimension;
}

// pure virtual Function


virtual float calculateArea() = 0;
};

// Derived class
class Square : public Shape {
public:
float calculateArea() {
return dimension * dimension;
}
};

// Derived class
class Circle : public Shape {
public:
float calculateArea() {
return 3.14 * dimension * dimension;
}
};

int main() {
Square square;
Circle circle;

cout << "Enter the length of the square: ";


square.getDimension();
cout << "Area of square: " << square.calculateArea() << endl;

cout << "\nEnter radius of the circle: ";


circle.getDimension();
cout << "Area of circle: " << circle.calculateArea() << endl;

return 0;
}

Output

Enter the length of the square: 4


Area of square: 16

Enter radius of the circle: 5


Area of circle: 78.5

In this program, virtual float calculateArea() = 0; inside the Shape class is a


pure virtual function.
That's why we must provide the implementation of calculateArea() in both of
our derived classes, or else we will get an error.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy