Answer to Question #221824 in C++ for mansu

Question #221824

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.


1
Expert's answer
2021-07-31T07:24:27-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS