Assume the existence of a Building class. Define a derived class , ApartmentBuilding that contains four (4) data members: an integer named numFloors, an integer named unitsPerFloor, a boolean named hasElevator, and a boolean named hasCentralAir. There is a constructor containing parameters for the initialization of the above variables (in the same order as they appear above). There are also two function: the first, getTotalUnits, accepts no parameters and returns the total number of units in the building; the second, isLuxuryBuilding accepts no parameters and returns true if the building has central air, an elevator and 2 or less units per floor.

LANGUAGE: C++

CHALLENGE:

Assume the existence of a Building class. Define a derived class , ApartmentBuilding that contains four (4) data members: an integer named numFloors, an integer named unitsPerFloor, a boolean named hasElevator, and a boolean named hasCentralAir. There is a constructor containing parameters for the initialization of the above variables (in the same order as they appear above). There are also two function: the first, getTotalUnits, accepts no parameters and returns the total number of units in the building; the second, isLuxuryBuilding accepts no parameters and returns true if the building has central air, an elevator and 2 or less units per floor.

SOLUTION:


class ApartmentBuilding: public Building {
    private:
        int numFloors;
        int unitsPerFloor;
        bool hasElevator;
        bool hasCentralAir;
    public:
       int getTotalUnits();
       bool isLuxuryBuilding();
       ApartmentBuilding(int , int , bool , bool );
};

ApartmentBuilding::ApartmentBuilding(int x, int y, bool a, bool b) {
    numFloors = x;
    unitsPerFloor = y;
    hasElevator = a;
    hasCentralAir = b;
} 

int ApartmentBuilding::getTotalUnits() {
    return numFloors * unitsPerFloor;
} 

bool ApartmentBuilding::isLuxuryBuilding(){
    if(hasCentralAir && hasElevator && unitsPerFloor <= 2) {
        return true;
    }
    return false;
}

Posted in