Ask Question
27 February, 21:18

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.

+1
Answers (1)
  1. 27 February, 21:44
    0
    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 ())

    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.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Create a base class named rectangle that contains lenght and width data members. Form this class derive a class named Box having an ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers