Create a C++ class called Candle that has fields for color, height, and price. W
ID: 3694318 • Letter: C
Question
Create a C++ class called Candle that has fields for color, height, and price. Write accessor functions for all three fields. Create mutator functions for the color and height, but have the height also set the price based on $2 per inch.
Create a child class called Scented candle that contains an additional field called scent and an accessor and a mutator function for this field. Write a setHeight function that calculates the price based on $3.00 per inch.
Write a main function that creates both a parent object and a child object. Ask the user to enter the information and then display all information about all fields.
Explanation / Answer
class Candle
{
public:
virtual string getcolor();
virtual double getheight();
virtual void setcolor(const string col);
virtual void setheight(constg string heig);
virtual void setprice();
private:
string color;
double height, price;
};
class Scented_candle: public Candle
{
public:
string getscent();
void setscent();
private:
string scent;
};
string Scented_candle:getcolor()
{
return color;
}
string Scented_candle:getheight()
{
return height;
}
void Scented_candle:setcolor(constg string clr)
{
color = clr;
return;
}
void Scented_candle:setheight(constg string heigh)
{
height = heigh;
return;
}
string Scented_candle:getscent()
{
return scent;
}
void Scented_candle:setscent(constg string scen)
{
scent = scen;
return;
}
void Scented_candle:settprice()
{
price = height * 2;
return;
}
int main()
{
Scented_candle *c = new Scented_candle;
string color, scent,ch;
double height;
cout<<" Enter candle color:";
cin>> color;
cout<<" Enter height in inches:";
cin>> height;
c->setcolor(color);
c->setheight(height);
c->setprice();
cout<<" You want to enter scent(if yes->y or no->n:";
cin>>ch;
switch(ch){
case 'y' or 'Y':
cout<< " Enter Scent:";
cin>>scent;
c->setscent(scent);
void setHeight()
{
cout<<"Enter New height:";
cin >> height;
c->price = height *3;
}
break;
case 'n' or 'N':
break;
default: exit();
}
cout<<" Candle color:" << c->color;
cout<<" Candle height:" << c->height;
cout<<" Candle Price:" << c->price;
}