Create a class home with following data members, area (int), and cost as protected data member. Create a class (with data members as city, state) derived from this bass class home. Enter the detail for 5 objects (derived) and search with state to display all the homes available.
#include <iostream>
#include <string>
using namespace std;
class Home{
//area (int), and cost as protected data member.
protected:
int area;
float cost;
public:
Home(){
}
//Constructor
Home(int area,float cost){
this->area=area;
this->cost =cost;
}
void setArea(int area){
this->area=area;
}
void setCost(float cost){
this->cost =cost;
}
int getArea(){
return this->area;
}
float getCost(){
return this->cost;
}
};
//a class (with data members as city, state) derived from this bass class home.
class MyHome:public Home {
private:
string city;
string state;
public:
MyHome(){
}
//Constructor
MyHome(int area,float cost,string city,string state):Home(area,cost){
this->city=city;
this->state=state;
}
void setCity(string city){
this->city=city;
}
void setState(string state){
this->state =state;
}
string getCity(){
return this->city;
}
string getState(){
return this->state;
}
};
int main()
{
int area;
float cost;
string city;
string state;
string stateSearch;
const int TOTAL_HOMES=5;
MyHome myHomes[TOTAL_HOMES];
//Enter the detail for 5 objects (derived) and search with state to display all the homes available.
for(int i=0;i<TOTAL_HOMES;i++){
cout<<"Enter home area "<<(i+1)<<": ";
cin>>area;
cout<<"Enter home cost "<<(i+1)<<": ";
cin>>cost;
cin.ignore();
cout<<"Enter home city "<<(i+1)<<": ";
getline(cin,city);
cout<<"Enter home state "<<(i+1)<<": ";
getline(cin,state);
cout<<"\n";
myHomes[i]=MyHome(area,cost,city,state);
}
cout<<"Enter home state to search: ";
bool isFound=false;
getline(cin,stateSearch);
for(int i=0;i<TOTAL_HOMES;i++){
if(myHomes[i].getState()==stateSearch){
cout<<"Home area: "<<myHomes[i].getArea()<<"\n";
cout<<"Home cost: "<<myHomes[i].getCost()<<"\n";
cout<<"Home city: "<<myHomes[i].getCity()<<"\n";
cout<<"Home state: "<<myHomes[i].getState()<<"\n\n";
isFound=true;
}
}
if(!isFound){
cout<<"\nThe homes with the state '"<<stateSearch<<"' do not exist.\n\n";
}
system("pause");
}
Comments
Leave a comment