Simple Class Problem

I had this program using a struct but now I have to write it using a class and it must contain atleast one Constructor and one Destructor. Ive read both of the classes documentations on here and my book and I just cant seem to grasp the concept. Below is what I have so far and it compiles successfully but it only displays the main function to the screen. I know there has to be more to my class then this, I just dont know what. Does anybody have any ideas?

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
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>

using namespace std;


class menuItemType 
{
public:
    string itemName;
    double itemCost;

private:
	void getData(menuItemType item[], ofstream& outData);
	void showMenu(menuItemType item[], ofstream& outData);
	void printCheck (double& total, double& tax, double& totalF, ofstream& outData);

};


int main()
{

ofstream outData;
outData.open("OutMenu.txt");

cout<< "                Welcome !:)           " << endl;
cout<< "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
cout<< "  *To select an Item from the list below,  " << endl;
cout<< "just indicate the Item number for the item \n\n" << endl;
				

outData << "             Welcome! :)        " << endl;
outData << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
outData << "  *To select an Item from the list below,  " << endl;
outData<< "just indicate the Item number for the item \n\n" << endl;


    menuItemType menu [8];
   /* getData(menu, outData); 
    showMenu(menu, outData);*/
	

    system("pause");

    return 0;
}

void menuItemType :: getData(menuItemType item[], ofstream& outData)
{
    int i=0;
    ifstream inData;
    inData.open("InMenu.txt");
    cout << fixed << setprecision(2);
	outData << fixed << setprecision(2);


    while(!inData.eof()) 
	{
        inData >> item[i].itemName >> item[i].itemCost;
        ++i;
    }

}

void menuItemType :: showMenu(menuItemType item[],ofstream& outData) 
{
	int choice = 0;
    double total = 0.0, totalF = 0.0, tax = 0.0;
    char exit = 'y';
    int j = 1, z = 1, i = 1;

    //the Menu
    for (int i=0; i<8; i++)
	{
        cout << j << ". " << setw(18) << left << item[i].itemName << "$" << setw(10) << item[i].itemCost << endl;
		outData << j << ". " << setw(18) << left << item[i].itemName << "$" << setw(10) << item[i].itemCost << endl;
		j++;
		
    }

    cout << endl;

    while(exit == 'y' || exit == 'Y')
	{

        cout << "Please Enter your Selection or 0 to Exit : ";
        cin >> choice;

        if(cin.fail()) {
            cout << "~~~~~~~~~Invalid selection~~~~~~~~~" << endl;
            cin.clear();
            cin.ignore(1000,'\n');
        } 
		else if (choice==0) {break; }
            else 
			{
                choice--;
                total += (item[choice].itemCost);
                tax = (total * .05);
                totalF = total + tax;
                cout << endl;
				outData << endl;
            }
            cout << endl;
			outData << endl;

            cout << item[choice].itemName << " " << item[choice].itemCost << endl;
            cout << "======================================================" << endl;
            cout << "Do you want to continue (Y/N): ";
			outData << item[choice].itemName << " " << item[choice].itemCost << endl;
            outData << "======================================================" << endl;
            outData << "Do you want to continue (Y/N): ";
            cin >> exit;

        }
	printCheck(total, tax, totalF, outData);
}
void menuItemType ::  printCheck(double& total, double& tax, double& totalF, ofstream& outData)
 {
     cout << "\n\nThanks for Dinning With Us!" << endl;
	 cout << "<><><><><><><><><><><><><><>\n" << endl;
	 cout << "Customer Receipt            " << endl;
	 cout << "SubTotal:               $" << total <<endl;
	 cout << "Tax:                    $" << tax << endl;
	 cout << "                        -----" << endl;
	 cout << "Total:                  $" << totalF << endl;
	 cout << "Please Come Again!" << endl;

	 outData << "\n\nThanks for Dinning With Us!" << endl;
	 outData << "<><><><><><><><><><><><><><>\n" << endl;
	 outData << "Customer Receipt            " << endl;
	 outData << "SubTotal:               $" << total <<endl;
	 outData << "Tax:                    $" << tax << endl;
	 outData << "                        -----" << endl;
	 outData << "Total:                  $" << totalF << endl;
	 outData << "Please Come Again!" << endl;
 }
Last edited on
I haven't had the chance to divulge the code yet, from a quick perusal, i feel perhaps you may want to review your public functions and private members again...not sure if they are where they should be?
Should it not be like this instead?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

{
public:

	void getData(menuItemType item[], ofstream& outData);
	void showMenu(menuItemType item[], ofstream& outData);
	void printCheck (double& total, double& tax, double& totalF, ofstream& outData);

private:

    string itemName;
    double itemCost;

};


Please note if you want a constructor and destructor you will have to create one in public...

Thats exactly what I thought, but on my directions it said all data in the class must be private but if I need to make a public accessor then I can.
Thanks for the reply by the way!
Thats exactly what I thought, but on my directions it said all data in the class must be private but if I need to make a public accessor then I can.

Well, yes. That's the way it should be. But you did exactly the opposite - you made the data members public, and the accessor functions private. Kart fixed that for you.
Ok Ive got that fixed. Would you be able to explain to me how constructors and destructors would work with my program? I sort of understand them by reading the documentation on this site but I am lost as to how they would be used here
Topic archived. No new replies allowed.