In: Computer Science
I. Create the class Item with the following members:
1. id, a protected variable of type int
2. name, a protected variable of type string
3. price, a protected variable of type double
4. a public non-default constructor which accepts three
parameters to initialize the variables above
II. Create the class Chair. The class publicly inherits from Item
and has the following members
1. numLegs, a private variable of type int
2. a non-default constructor to initialize numLegs, id,
name, and price
3. accessors and mutators for numLegs
4. an int conversion constructor to convert from an int
to a type of class Chair.
5. a string conversion operator which returns a string
consisting of the values of id, name, price, and int with spaces
between the values
6. overloaded output stream operator
7. overloaded input stream operator
III. Write test code to create an instance of type Chair and test
all members
//C++ program
#include<iostream>
#include <sstream>
#include <string>
using namespace std;
class Item{
protected:
int id;
string name;
double price;
public:
Item(int i ,string n , double
p){
id = i;
name = n;
price=p;
}
};
class chair:public Item{
private:
int numLegs;
public:
chair(int i , string n , double
p,int leg):Item(i,n,p){
numLegs =
leg;
}
void setNumLegs(int leg){
numLegs =
leg;
}
int getNumLegs(){
return
numLegs;
}
string tostring(){
ostringstream
str1,str2,str3;
str1 << id;
str2 << price;
str3 << numLegs;
return
str1.str()+" "+name+" "+str2.str()+" "+str3.str()+"\n";
}
friend ostream & operator
<< (ostream & out , const chair &c){
out<<c.id<<" "<<c.name<<"
"<<c.price<<" "<<c.numLegs<<"\n";
return
out;
}
friend istream & operator
>> (istream & in , chair c){
cout<<"Enter id : ";
in>>c.id;
cout<<"Enter name : ";
in>>c.name;
cout<<"Enter price : ";
in>>c.price;
cout<<"Enter numLegs : ";
in>>c.numLegs;
return in;
}
};
int main(){
chair c (10,"Hello world",100.67,4);
cout<<c.tostring();
cout<<c;
}
//sample output