First class program, how to implement design ideas?

I'm now learning about classes, and after doing this exercise, which has one class using another class, I thought I would build the program up with more options.

Here is the initial exercise:

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
// Pizza-using-Circle-class.cpp
// date: Sun Mar 20 2016
// Create a Pizza class that uses a Circle class.
#include <iostream>
#include <cmath>    // in order to use pow(x, 2) function
using namespace std;

class Circle
{
private:
    double radius;

public:
    void setRadius(double r)
    { radius = r; }
    double getArea()
    { return 3.14 * pow(radius, 2); }
};

class Pizza
{
private:
    double price;
    string type;
    Circle size;

public:
    void setName(string t)
    { type = t; }
    string getName()
    { return type; }
    void setPrice(double p)
    { price = p; }
    void setSize(double r)
    { size.setRadius(r); }
    double costPerSqin()
    { return (price + size.getArea()); }
};

int main()
{

    Pizza myPizza;
    myPizza.setPrice(0.99);
    myPizza.setSize(2.5);
    cout << endl << "Price per square inch $" << myPizza.costPerSqin();
    cout << endl;
	return 0;
}


Here is the output:



Price per square inch $20.615



From there I built up this program, which I later separated into 5 files, and got those working as well:

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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Pizza-using-Circle-class-menu-if-stmt.cpp
// date: Sun Mar 20 2016
// Create a Pizza class that uses a Circle class.
// MOD: build up this program using function prototypes and place
//       functions outside the class.  Add a menu and user select items.

#include <iostream>
#include <cmath>                    // in order to use pow(x, 2)
#include <iomanip>                  // in order to set dollar .00 placement
using namespace std;

// Circle class declaration
class Circle
{
private:
    double radius;

public:
    Circle();                   // Constructor prototype
    ~Circle();                  // Destructor prototype
    void setRadius(double r);
    double getArea();
}; // end Circle class

class Pizza
{
private:
    double price;
    string type;
    Circle size;

public:
    Pizza();                        // Constructor prototype
    ~Pizza();                       // Destructor prototype
    void setName(string t);
    string getName();
    void setPrice(double p);
    void setSize(double r);
    double costPerSqin();
}; // end Pizza class

Circle::Circle()
{
    radius = 0.0;
}

Circle::~Circle()
{
    ;
}

void Circle::setRadius(double r)
{
    radius = r;
}

double Circle::getArea()
{
    return 3.14 * pow(radius, 2);
}

Pizza::Pizza()
{
    price = 0.0;
    type = "";
}

Pizza::~Pizza()
{
    ;
}

void Pizza::setName(string t)
{
    type = t;
}

string Pizza::getName()
{
    return type;
}

void Pizza::setPrice(double p)
{
    price = p;
}

void Pizza::setSize(double r)
{
    size.setRadius(r);
}

double Pizza::costPerSqin()
{
    return (price + size.getArea());
}

int main()
{
    string pizzaName    = "";
    double smallSize    = 3.0;  // slice wide end size = 3"
    double smallPrice   = 1.99; // price per slice
    double mediumSize   = 3.25;
    double mediumPrice  = 2.50;
    double largeSize    = 3.5;
    double largePrice   = 2.99;
    double xLargeSize   = 4.0;
    double xLargePrice  = 3.99;
    double xxLargeSize  = 4.5;
    double xxLargePrice = 4.99;

    Pizza newPizza;             // instantiate newPizza from Pizza class

    cout << endl;
    cout << "What size pizza do you want: " << endl;
    cout << endl;
    cout << "small"   << endl;
    cout << "medium"  << endl;
    cout << "large"   << endl;
    cout << "xlarge"  << endl;
    cout << "xxlarge" << endl;
    cout << endl;
    cout << "enter:  ";
    cin >> pizzaName;

    if (pizzaName == "small")
    {
        newPizza.setName(pizzaName);
        newPizza.setPrice(smallSize);
        newPizza.setSize(smallPrice);
    }
    else if (pizzaName == "medium")
    {
        newPizza.setName(pizzaName);
        newPizza.setPrice(mediumSize);
        newPizza.setSize(mediumPrice);
    }
    else if (pizzaName == "large")
    {
        newPizza.setName(pizzaName);
        newPizza.setPrice(largeSize);
        newPizza.setSize(largePrice);
    }
    else if (pizzaName == "xlarge")
    {
        newPizza.setName(pizzaName);
        newPizza.setPrice(xLargeSize);
        newPizza.setSize(xLargePrice);
    }
    else if (pizzaName == "xxlarge")
    {
        newPizza.setName(pizzaName);
        newPizza.setPrice(xxLargeSize);
        newPizza.setSize(xxLargePrice);
    }
    else
    {
        cout << endl << "ERROR!! " << pizzaName << " is not a valid entry.  Exiting program." << endl;
        return 1;
    } // end if

    cout << endl;
    cout << fixed << showpoint << setprecision(2) << endl;
    cout << "You ordered a " << newPizza.getName() << " pizza which will cost you $"
         << newPizza.costPerSqin() << "." << endl;
    cout << endl;
	return 0;
}




