Pure virtual Draw( ) Function

Learn to create a pure virtual Draw ( ) function in a Shape Class.

Challenge

Write a program to create a Shape class containing a pure virtual draw( ) function. Inherit from these two classes Rectangle and Circle. Create an array of upcasted pointers in main( ) and call the draw( ) function using its elements. Each call should take control of the appropriate class’ draw( ) function.

Sample run

Here’s what you should see when you run the program.

In Rectangle class's draw
In Circle class's draw
In Rectangle class's draw
In Circle class's draw
In Rectangle class's draw
In Circle class's draw
In Rectangle class's draw
In Circle class's draw
In Rectangle class's draw
In Circle class's draw

Coding exercise

Your job is to define a pure virtual draw ( ) method in the base class, Shape. Also, define draw ( ) methods in the derived classes Circle and Rectangle.

Get hands-on with 1400+ tech skills courses.