Having troubles with the subject inheritence. This is a myProgramming question u
ID: 3860643 • Letter: H
Question
Having troubles with the subject inheritence. This is a myProgramming question using c++. I was hoping to get some help. Thank you.
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 functions: the first, getTotalUnits, accepts no parametersand 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.
Explanation / Answer
#include <iostream>
using namespace std;
class Building
{
public:
void display()
{
cout<<"Base class"<<endl;
}
};
class ApartmentBuilding : public Building
{
private:
int numFloors;
int unitPerFloor;
char hasElevator;
char hasCentralAir;
public:
ApartmentBuilding();
void setNumFloors(int);
int getNumFloors();
void setTotalUnits(int);
void setHasElevator(char);
char getHasElevator();
int getTotalUnits();
void setHasCentralAir(char);
char getHasCentralAir();
bool isLuxuryBuilding();
};
ApartmentBuilding::ApartmentBuilding() {
numFloors = 0;
unitPerFloor = 0;
hasElevator = 'y';
hasCentralAir = 'y';
}
void ApartmentBuilding :: setNumFloors(int numFloors){
numFloors=numFloors;
}
int ApartmentBuilding :: getNumFloors(){
return numFloors;
}
void ApartmentBuilding :: setTotalUnits(int unitPerFloor){
unitPerFloor=unitPerFloor;
}
void ApartmentBuilding :: setHasElevator(char hasElevator){
hasElevator=hasElevator;
}
char ApartmentBuilding :: getHasElevator(){
return hasElevator;
}
int ApartmentBuilding :: getTotalUnits(){
return unitPerFloor;
}
void ApartmentBuilding :: setHasCentralAir(char hasCentralAir ){
hasCentralAir=hasCentralAir;
}
char ApartmentBuilding :: getHasCentralAir(){
return hasCentralAir;
}
bool ApartmentBuilding :: isLuxuryBuilding(){
if(hasCentralAir=='y' && hasElevator=='y' && unitPerFloor<=2)
cout<<"Luxury building"<<endl;
return true;
}
int main()
{
ApartmentBuilding apartmentBuilding;
apartmentBuilding.setNumFloors(5);
apartmentBuilding.setTotalUnits(2);
apartmentBuilding.setHasCentralAir('y');
apartmentBuilding.setHasElevator('y');
apartmentBuilding.isLuxuryBuilding();
apartmentBuilding.display();
return 0;
}
output screenshot:
sh-4.2$ g++ -o main *.cpp
sh-4.2$ main
Luxury building
Base class
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.