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

Q1.Which of the following are propositions? (a) The Apple Macintosh is a 256-bit computer. (b) Why...
Q1.Which of the following are propositions? (a) The Apple Macintosh is a 256-bit computer. (b) Why are you here? (c) 8 + 10 = 12 Q2.Construct truth tables for each of the following compound propositions. What do you notice about the results? (a) p∨(¬p⋏q) (b) p∨q Q3.For the following multiple choice question, pick one choice (1. A tautology; 2. A contradiction; 3. Neither) for each compound proposition. p ⋏(¬p)     p ⋏(¬q)     For the following multiple choice question, pick one choice...
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.
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...
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,020,000 $ 1,530,000 Cost of goods sold and operating expenses 765,000 1,147,500 Net operating income $ 255,000 $ 382,500 Average invested assets $ 2,550,000 $ 2,250,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. 1-b. Determine which...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT