Pure Virtuality

You are given an abstract base class Shape.

  • Create a pure virtual function area().

You are also given a derived class Rectangle that inherits from Shape.

  • Implement the area() function to return area of rectangle i.e. length × width

Your task is to:

  • Use the values length and width to initialize the Rectangle object.
  • Demonstrate runtime polymorphism using a base class pointer.
  • Return the base class pointer pointing to the derived object.

Example 1:

Input: length = 8, width = 9

Output: 72

Example 2:

Input: length = 1, width = 10

Output: 10

Now Your Turn!

Pick the correct output for the given input

Input: length = 12, width = 8

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

0
class Shape {
//your code goes here
};
 
class Rectangle : public Shape {
//your code goes here
};
 
class Solution {
public:
Shape* getArea(int length, int width) {
//your code goes here
}
};
Test Case

Input:

Length
Width