31.7k views
0 votes
Create a base class named rectangle that contains lenght and width data members.

Form this class derive a class named Box having an additional data member named depth. The function members of the base rectangle class should consist of a constructor and an area function.

The derived box class should have a constructor, a voulume function, and an override function named area that returns the surface area of the box.

User Kkica
by
5.5k points

1 Answer

3 votes

Answer:

class Rectangle:

def __init__(self, length, width):

self.length = length

self.width = width

def area(self):

area = self.length*self.width

return area

class Box (Rectangle):

def __init__(self, length, width, height):

super().__init__(length, width)

self.height = height

def volume(self):

volume = self.length * self.width * self.height

return volume

def area(self):

area = 2*(self.length*self.width) + 2*(self.length*self.height) + 2*(self.width*self.height)

return area

rec_1 = Rectangle(2,4)

box_1 = Box(2,2,6)

print(box_1.length)

print(box_1.area())

print(rec_1.length)

print(rec_1.area())

Step-by-step explanation:

The programming language used is python.

class Rectangle

The class Rectangle is created with attributes length and width.

The class attributes are initialized using the __init__ constructor.

In the rectangle class, a method/function is declared to return the area of the rectangle.

class Box

This class is a child of rectangle and it inherits all its attributes and methods,

It has an additional attribute of depth and its constructor is used to initialize it.

The class contains a volume function, that returns the volume and an override function 'area()' that displaces the area from the parent class.

Finally, some instances of both classes are created.

User Kaffekopp
by
5.1k points