Chapter 11 : Inheritance and Polymorphism
- Create a class named Rectangle that contains data fields for height and length and a method named getArea() that returns the area of this Rectangle.
Create a child class (derived from Rectangle) named Cube. Cube contains an additional data field named depth, and a getArea() method that overrides the parent method.
Draw the UML diagram for the class (Class Rectangle and Class Cube) and then implement the class. Write a test program that creates one Rectangle object with height 4 and length 40 and one Cube object height 15, length 20 and depth 45.
Display height, length and area of each object.
- Create a base class named Point consisting of x and y data members representing point coordinates. From this class, derive a class named Circle with another data member named radius. For this derived class, the x and y data members represent a circle’s centre coordinates. The member method of the Point class should consist of a constructor that set the value for x and y to 0, an area() method that return 0.
The derived class should has an override method named area() that return’s a circle area
Area = 3.14 x radius2.
Draw UML diagram for the class Point and class Circle and then implement the class. Write a test program that creates one Point object and one Circle object with x 5, y 7 and radius 13. Display x and y for Point Object; x, y and radius for Circle Object.