I am writing a program which has a class called Pizza, and it does various things to calculate the cost of a pizza, and outputs the description based on the various toppings/size/style.
I have a member variable that sets the toppings, and i need that I need to be global (not set to a particular object) , but I do not know how to do that. The only thing I know to do is to set it to a particular object, but i need all three objects i created (Hand, Pan, Deep) to have the toppings count.
If you have 1 class called Pizza with a member variable (function? you said sets the toppings) called toppings, and you have 3 objects of this class type.
If you provide a public function to set the toppings value, then your 3 objects will have access to the function and can set the member variable.
So no need for global variables.
If you want to set the same value for all of them, then do it in the constructor.
If I read your description of the problem correctly, it sounds like you are describing a static member variable (that is, it is a member of the class, not the object):
Object one's i member: 23
Object two's i member: 1
Object one's j member: 10
Object two's j member: 10
Setting object two's j member to -9.
Object one's j member: -9
Object two's j member: -9
Notice how j is set to 10 only once (and in a manner similar to how we define functions of a class outside of the body of that class), and that the value that it is set to is the same for both objects that were created of that class? Then, the value is changed again, by only one object, and the value is same for both objects (am I making sense?).
This is because, as I said earlier (and mislabeled in the code), a static variable of a class belong to that class, not an object of the class.
My question was worded incorrectly. I need to pass parameters to a member FUNCTION, not variable, that is not in a particular object. Can you declare static functions?
Please forgive my sloppy code, first time doing this the OOP way (as opposed to procedural)
#include <iostream>
#include <cmath>
usingnamespace std;
class Pizza
{
private:
int pepperoni =0;
int cheese = 0;
int TotalToppings = 0;
public:
int SizePrice =0;
void GetToppings(int); //function that will calculate the price of the toppings
void SetToppings(int, int); //mutator function that will set the amount of toppings
void outputDescription(int,int); // Description of pizza that you are ordering.
void computePrice (); // Function to compute price of said pizza + toppings
void Size (int);
};
/* "GetToppings" definition */
void Pizza::GetToppings(int num)
{
int TopPrice ;
TopPrice= (num *2 );
}
// SetToppings definition
void Pizza::SetToppings(int p, int c)
{
pepperoni = p; // Assigns the number of pepperoni toppings to private member "pepperoni"
cheese = c; // Assigns the number of pepperoni toppings to private member "cheese"
TotalToppings = c+p;
}
// Function to set the price for the size of the pizza
void Pizza::Size(int size)
{
if (size == 1)
{
SizePrice =10;
}
elseif (size ==2)
{
SizePrice = 14;
}
else
SizePrice =17;
}
void Pizza::outputDescription (int size,int type)
{
if (size == 1 && type == 1)
cout << "You have ordered a small, hand tossed pizza with " <<cheese << "cheese toppings and " << pepperoni<< " pepperoni toppings.";
}
int main(int argc, constchar * argv[])
{
int Type; // Holds the number for the type of pizza
int Size;
int Cheese;
int Pepperoni;
Pizza Hand, Deep, Pan;
cout << "Welcome to the Pizza calculation \n";
cout << "What type of pizza do you want? The choices are:\n 1- Hand Tossed\n 2- Deep Dish\n 3- Pan Pizza.\nIndicate the type of pizza by inputting the corresponding number. \n";
cin >> Type;
cout << "Thank you. Now please indicate what size pizza you want.\n 1- Small\n 2- Medium\n 3- Large\nIndicate the size by inputting the corresponding number.\n";
cout << "How many cheese toppings?\n";
cin >> Cheese;
cout << "And finally, how many pepperoni toppings?";
cin >> Pepperoni;
Pizza.SetToppings (Cheese, Pepperoni); // This does not work.
cin >> Size;
if (Type ==1 && Size == 1) {
Hand.outputDescription(1,1);
//Sends 1,1 to outputDescription, which signifies a small hand tossed pizza
}
return 0;
}
This is not done yet btw.
I need to be able to cin the users topping requests and send them to SetToppings without being tied to a particular Pizza object
Line 78 doesn't work because you need to go through an object to call a class function.
It seems to me that Hand, Deep & Pan should be class member variables - an enum?. Same with size info. This is because that information is directly relevant to a Pizza, and so it should exist inside the class.
Getting the user input should also be in a class function for the same reason. The only thing main() should do is create objects & call their functions.