Design and implement your own simple class to represent any household item of yo
ID: 3571781 • Letter: D
Question
Design and implement your own simple class to represent any household item of your choice (toaster, fan, hair dryer, piano ...) Your class should have a constructor, one additional method and at least one member variable (e.g. boolean isOn to turn the item on or off). Be sure you demonstrate your class works properly by constructing an instance of it and calling your method.
THE ITEM LISTED BELOW IS INCORRECT! PLEASE DO NOT POST IT AS AN ANSWER
#include <bits/stdc++.h>
using namespace std;
class HouseItem
{
string item;
bool isOn;
public:
HouseItem()//constructor
{
item="";
isOn=false;
}
void onItem()//Set on item
{
isOn=true;
}
void setNameofItem(string s)//set name of item
{
item=s;
}
string getNameofItem()//return name of item
{
return item;
}
string status()//return status of item
{
if(isOn)
{
return "ON";
}
return "OFF";
}
};
int main(int argc, char const *argv[])
{
HouseItem obj;
string name;
cout<<"Enter name of item you want ";
cin>>name;
obj.setNameofItem(name);
cout<<"Your household item is "<<name<<endl;
char ch;
cout<<"Do you want to turn ON "<<obj.getNameofItem()<<" y/n ";
cin>>ch;
if(ch=='y')
{
obj.onItem();//On item
}
cout<<"Status of your item "<<obj.getNameofItem() <<" is "<<obj.status()<<endl;
return 0;
}
============================================================
Output:
akshay@akshay-Inspiron-3537:~/Chegg$ g++ House.cpp
akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out
Enter name of item you want
FAN
Your household item is FAN
Do you want to turn ON FAN y/n
y
Status of your item FAN is ON
Explanation / Answer
#include <iostream>
using namespace std;
class Fan
{
string brand;
bool isOn;
public:
Fan() //default constructor
{
this->brand="";
this->isOn=false;
}
void setOn() //Set on item
{
isOn= true;
}
void setOff()
{
isOn = false;
}
void setBrand(string brand) //set brand of fan
{
this->brand = brand;
}
string getBrand() //return name of item
{
return brand;
}
string getStatus() //return status of item
{
if(isOn)
return "on";
else
return "off";
}
};
int main()
{
Fan f1;
string brand;
cout<<"Enter name of brand of fan you want ";
cin>>brand;
f1.setBrand(brand);
cout<<"Your Fan's Brand is "<<f1.getBrand()<<endl;
char ch;
cout<<"Do you want to turn ON ?";
cin>>ch;
if(ch=='y' || ch == 'Y')
{
f1.setOn(); //switch on the fan
}
cout<<" Your Fan of brand "<<f1.getBrand() <<" is "<<f1.getStatus()<<endl;
return 0;
}
output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.