Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Pure Virtual Method | Polymorphism Overview
C++ OOP
course content

Contenido del Curso

C++ OOP

C++ OOP

1. Fundamentals of OOP
2. Constructors and Destructors
3. Encapsulation Overview
4. Inheritance Overview
5. Polymorphism Overview

book
Pure Virtual Method

The concept of virtual method stands as a cornerstone for achieving polymorphism, a fundamental principle in designing flexible and extensible software systems. Pure virtual methods represent a powerful extension of this concept, providing a mechanism for defining interfaces and enabling dynamic binding.

Syntax of Pure Virtual method

A pure virtual method is declared with the virtual keyword and initialized to zero. It indicates that the function has no definition within the class and must be overridden by any subclass before objects of that class can be instantiated.

Note

This method functions similarly to a standard one, except that it lacks a body enclosed in { } and instead is terminated with = 0;, indicating no implementation. Nevertheless, the other rules for this method are the same as for the others.

Declaring a pure virtual method makes the class where it is declared abstract. This is means it becomes impossible to instantiate objects of it. This restriction arises because every method within the class must be implemented before creating and utilizing an instance in order to prevent errors and unpredictable behavior. Look at the code below:

h

Animal

copy
1234
class Animal { public: virtual void speak() = 0; };

The Animal class features a pure virtual method named speak() preventing the instantiation of objects from this class. This design choice is logical, given that the purpose of the virtual method is to represent the distinctive sounds each animal makes.

By making the method virtual, it allows each subclass to implement its unique version, capturing the individuality of each animal's noise. The decision to make the method pure virtual indicates that no default implementation exists, emphasizing the need for concrete implementations in derived classes.

cpp

main

copy
1234567
#include "Animal.h" int main() { // cannot declare variable 'animal' to be of abstract type Animal animal; }

This is also makes sense. Creating instances of the Animal class would be impractical and counterintuitive, as it represents an abstract concept that serves as a category for various animals. There is no specific behavior associated with a generic animal, reinforcing the abstract nature of the class and highlighting the importance of creating specialized subclasses to capture the nuances of each specific animal's sound.

cpp

main

copy
12345678910111213141516171819202122232425262728293031
#include <iostream> class Animal { public: // Pure virtual function to enforce implementation in derived classes virtual void speak() = 0; }; class Cat : public Animal { public: void speak() override { std::cout << "Meow!" << std::endl; } }; class Dog : public Animal { public: void speak() override { std::cout << "Bark!" << std::endl; } }; class Cow : public Animal { public: void speak() override { std::cout << "Moo!" << std::endl; } }; void pet(Animal& animal) { animal.speak(); } int main() { // Replace `Cat` with `Dog` or `Cow` to see their specific behavior Cat cat; pet(cat); }

While it's not possible to create an object of type Animal directly, we can still employ it as a parameter in a function. Similar to the example with buttons mentioned earlier, we leverage polymorphism to craft a versatile program that can dynamically alter its behavior during runtime.

Note

Attempt passing an object of another class to the function and check the output. Additionally, try to create an object of abstract class and look at the results.

What happens when a class contains a pure virtual method?

What happens when a class contains a pure virtual method?

Selecciona la respuesta correcta

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 5. Capítulo 3
We're sorry to hear that something went wrong. What happened?
some-alt