OOP Program: This is my code so far, but it does not give the correct output: Te
ID: 3770459 • Letter: O
Question
OOP Program:
This is my code so far, but it does not give the correct output:
TestPizza.cpp:
#include "stdafx.h"
#include "Pizza.h"
#include "Pizza.cpp"
#include <iostream>
#include <string>
using namespace std;
int _tmain(int argc, _TCHAR* argv[]){
Pizza p1("thin", "cheese", 18, 5);
p1.smell("good");
p1.taste("delicious");
p1.display();
cout << " ";
Pizza p2("thick", "pepperoni", 12, 3);
p2.smell("bad");
p2.taste("horrible");
p2.display();
cout << " ";
system("PAUSE");
return 0;
}
Pizza.cpp:
#include "Pizza.h"
#include <iostream>
#include <string>
using namespace std;
Pizza::Pizza(){
}
Pizza::Pizza(string pCrust, string pTopping, double pDiameter, double pServes){
crust = pCrust;
topping = pTopping;
diameter = pDiameter;
serves = pServes;
}
void Pizza::taste(string pTaste){
cout << "The pizza tastes: " << taste << endl;
}
void Pizza::smell(string pSmell) {
cout << "The pizza smells: " << smell << endl;
}
void Pizza::display() {
cout << "The pizza crust is: " << crust << endl;
cout << "The pizza topping is: " << topping << endl;
cout << "The pizza diameter is: " << diameter << endl;
cout << "The pizza serves: " << serves << " people" << endl;
}
Pizza.h:
#include "stdafx.h"
#include "Pizza.cpp"
#include <iostream>
#include <string>
using namespace std;
class Pizza{
public:
string crust;
string topping;
double diameter;
double serves;
Pizza();
Pizza(string pCrust, string pTopping, double pDiameter, double pServes);
void taste(string pTaste);
void smell(string pSmell);
void display();
};
I think most of the code is there, but I misplaced some code within the different header and source files as this was my first time working with split code.
Explanation / Answer
Pizza.h
#ifndef PIZZA_H
#define PIZZA_H
class Pizza
{
private:
int pizzaSize;
int pizzaTop;
public:
Pizza(); // Constructor
void setPizzaSize(int);
void setPizzaTop(int);
int getPizzaSize() const;
int getPizzaTop() const;
void reset(int, int);
};
#endif
Pizza.cpp
#include "Pizza.h"
#include <iostream>
using namespace std;
// Construtor
Pizza::Pizza()
{
pizzaSize = 10;
pizzaTop = 1;
}
void Pizza::setPizzaSize(int p)
{pizzaSize = p;}
// setPizzaTop sets topping default value of ordered pizza
void Pizza::setPizzaTop(int t)
{pizzaTop = t;}
// getPizzaSize returns value in the member variable pizzaSize
int Pizza::getPizzaSize() const
{return pizzaSize;}
// getPizzaTop returns value in the member variable pizzaTop
int Pizza::getPizzaTop() const
{return pizzaTop;}
// reset sets default values of pizzaSize and pizzaTop member variables
void Pizza::reset()
{return Pizza;}
"Pizza_Program.cpp"
#include "Pizza.h"
#include <iostream>
#include <iomanip>
using namespace std;
// Function prototypes
void displayMenu();
void size(Pizza &);
void topping(Pizza &);
void reset();
int main()
{
Pizza dinner; // Instance of pizza class object
char choice; // menu selection
char subChoice; // sub-menu selection in select case 'C'
cout << fixed << showpoint << setprecision(2);
do
{
displayMenu();
cin >> choice;
while (toupper(choice) < 'A' || toupper(choice) > 'E')
{
cout << "Please make a choice from the menu below (A-E): ";
cin >> choice;
}
// Process user choice
switch(choice)
{
case 'a':
case 'A': size(dinner);
break;
case 'b':
case 'B': topping(dinner);
break;
case 'c':
case 'C': cout << "You chose a " << dinner.getPizzaSize() << " inch pizza with" << dinner.getPizzaTop() << " ";
cout << "Choose either (R) to restart or (E) to exit.";
cin >> subChoice;
if (toupper(subChoice) != 'R' || toupper(subChoice) != 'E')
{cout << "Please enter R or D: ";
cin >> subChoice;
}
else
break;
case 'd':
case 'D': reset();
size(dinner);
break;
}
}
while (toupper(choice) != 'E');
return 0;
}
void displayMenu()
{
cout << " MENU ";
cout << "A) What size of pizza do you want? ";
cout << "B) What type of toppings do you wants? ";
cout << "C) Display/Place current order. ";
cout << "D) Cancel current order. ";
cout << "E) Quit Program ";
cout << "Enter your choice: ";
}
void size(Pizza &dinner)
{
int size;
cout << "Enter either (10,16,20,24) for the size pizza you would like: ";
cin >> size;
if (size != 10 && size != 16 && size != 20 && size != 24)
{cout << "Please enter 10,16,20,24 for size of pizza!";
cin >> size;
}
else
dinner.setPizzaSize(size);
}
void topping(Pizza &dinner)
{
int topping;
cout << " Toppings Choices ";
cout << " 1) pepperoni ";
cout << " 2) sausage ";
cout << " 3) vegetarian ";
cout << " 4) deluxe ";
cout << " 5) plain cheese ";
cin >> topping;
if (topping < 1 || topping > 5)
{cout << "Please enter a valid topping:";
cin >> topping;
}
dinner.setPizzaTop(topping);
}
void reset()
{
dinner.reset();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.