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

Pure Virtual MethodPure 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.h

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.cpp

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.

cat

Meow

dog

Bark

cow

Moo

cpp

main.cpp

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.


1. What is a pure virtual method?
2. What happens when a class contains a pure virtual method?

What is a pure virtual method?

Selecciona la respuesta correcta

What happens when a class contains a pure virtual method?

Selecciona la respuesta correcta

¿Todo estuvo claro?

Sección 5. Capítulo 3

Pure Virtual MethodPure 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.h

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.cpp

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.

cat

Meow

dog

Bark

cow

Moo

cpp

main.cpp

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.


1. What is a pure virtual method?
2. What happens when a class contains a pure virtual method?

What is a pure virtual method?

Selecciona la respuesta correcta

What happens when a class contains a pure virtual method?

Selecciona la respuesta correcta

¿Todo estuvo claro?

Sección 5. Capítulo 3
some-alt