In: Computer Science
Code in C++ programming language description about chose favorite fruit and wage color of fruit in lesson Class object (Multiple class).
#include
#include
#include
using namespace std;
enum Color{RED=0,BLUE,GREEN,YELLOW};
string colors[]={"RED","BLUE","GREEN","YELLOW"};
class fruit{
protected:
string name;
enum Color color;
public:
fruit(string name,Color clr)
:name(name), color(clr){}
void input(){
int cl;
cout<<" Enter name fruit: ";cin.sync();getline(cin,name);
cout<<" 0.RED\n 1.BLUE\n 2.GREEN\n 3.YELLOW"< cout<<" Enter color of fruit: "; cin>>cl; if(cl==0) color=RED; else if(cl==1) color=BLUE; else if(cl==2) color=GREEN; else if(cl==3) color=YELLOW; else color=GREEN; } void printResult(){ cout< } }; class apple:public fruit{ protected: float kilo; public: apple(string name, Color color,float kg) :fruit(name,color),kilo(kg){} void setKilo_ap(){ cout<<" Input kilo: ";cin>>kilo; } void printKilo(){ fruit::printResult(); cout<<"Kilo = "< } }; class banana: public fruit{ protected: float kilo; public: banana(string name,Color color) :fruit(name,color){} void print(){ fruit::printResult(); fruit::input(); } void setKilo_ba(){ cout<<" Input kilo: ";cin>>kilo; } void printKilo(){ cout<<"Kilo = "< } }; int main(){ apple ap("Apple",RED,1.25); banana ba("Banana",YELLOW);ap.printKilo(); ba.printResult();ba.printKilo(); cout< ap.input();cin.sync();ap.setKilo_ap(); ba.input();ba.setKilo_ba(); cout< ap.printKilo(); ba.printResult();ba.printKilo(); cout< }