Question: QUESTION 3 - WALKTHROUGH - (20 MARKS): What is the exact output of the following program? Show your rough work to avoid deductions. #include using

QUESTION 3 - WALKTHROUGH - (20 MARKS): What is the exact output of the following program? Show your rough work to avoid deductions. #include using namespace std; class Fruit { int items; double price; public: Fruit(); Fruit(int, double); Fruit(const Fruit&); void to(Fruit&); void operator+=(double); friend ostream& operator<<(ostream&, const Fruit&); virtual ~Fruit(); }; Fruit::Fruit() { items = 0; price = 0.0; cout << '#'; } Fruit::Fruit(int i, double p) { items = i; price = p; cout << '!'; } Fruit::Fruit(const Fruit& f) { items = f.items; price = f.price; cout << '$'; } void Fruit::to(Fruit& dest) { if (items > 0) { dest.items++; items--; } } void Fruit::operator+=(double change) { price += change; } Fruit::~Fruit() { cout << '~' << *this << endl; } ostream& operator<<(ostream& os, const Fruit& s) { os << s.items << '-' << s.price; return os; } class OldFruit : public Fruit { public: OldFruit (); OldFruit (int, double); OldFruit (const OldFruit&); friend ostream& operator<<(ostream&, const OldFruit&); ~OldFruit(); }; OldFruit::OldFruit () { cout << '&'; } OldFruit::OldFruit (int i, double p) : Fruit(i, p) { cout << ':'; } OldFruit::OldFruit(const OldFruit& f) : Fruit(f) { cout << '*' << endl; } OldFruit::~OldFruit() { cout << '%' << *this << endl; } ostream& operator<<(ostream& os, const OldFruit& s) { os << (Fruit&)s; // calls Fruit version of << operator os << " reduced "; return os; } OldFruit reduce(Fruit& fruit, int n, double p) { OldFruit old; for (int i = 0; i < n; i++) fruit.to(old); old += p; return old; } int main() { Fruit apples(5, 2.00), pears; cout << endl; cout << '=' << apples << endl; OldFruit oldApps = reduce(apples, 2, 1.00); cout << '=' << oldApps << endl; return 0; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!