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

Course Content

C++ OOP

C++ OOP

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

book
Pure Virtual Method

The concept of virtual methods is key to achieving polymorphism, enabling flexible and extensible software design. Pure virtual methods extend this by defining interfaces and supporting 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.

virtual.h

virtual.h

copy
1
virtual void example() = 0;

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

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

Animal.h

Animal.h

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 makes sense, as 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 own version, capturing the individuality of each animal's sound. Declaring it as pure virtual indicates that no default implementation exists, emphasizing the need for concrete implementations in derived classes.

main.cpp

main.cpp

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

This 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 unique sounds of each specific animal.

main.cpp

main.cpp

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 use it as a parameter in a function. Similar to the earlier button example, this approach leverages polymorphism to craft a versatile program that can dynamically alter its behavior at runtime.

Note
Note

Try passing an object of a different class to the function and observe the output. Also, attempt to create an object of the abstract class to see how the compiler prevents instantiation due to unimplemented pure virtual methods.

question mark

What happens when a class contains a pure virtual method?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 5. ChapterΒ 3

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

course content

Course Content

C++ OOP

C++ OOP

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

book
Pure Virtual Method

The concept of virtual methods is key to achieving polymorphism, enabling flexible and extensible software design. Pure virtual methods extend this by defining interfaces and supporting 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.

virtual.h

virtual.h

copy
1
virtual void example() = 0;

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

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

Animal.h

Animal.h

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 makes sense, as 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 own version, capturing the individuality of each animal's sound. Declaring it as pure virtual indicates that no default implementation exists, emphasizing the need for concrete implementations in derived classes.

main.cpp

main.cpp

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

This 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 unique sounds of each specific animal.

main.cpp

main.cpp

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 use it as a parameter in a function. Similar to the earlier button example, this approach leverages polymorphism to craft a versatile program that can dynamically alter its behavior at runtime.

Note
Note

Try passing an object of a different class to the function and observe the output. Also, attempt to create an object of the abstract class to see how the compiler prevents instantiation due to unimplemented pure virtual methods.

question mark

What happens when a class contains a pure virtual method?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 5. ChapterΒ 3
some-alt