Ask Question
13 October, 07:13

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.

+3
Answers (1)
  1. 13 October, 08:03
    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 and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers