CSC 143 Vegetable Basket Ch. 9 Inheritance Create an abstract Vegetable Class wi
ID: 3706040 • Letter: C
Question
CSC 143 Vegetable Basket Ch. 9 Inheritance Create an abstract Vegetable Class with two private fields, (double) price per unit & (String) type of unit, (which will be pound, bunch or item). A. Auto-generate getters and setters Create a toString() method that prints price per unit and type of unit. If a Vegetable was $2.25 per unit and the unit was bunch, then the toString() method would return the following string: $2.25 per bunch Create each of the following subclasses with constructors, where the additional field gets added as a parameter before the other two (inherited) parameters when each sub-class object is created. B. Create constructor, getter and setter methods (for new fields) for each Leaf additional field int bunches). To create a Leaf we would call Leaf(2, 2.25, "bunch")] Root (additional field double pounds) [Root(3.5, 1.25, "pound"] Pepper (additional field int items) [Pepper(4, 0.95, "item")] .Explanation / Answer
#include<iostream>
#include<string.h>
#include<stdlib.h>
class Vegetable
{
private:
double price;
char type[10];
public:
virtual void vege_setter(double price_,char type_[])=0;
double* price_getter()
{
return &price;
}
char* type_getter()
{
return type;
}
char* toString()
{
char temp[30]="";
char price_in_string[3];
strcat(temp,"$");
strcat(temp,itoa(price,price_in_string,10));//.........................................
strcat(temp," Per ");
strcat(temp,type);
return temp;
}
};
class Leaf:public Vegetable
{
private:
float qty_;
public:
Leaf(float val,double price_,char type_[])
{
qty_=val;
vege_setter(price_,type_);
}
void vege_setter(double price_,char type_[])
{
double *price_temp=Vegetable::price_getter();
char *type_temp=Vegetable::type_getter();
*price_temp=price_;
strcpy(type_temp,type_);
}
char* toString()
{
char temp_[30];
strcpy(temp_,"");
char val_in_string[3];
strcpy(temp_,"Leaf, ");
itoa(qty_,val_in_string,10);//.........................................
strcat(temp_,val_in_string);
strcat(temp_," @ ");
strcat(temp_,Vegetable::toString());
return temp_;
}
};
class Root:public Vegetable
{
private:
float qty_;
public:
Root(float val,double price_,char type_[])
{
qty_=val;
vege_setter(price_,type_);
}
void vege_setter(double price_,char type_[])
{
double *price_temp=Vegetable::price_getter();
char *type_temp=Vegetable::type_getter();
*price_temp=price_;
strcpy(type_temp,type_);
}
char* toString()
{
char temp_[30];
strcpy(temp_,"");
char val_in_string[3];
strcpy(temp_,"Root, ");
itoa(qty_,val_in_string,10);//.........................................
strcat(temp_,val_in_string);
strcat(temp_," @ ");
strcat(temp_,Vegetable::toString());
return temp_;
}
};
class Pepper:public Vegetable
{
private:
float qty_;
public:
Pepper(float val,double price_,char type_[])
{
qty_=val;
vege_setter(price_,type_);
}
void vege_setter(double price_,char type_[])
{
double *price_temp=Vegetable::price_getter();
char *type_temp=Vegetable::type_getter();
*price_temp=price_;
strcpy(type_temp,type_);
}
char* toString()
{
char temp_[30];
strcpy(temp_,"");
char val_in_string[3];
strcpy(temp_,"Pepper, ");
itoa(qty_,val_in_string,10);//.........................................
strcat(temp_,val_in_string);
strcat(temp_," @ ");
strcat(temp_,Vegetable::toString());
return temp_;
}
};
int main()
{
char *print;
Leaf *leaf=new Leaf(2.5,2.25,"bunch");
print=leaf->toString();
std::cout<<print;
std::cout<<std::endl;
Root *root=new Root(3.5,1.25,"pound");
print=root->toString();
std::cout<<print;
std::cout<<std::endl;
Pepper *pepper=new Pepper(4,0.95,"item");
print=pepper->toString();
std::cout<<print;
return 0;
}