What size pizza do you want:

small
medium
large
xlarge
xxlarge

enter:  small

You ordered a small pizza which will cost you $15.43.



Now, I thought it might be a good idea that instead of outlining all these variables separately, that I'd look at arrays. So I built a separate program to test that:

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
// MOD2: place the pizza size names, slice sizes and cost in 3 arrays.
#include <iostream>
#include <iomanip>
using namespace std;

void setArray(string pizzaName[], double sliceSize[], double slicePrice[], int size);
void getChoice(string pizzaName[], double slicePrice[]);

int main()
{
    const int NUM_SIZE = 5;
    string pizzaName[NUM_SIZE]  = {"small", "medium", "large", "xlarge", "xxlarge"};
    double sliceSize[NUM_SIZE]  = {3.0, 3.25, 3.5, 4.0, 4.5};
    double slicePrice[NUM_SIZE] = {1.99, 2.50, 2.99, 3.99, 4.99};

    setArray(pizzaName, sliceSize, slicePrice, NUM_SIZE);
    getChoice(pizzaName, slicePrice);

    return 0;
}

void setArray(string pizzaName[], double sliceSize[], double slicePrice[], int size)
{
    for (int count = 0; count < size; count++)
        cout << setw(7) << left << "Choice: "
             << setw(7) << pizzaName[count] << " - "
             << setw(4) << sliceSize[count] << " - "
             << setw(7) << slicePrice[count] << endl;
    cout << endl;
}

void getChoice(string pizzaName[], double slicePrice[])
{
    // initialize choice variable for user input
    int choice = 0;

    cout << "Enter choice number for the size of the pizza you want:" << endl;
    cout << endl;
    cout << "small   - 1: " << endl;
    cout << "medium  - 2: " << endl;
    cout << "large   - 3: " << endl;
    cout << "xlarge  - 4: " << endl;
    cout << "xxlarge - 5: " << endl;
    cout << endl << " >> ";
    cin >> choice;

switch(choice)
{
        case 1:     cout << fixed << showpoint << setprecision(2) << endl;
                    cout << "You ordered a " << pizzaName[choice-1] << " pizza which will cost you $"
                         << slicePrice[choice-1] << "." << endl;
                    break;
        case 2:     cout << fixed << showpoint << setprecision(2) << endl;
                    cout << "You ordered a " << pizzaName[choice-1] << " pizza which will cost you $"
                         << slicePrice[choice-1] << "." << endl;
                    break;
        case 3:     cout << fixed << showpoint << setprecision(2) << endl;
                    cout << "You ordered a " << pizzaName[choice-1] << " pizza which will cost you $"
                         << slicePrice[choice-1] << "." << endl;
                    break;
        case 4:     cout << fixed << showpoint << setprecision(2) << endl;
                    cout << "You ordered a " << pizzaName[choice-1] << " pizza which will cost you $"
                         << slicePrice[choice-1] << "." << endl;
                    break;
        case 5:     cout << fixed << showpoint << setprecision(2) << endl;
                    cout << "You ordered a " << pizzaName[choice-1] << " pizza which will cost you $"
                         << slicePrice[choice-1] << "." << endl;
                    break;
        default:    cout << endl << "ERROR!! " << pizzaName[choice-1] 
                     << " is not a valid entry.  Exiting program." << endl;
    }
}


What I don't know how to do at this point is figure out how to combine this last program with arrays into the Pizza-Circle class to make it work like objects. I even thought today I could build it using a struct:

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
// MOD 3: using a struct.

#include <iostream>
#include <iomanip>
using namespace std;

const int ARRAY_SIZE = 5;

struct Pizza
{
	string name[ARRAY_SIZE];
	double sliceSize[ARRAY_SIZE];
	double slicePrice[ARRAY_SIZE];
};

int main()
{
	Pizza order;

	order = { {"small", 3.00, 1.99},
              	  {"medium", 3.50, 2.50},
                  {"large", 4.00, 2.99},
                  {"xlarge", 4.50, 3.99},
                  {"xxlarge", 5.00, 4.99} };

    cout << endl;
    cout << order << endl;

    cout << endl;
    return 0;
}


But it doesn't work, and I don't know how to build it out as a struct or array to combine it with the Pizza-Circle class. Any suggestions would be welcome, or other design ideas that would work more efficiently.
Last edited on
Topic archived. No new replies allowed.