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

Practical 15- Virtual and Pure Virtual Functions

In C++, virtual functions allow derived classes to override base class functions, ensuring the correct function is called based on the object type. A pure virtual function has no implementation in the base class and must be overridden in derived classes, making the base class abstract. The document provides examples of virtual and pure virtual functions through a Shape base class and derived Circle and Square classes, demonstrating polymorphism.

Uploaded by

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

Practical 15- Virtual and Pure Virtual Functions

In C++, virtual functions allow derived classes to override base class functions, ensuring the correct function is called based on the object type. A pure virtual function has no implementation in the base class and must be overridden in derived classes, making the base class abstract. The document provides examples of virtual and pure virtual functions through a Shape base class and derived Circle and Square classes, demonstrating polymorphism.

Uploaded by

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

Virtual and Pure Virtual Functions

In C++, a virtual function is a function that is defined in the base class and can be overridden
in derived classes. It ensures that the correct function is called for an object, regardless of
the type of reference (or pointer) used for the function call.

A pure virtual function is a virtual function that has no implementation in the base class and
must be overridden in any derived class. It makes the base class abstract, meaning you
cannot instantiate objects of the base class directly.

Example of Virtual and Pure Virtual Functions in C++:

cpp

#include <iostream>
using namespace std;

// Base class
class Shape {
public:
// Virtual function
virtual void draw() {
cout << "Drawing Shape" << endl;
}

// Pure virtual function


virtual double area() = 0;

// Virtual destructor
virtual ~Shape() {}
};

// Derived class
class Circle : public Shape {
private:
double radius;

public:
Circle(double r) : radius(r) {}

// Override virtual function


void draw() override {

1/4
cout << "Drawing Circle" << endl;
}

// Implement pure virtual function


double area() override {
return 3.14159 * radius * radius;
}
};

// Derived class
class Square : public Shape {
private:
double side;

public:
Square(double s) : side(s) {}

// Override virtual function


void draw() override {
cout << "Drawing Square" << endl;
}

// Implement pure virtual function


double area() override {
return side * side;
}
};

int main() {
// Create objects of derived classes
Circle c(5);
Square s(4);

// Use pointers to base class


Shape* shape1 = &c;
Shape* shape2 = &s;

// Call virtual functions (polymorphism)


shape1->draw(); // Outputs: Drawing Circle
shape2->draw(); // Outputs: Drawing Square

// Call pure virtual function implementations

2/4
cout << "Area of Circle: " << shape1->area() << endl; // Outputs: Area of
Circle: 78.53975
cout << "Area of Square: " << shape2->area() << endl; // Outputs: Area of
Square: 16

return 0;
}

Explanation:
1. Base class Shape :

Contains a virtual function draw() , which can be overridden by derived classes.

Contains a pure virtual function area() , which forces derived classes to implement
it.

The = 0 in the declaration of area() marks it as a pure virtual function.

2. Derived classes Circle and Square :

Override the draw() function to provide their own specific implementations.

Implement the area() function to calculate the area for each shape.

3. Main function:

Demonstrates polymorphism by using base class pointers ( Shape* ) to call


overridden functions in derived classes ( Circle and Square ).

The area() function is called polymorphically, and the correct implementation is


used based on the object type (either Circle or Square ).

Output:

mathematica

Drawing Circle
Drawing Square
Area of Circle: 78.53975
Area of Square: 16

Key Concepts:
Virtual Function: Ensures the correct function is called according to the object type,
even if the reference or pointer is to the base class.

3/4
Pure Virtual Function: Makes the base class abstract and forces derived classes to
implement the function.

4/4

You might also like