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 inputInput: 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