MultipIe Inheritance Using Rectangle Class
Understand multiple inheritance with an example of a derived Rectangle class.
We'll cover the following
Challenge
Write a program that has 3 classes in it (Shape
, Cost
, and Rectangle
). Of these, Shape
and Cost
should be independent classes, whereas Rectangle
should be derived from Shape
and Cost
.
The Shape
class should maintain shape type (Plate or Ring), length and breadth of the shape type. The Cost
class should have a getCost( )
function that returns the cost of polishing the shape based on the type of the shape. Assume that it costs $50 per plate and $22 per ring.
The Rectangle
class should contain a function getQuantity( )
that returns the area if the shape type is a plate and perimeter if the shape type is a rectangular ring. Create objects of the Rectangle
class in main( )
and call getQuantity( )
and getCost( )
using these objects to determine the quantity and polishing of each.
Sample run
Here’s what you should see when you run the program.
Shape type: plate with length = 10 and breadth = 20
Quantity = 200 and Cost = 10000
Shape type: ring with length = 2 and breadth = 3
Quantity = 10 and Cost = 220
Coding exercise
Your job is to define a Rectangle
class that is derived from two classes (Shape
and Cost
).
Get hands-on with 1400+ tech skills courses.