In: Computer Science
Given the following inheritance Diagram structure: Fruit: Apple and Orange, Apple: GoldenDelicious and MacIntosh Create a C++ Program that simulate the inheritance structure above. Fruit class should have at least two attributes name and color, getters and setters in addition to a method display() just to display the fruit details. Define the classes Apple, Orange, GoldenDelicious and MacIntosh All of which you can choose the attributes, constructors and methods of your choice, but all should have a redefinition of the method display() In the main create different objects of different classes, assign names and colors and demonstrate the dynamic binding call of the method display using (Polymorphism) GoldenDelicious MacIntosh Fruit Apple Orange
CODE:
#include<iostream>
#include<string>
using namespace std;
class Fruit{
private:
string name;
string color;
public:
Fruit(){
name = "";
color = "";
}
Fruit(string n, string c){
name = n;
color = c;
}
string getColor(){
return color;
}
string getName(){
return name;
}
void setColor(string a){
color = a;
}
void setName(string a){
name = a;
}
virtual void display(){
cout << "This is fruit\n";
}
};
class Orange : public Fruit{
private:
string orangeAttribute;
public:
Orange(string n, string c, string o) : Fruit(n,c){
orangeAttribute = o;
}
string getOrangeAttribute(){
return orangeAttribute;
}
string setOrangeAttribute(string a){
orangeAttribute = a;
}
void display(){
cout << "This is orange\n";
}
};
class GoldenDelicious : public Fruit{
private:
string goldenDeliciousAttribute;
public:
GoldenDelicious(string n, string c, string o) : Fruit(n,c){
goldenDeliciousAttribute = o;
}
string getGoldenDeliciousAttribute(){
return goldenDeliciousAttribute;
}
string setGoldenDeliciousAttribute(string a){
goldenDeliciousAttribute = a;
}
void display(){
cout << "This is golden delicious\n";
}
};
class MacIntosh : public Fruit{
private:
string macIntoshAttribute;
public:
MacIntosh(string n, string c, string o) : Fruit(n,c){
macIntoshAttribute = o;
}
string getMacIntoshAttribute(){
return macIntoshAttribute;
}
string setMacIntoshAttribute(string a){
macIntoshAttribute = a;
}
void display(){
cout << "This is golden macintosh\n";
}
};
class Apple : public Fruit{
private:
string appleAttribute;
public:
Apple(string n, string c, string o) : Fruit(n,c){
appleAttribute = o;
}
string getAppleAttribute(){
return appleAttribute;
}
string setAppleAttribute(string a){
appleAttribute = a;
}
void display(){
cout << "This is apple\n";
}
};
int main(){
Fruit *a,*g,*o,*m;
a = new Apple("name1","red","apple");
g = new GoldenDelicious("name1","golden","golden");
o = new Orange("name1","orange","orange");
m = new MacIntosh("name1","yellow","macintosh");
a->display();
g->display();
o->display();
m->display();
}