Safe Override

You are given a base class Shape.

  • Create a virtual function area() that returns 0.

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

  • Override the area() function using the override keyword to return area of rectangle i.e. length × width.

Your task is to:

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

Example 1:

Input: length = 5, width = 10

Output: 50

Example 2:

Input: length = 1, width = 5

Output: 5

Now Your Turn!

Pick the correct output for the given input

Input: length = 15, width = 5

Still unsure what the problem is asking ?

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

Constraints:

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

Input:

Length
Width