Question

In: Computer Science

Given the following inheritance Diagram structure: Fruit: Apple and Orange, Apple: GoldenDelicious and MacIntosh Create a...

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

Solutions

Expert Solution

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();

}


Related Solutions

In cucumbers: Dull fruit (D) is dominant to glossy (d) Orange fruit (R) is dominant to...
In cucumbers: Dull fruit (D) is dominant to glossy (d) Orange fruit (R) is dominant to cream (r) Bitter cotyledons (B) are dominant to nonbitter (b) A cucumber with the genotype ddRrBb was crossed to a cucumber with the genotype DdRrBb using a branch diagram, determine the probability that an offspring from this cross will be dull and cream with nonbitter cotelydons?  (Type in as a decimal and round to 6 decimal digits.)
Create a UML Class Diagram of a Food Supermarket. Show inheritance of common methods. Upload image.
Create a UML Class Diagram of a Food Supermarket. Show inheritance of common methods. Upload image.
Which diagram illustrates multiple inheritance?
Which diagram illustrates multiple inheritance? 
Orange Corp. has two divisions: Fruit and Flower. The following information for the past year is...
Orange Corp. has two divisions: Fruit and Flower. The following information for the past year is available for each division: Fruit Division Flower Division Sales revenue $780,000 $1,170,000 Cost of goods sold and operating expenses 585,000 877,500 Net operating income $195,000 $292,500 Average invested assets $1,950,000 $1,625,000 Orange has established a hurdle rate of 6 percent.    Required: 1-a. Compute each division’s return on investment (ROI) and residual income for last year. (Enter your ROI answers as a percentage rounded to...
Orange Corp. has two divisions: Fruit and Flower. The following information for the past year is...
Orange Corp. has two divisions: Fruit and Flower. The following information for the past year is available for each division: Fruit Division Flower Division Sales revenue $ 660,000 $ 990,000 Cost of goods sold and operating expenses 495,000 742,500 Net operating income $ 165,000 $ 247,500 Average invested assets $ 2,062,500 $ 1,375,000     Orange has established a hurdle rate of 5 percent.    Required: 1-a. Compute each division’s return on investment (ROI) and residual income for last year.(Enter your ROI...
Orange Corp. has two divisions: Fruit and Flower. The following information for the past year is...
Orange Corp. has two divisions: Fruit and Flower. The following information for the past year is available for each division: Fruit Division Flower Division Sales revenue $ 1,560,000 $ 2,340,000 Cost of goods sold and operating expenses 1,170,000 1,755,000 Net operating income $ 390,000 $ 585,000 Average invested assets $ 4,875,000 $ 2,437,500 1-a. Compute each division’s return on investment (ROI) and residual income for last year. (Enter your ROI answers as a percentage rounded to two decimal places, (i.e.,...
Orange Corp. has two divisions: Fruit and Flower. The following information for the past year is...
Orange Corp. has two divisions: Fruit and Flower. The following information for the past year is available for each division: Fruit Division Flower Division Sales revenue $ 1,440,000 $ 2,160,000 Cost of goods sold and operating expenses 1,080,000 1,620,000 Net operating income $ 360,000 $ 540,000 Average invested assets $ 3,000,000 $ 2,160,000     Orange has established a hurdle rate of 8 percent.    Required: 1-a. Compute each division’s return on investment (ROI) and residual income for last year. (Enter your...
Orange Corp. has two divisions: Fruit and Flower. The following information for the past year is...
Orange Corp. has two divisions: Fruit and Flower. The following information for the past year is available for each division: Fruit Division Flower Division Sales revenue $ 1,740,000 $ 2,610,000 Cost of goods sold and operating expenses 1,392,000 1,957,500 Net operating income $ 348,000 $ 652,500 Average invested assets $ 3,480,000 $ 2,718,750     Orange has established a hurdle rate of 6 percent.    Required: 1-a. Compute each division’s return on investment (ROI) and residual income for last year. (Enter your...
Orange Corp. has two divisions: Fruit and Flower. The following information for the past year is...
Orange Corp. has two divisions: Fruit and Flower. The following information for the past year is available for each division: Fruit Division Flower Division Sales revenue $ 1,260,000 $ 1,890,000 Cost of goods sold and operating expenses 945,000 1,323,000 Net operating income $ 315,000 $ 567,000 Average invested assets $ 6,300,000 $ 2,362,500 Orange has established a hurdle rate of 4 percent. Required: 1-a. Compute each division’s return on investment (ROI) and residual income for last year. (Enter your ROI...
Orange Corp. has two divisions: Fruit and Flower. The following information for the past year is...
Orange Corp. has two divisions: Fruit and Flower. The following information for the past year is available for each division: Fruit Division Flower Division Sales revenue $660,000 $990,000 Cost of goods sold and operating expenses 495,000 742,500 Net operating income $165,000 $247,500 Average invested assets $2,062,500 $1,375,000 Orange has established a hurdle rate of 5 percent. Required: 1. Compute each division’s return on investment (ROI) and residual income for last year. 2. Suppose Orange is investing in new technology that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT