Changing a struct to a class, using public and private variables.

So basically I worked really hard on this code in my CS150 class and now I need to change it to a class, and I thought OK that's simple enough, but I cant seem to figure it out at all! I know I can just make all of the variables inside the class public, but that's not what my professor wants. I am supposed to implement the private variables. I tried making the "letter" and "letterCount" variables private and I created some void functions under the public part, I think I might have been on the right track with that but I wasn't sure how to modify the rest of my code in order to get it to run. Here's the original code, still in the struct format. Any help would be GREATLY appreciated.

P.S. my professor said "make the member variables private, and implement functions to manage those private variables, such as getter and setter functions." so that might help you understand whats expected. Thanks!

I NEED TO GET RID OF THE STRUCT ALLTOGETHER!

my class should be set up like this:

class
{
private:
letter
letterCount

public:
//functions to control the private variables such as...
void getletter
void getletterCount
}

I just don't know how to implement that in my code, and what to write in those functions in the class. Also I don't know what else needs to be changed in the rest of my code to use the functions inside the class. Thanks for your help!

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

using namespace std;

//Struct definition
struct letterType
{
char letter;
int letterCount;
};

//Function to open I/O files
void openFile(ifstream& inFile, ofstream& outFile);

//Function to fill the array of structs and keep track of the amount of uppercase/lowercase letters
void count(ifstream& inFile, letterType letterList[], int& totalBig, int& totalSmall);

//Function to print the letters, the occurences, and the percentages
void printResult(ofstream& outFile, letterType letterList[], int totalBig, int totalSmall);
//Function to create an output file that contains programmer info
void Info (ofstream& outputFile);

int main()
{
ofstream fout;
ifstream input; //object to read the text
ofstream output; //object to write the results
int totalCapital = 0; //variable to store the total number of uppercase
int totalLower = 0; //variable to store the total number of lowercase
letterType letterObj[52]; //array of structs of type letterType to hold the information

//Input and process data
Info (fout);
openFile(input, output);
count(input, letterObj, totalCapital, totalLower);
printResult(output, letterObj, totalCapital, totalLower);

//Close files
input.close();
output.close();

return 0;
}

void openFile(ifstream& inFile, ofstream& outFile)
{
string inFileName;
string outFileName;

cout << "Enter the name of the input file: ";
cin >> inFileName;
inFile.open(inFileName.c_str());
cout << endl;

cout << "Enter the name of the output file: ";
cin >> outFileName;
outFile.open(outFileName.c_str());
cout << endl;
}

void count(ifstream& inFile, letterType letterList[], int& totalBig, int& totalSmall)
{
char ch;

//Loop to initialize the array of structs; set letterCount to zero
for(int index = 0; index < 26; index++)
{
//This segment sets the uppercase letters
letterList[index].letter = static_cast<char>(65 + index);
letterList[index].letterCount = 0;

//This segment sets the lowercase letters
letterList[index + 26].letter = static_cast<char>(97 + index);
letterList[index + 26].letterCount = 0;
}

//read first character
inFile >> ch;

//Keep reading until end of file is reached
while(!inFile.eof())
{
//If uppercase letter or lowercase letter is found, update data
if('A' <= ch && ch <= 'Z')
{
letterList[static_cast<int>(ch) - 65].letterCount++;
totalBig++;
}
else if('a' <= ch && ch <= 'z')
{
letterList[static_cast<int>(ch) - 71].letterCount++;
totalSmall++;
}

//read the next character
inFile >> ch;

} //end while
} //end function

void printResult(ofstream& outFile, letterType letterList[], int totalBig, int totalSmall)
{
outFile << fixed << showpoint << setprecision(2);
outFile << "Letter Occurences Percentage" << endl;

for(int index = 0; index < 52; index++)
{
if(0 <= index && index <= 25)
{

outFile << setw(4) << letterList[index].letter << setw(12) << letterList[index].letterCount << " ";
outFile << setw(15)<< 100 * (letterList[index].letterCount / (static_cast<float>(totalBig)+static_cast<float>(totalSmall))) << "%" << endl;
}
else
{
outFile << setw(4) << letterList[index].letter << setw(12) << letterList[index].letterCount << " ";
outFile << setw(15) << 100 * (letterList[index].letterCount / (static_cast<float>(totalBig)+static_cast<float>(totalSmall))) << "%" << endl;
}
} //end for
} //end

void Info (ofstream& outputFile)
{

string outputFileName;


cout << "Enter your name: ";
cin >> outputFileName;
outputFileName = outputFileName+"_p4.txt";
outputFile.open(outputFileName.c_str());

outputFile << "Programmer name: ";
outputFile << "\nThis program is a collection of variable declarations";
outputFile << "\nand function calls. This program reads a text and outputs";
outputFile << "\nthe letters, together with their counts, as explained below";
outputFile << "\nin the function printResult.";
outputFile << "\n\nThe program consists of the following four functions:\n";
outputFile << "\n**Function openFile: Opens the input and output files. You must pass the";
outputFile << "\nfile streams as parameters (by reference, of course). If the file does not";
outputFile << "\nexist, the program should print an appropriate message and exit. The";
outputFile << "\nprogram must ask the user for the names of the input and output files.";

outputFile << "\n\n**Function count: counts every occurrence of capital letters A-Z and";
outputFile << "\nsmall letters a-z in the text file opened in the function openFile. This";
outputFile << "\ninformation must go into an array of structures. The array must be passed";
outputFile << "\nas a parameter, and the file identifier must also be passed as a parameter.";

outputFile << "\n\n**Function printResult: Prints the number of capital letters and small";
outputFile << "\nletters, as well as the percentage of capital letters for every letter A-Z and";
outputFile << "\nthe percentage of small letters for every letter a-z. The percentages";
outputFile << "\nshould look like this: ‘‘25%’’. This information must come from an array";
outputFile << "\nof structures, and this array must be passed as a parameter.";

outputFile << "\n\n**Function Info: Creates an output file named 'yourName_P4.txt'";
outputFile << "\nthat contains all your required programmer info, along with";
outputFile << "\nTA name,..) a description of the program ( what it does, and how),";
outputFile << "\nand how you approached solving it. You must include your algorithm that";
outputFile << "\nyou wrote, including the steps and any refinements made.";
outputFile << "\nFor Project 5, create an output file named 'yourName_P5.txt.'";

outputFile << "\n\n\n\n\n";
cout << endl;
}
Struct is a class where variables are public by default, while in a class, variables are private by default. A struct is basically a class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class object
{
   public:
    object()
    {
       std::cout<<"This is a class";
    }
}

struct object2
{
    public:
     object2()
      {
       std::cout<<"This is the same thing";
      }


If by some weird reason, you can't make variables public or private, just use an accessor.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class letterType
{
char letter;
int letterCount;

public:
char letterType_Accessor()
{
   return letterType;
}

int letterCount_Accessor()
{
   return letterCount;
}

};

Last edited on
I understand that, I just don't know how to rework my program to implement private variables. I know that "letter" and "letterCount" need to be private.
Use accessors as I said
Topic archived. No new replies allowed.