How to assign value to member variable of BASE class

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.
See if I understand you correctly.

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.

Is this what you are after?
I believe the keyword static is what you're looking for, check out:
http://www.cprogramming.com/tutorial/statickeyword.html
and come back if there's something you don't understand
[edit]Double ninja'd[/edit]

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):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
 
class MyClass
{
    int i;
    static int j;

  public:

    int get_i() {return i;}
    void set_i(int arg) {i = arg;}
    int get_j() {return j;}
    void set_j(int arg) {j = arg;}
};
 
int MyClass::j = 10;
 
int main()
{
    MyClass object_one;
    MyClass object_two;
    object_one.set_i(23);
    object_two.set_i(1);
    
    std::cout<< "Object one's i member: "<< object_one.get_i()<< std::endl;
    std::cout<< "Object two's i member: "<< object_two.get_i()<< std::endl;
    
    std::cout<< "Object one's j member: "<< object_one.get_j()<< std::endl;
    std::cout<< "Object two's j member: "<< object_two.get_j()<< std::endl;
    
    std::cout<< "Setting object two's j member to -9."<< std::endl;
    object_two.set_j(-9);
    
    std::cout<< "Object one's j member: "<< object_one.get_j()<< std::endl;
    std::cout<< "Object two's j member: "<< object_two.get_j()<< std::endl;
    
    return 0;
}
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.
Last edited on
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)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90

#include <iostream>
#include <cmath>
using namespace 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;
    }
    else if (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, const char * 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
Last edited on
bump?
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.

Hope this helps. :)
Topic archived. No new replies allowed.