Building Class Hierarchy

Write a class Building and then create child classes House, School, OfficeBuilding, each with additional attributes like number of rooms for houses, number of classrooms for schools, and number of offices for office buildings. Implement methods to print the details.

Example 1:
Input: building = House("Green House", 3)
Output: Name: Green House, Rooms: 3
Example 2:
Input: building = School("High School", 20)
Output: Name: High School, Classrooms: 20

Use inheritance to create child classes and override the method to return the specific details for each building type.

class Building:
    def details(self):

class House(Building):
    def __init__(self, name, rooms): = name
        self.rooms = rooms

    def details(self):
        return f"Name: {}, Rooms: {self.rooms}"

class School(Building):
    def __init__(self, name, classrooms): = name
        self.classrooms = classrooms

    def details(self):
        return f"Name: {}, Classrooms: {self.classrooms}"

class OfficeBuilding(Building):
    def __init__(self, name, offices): = name
        self.offices = offices

    def details(self):
        return f"Name: {}, Offices: {self.offices}"

building = House("Green House", 3)

building = School("High School", 20)

© Let’s Data Science


Unlock AI & Data Science treasures. Log in!