Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write the following code, run it, fix it and describe what it does. // ConsoleApplication3FactoryPizzaExample.cpp : This file contains the 'main' function. Program execution begins

Write the following code, run it, fix it and describe what it does.

// ConsoleApplication3FactoryPizzaExample.cpp : This file contains the 'main' function. Program execution begins and ends there. // // Factory Pattern // * Add code for a 4th pizza type: Cheese // * Modify the pizza_information function, so it also prints the pizza name // * Comment each function - Explaining what it does // * Explain why this is a factory pattern

#include using namespace std;

#include #include #include using namespace std;

class Pizza { public: virtual double getPrice() const = 0; virtual ~Pizza() { cout << "Destructor called" << endl; } };

class PepperoniOlivePizza : public Pizza { public: virtual double getPrice() const { return 8.50; }; virtual ~PepperoniOlivePizza() {}; };

class DeluxeChickenPizza : public Pizza { public: virtual double getPrice() const { return 10.50; }; virtual ~DeluxeChickenPizza() {}; };

class HawaiianPizza : public Pizza { public: virtual double getPrice() const { return 11.50; }; virtual ~HawaiianPizza() {}; };

class PizzaFactory { public: enum PizzaType { PepperoniOlive, DeluxeChicken, Hawaiian };

static unique_ptr createPizza(PizzaType pizzaType) { switch (pizzaType) { case PepperoniOlive: return make_unique(); case DeluxeChicken: return make_unique(); case Hawaiian: return make_unique(); } throw "invalid pizza type."; } };

// Instantiate all available pizzas and print their prices, and names void pizza_information(PizzaFactory::PizzaType pizzatype) { unique_ptr pizza = PizzaFactory::createPizza(pizzatype); cout << "Price of " << pizzatype << " is " << pizza->getPrice() << std::endl; // Currently this prints the enum #, and price // Fix this code so it prints the pizza name, not the enum #, and prints the price }

int main() { pizza_information(PizzaFactory::PepperoniOlive); pizza_information(PizzaFactory::DeluxeChicken); pizza_information(PizzaFactory::Hawaiian); }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions