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): pass class House(Building): def __init__(self, name, rooms): self.name = name self.rooms = rooms def details(self): return f"Name: {self.name}, Rooms: {self.rooms}" class School(Building): def __init__(self, name, classrooms): self.name = name self.classrooms = classrooms def details(self): return f"Name: {self.name}, Classrooms: {self.classrooms}" class OfficeBuilding(Building): def __init__(self, name, offices): self.name = name self.offices = offices def details(self): return f"Name: {self.name}, Offices: {self.offices}" building = House("Green House", 3) print(building.details()) building = School("High School", 20) print(building.details())
Unlock AI & Data Science treasures. Log in!