Create a class vegetable which has two functions color() and wh_grow(), create classes spanich,potato,
onion, tomato that inherits vegetable class and also redefines color() and wh_grow() method to display
color of each vegetable and where they grow. Use dynamic memory allocation to create objects and use
the methods.
#include <iostream>
#include <string>
using namespace std;
class vegetable {
public:
virtual void color(){
}
virtual void wh_grow(){
}
};
class spanich:public vegetable{
public:
void color(){
cout<<"Spanich color is green\n";
}
void wh_grow(){
cout<<"Spanich grows\n";
}
};
class potato:public vegetable{
public:
void color(){
cout<<"Potato color is brown\n";
}
void wh_grow(){
cout<<"Potato grows\n";
}
};
class onion:public vegetable{
public:
void color(){
cout<<"Onion color is pink\n";
}
void wh_grow(){
cout<<"Onion grows\n";
}
};
class tomato:public vegetable{
public:
void color(){
cout<<"Tomato color is red\n";
}
void wh_grow(){
cout<<"Tomato grows\n";
}
};
int main(){
vegetable** vegetables=new vegetable*[4];
vegetables[0]=new spanich();
vegetables[1]=new potato();
vegetables[2]=new onion();
vegetables[3]=new tomato();
for(int i = 0; i < 4; i++){
vegetables[i]->color();
vegetables[i]->wh_grow();
cout<<"\n";
}
for(int i = 0; i < 4; i++){
delete vegetables[i];
}
delete[] vegetables;
int i;
cin>>i;
return 0;
}
Comments
Leave a comment