C++ : Class Implementation. For the code provided, change the structure into a c
ID: 3777805 • Letter: C
Question
C++ : Class Implementation. For the code provided, change the structure into a class, make the member variables private, and implement functions to manage those private variables – such as the Accessors (getters) and Mutators (setters) functions. Describe the changes that you made to implement this latest class-based version of the program. You may implement as many additional functions as you find necessary for your solution. You must implement a class, and use private variables and public methods.
--------------C++ CODE------------------
//Program Description: This program works with record and files.
#include
#include
#include
#include
using namespace std;
/// defining the alphabet as an structure
struct Alfabeto{
int CountCapital; //counting capital letters
int CountLowCap; //counting lower cap
};
///Open Input/Output files: pass the file streams as parameters (by reference)
void openFile(ifstream &in, ofstream &out){
char FileName[20];
cout << "Enter input file name: "; //ask the user for the name of the input file
cin >> FileName;
in.open(FileName);
if(!in){ //If the file does not exist, print an appropriate message and exit
cout << "Can not open input file "<< FileName << endl;
exit(0);
}
cout << "Enter output file name: "; //ask the user for the name of the output file.
cin >> FileName;
out.open(FileName);
if (!out){
cout << "Can not open output file "<< FileName << endl;
exit(0);
}
}
///Counting the letters from the text file opened in the function openFile
/// file identifier & struct-array are passed as a parameters
int counting(ifstream &in, struct Alfabeto let[]){
int count = 0;
char ch;
while (in){
in.get(ch);
//counts every occurrence of capital letters A-Z
//info goes into an array of structures { struct Alfabeto let[] }
if(64 < ch && ch < 92)
let[static_cast(ch) - 65].CountCapital++;
else
//counts every occurrence of low-cap letters a-z
//info goes into an array of structures { struct Alfabeto let[] }
if(96 < ch && ch < 123)
let[static_cast(ch) - 97].CountLowCap++;
else
count--;
count++;
}//end of while
return count;
}
///Print the results to the output file
void printResult(ofstream &out, struct Alfabeto let[],int count){
int smallCount = 0, capitalCount = 0;
for(int i = 0; i < 26; ++i){
smallCount += let[i].CountLowCap;
}
for(int i = 0; i < 26; ++i){
capitalCount += let[i].CountCapital;
}
//Prints the number of capital letters and small letters
out << endl;
out << "Number of capital letters: " << capitalCount << endl;
out << "Number of small letters: " << smallCount << endl;
out << "Percentage of capital letters: "<< capitalCount * 100.0 / (capitalCount + smallCount)
<< "%" << endl;
out << "Percentage of small letters: " << smallCount * 100.0 / (capitalCount + smallCount)
<< "%"<< endl;
out<<"________________________________________ ";
//print the heading
out << "Letter Number Percentage"
<<" Letter Number Percentage";
//Print percentage of capital letters for every letter A-Z and
//the percentage of small letters for every letter a-z.
for(int i = 0; i < 26; ++i)
out <<" "<(i+65)
<<" "< < <<(let[i].CountCapital*100)/count<<"%"
<<" "<(i+97)
<<" "< < <<(let[i].CountLowCap*100)/count<<"%";
cout << " Results printed to the output file";
cout < }
/// PROGRAMER INFO
void programmer_info(ofstream& out)
{
out << " *************************************** ";
out << "* This program works with record and * ";
out << "* files. Counts every occurrence of * ";
out << "* capital letters and low-cap letters * ";
out << "* in the text file. Prints the number * ";
out << "* of capital letters and low-cap * ";
out << "* letters, as well as the percentage * ";
out << "* of capital letters for every letter * ";
out << "* A-Z and the percentage of small * ";
out << "* letters for every letter a-z. * ";
out << "* Finally, opens two output files. One* ";
out << "* chosen by the user and a second one * ";
out << "* named: name.txt which * ";
out << "* contains all required programmer * ";
out << "* info and algorithm. * ";
out << "*************************************** ";
}
///Creates an output file with program/programmer info
void programInfo(ofstream& outTWO){
char filename[]="Name_P4.txt";
outTWO.open(filename, ios::out|ios::trunc); //creates .txt file to folder
outTWO.fill(' ');
//outputs programmer info
outTWO << " *************************************** ";
outTWO << "* Project No. 4 * ";
outTWO << "*************************************** ";
//outputs DESCRIPTON OF THE PROGRAM
outTWO << "DESCRIPTON OF THE PROGRAM: ";
//outputs ALGORITHM
outTWO << "ALGORITHM: ";
outTWO << "1._ define the alphabet as an structure ";
outTWO << "2._ Function: Open Input/Output files ";
outTWO << " a)pass the file streams as parameters (by reference) ";
outTWO << " b)ask the user for the name of the input file ";
outTWO << " c)If the file does not exist, print an appropriate message and exit ";
outTWO << " d)ask the user for the name of the output file. ";
outTWO << "3._Function: Count the letters from the text file opened in the function ";
outTWO << " 'openFile'. File identifier & struct-array are passed as a parameters. ";
outTWO << " a)IF/ELSE statements: counts every occurrence of of capital letters A-Z ";
outTWO << " and low-cap letters a-z. ";
outTWO << " b)Info goes into an array of structures { struct Alfabeto let[] } ";
outTWO << "4._Function: Print the results to the output file ";
outTWO << " a)Prints the number of capital letters and small letters ";
outTWO << " b)prints the heading ";
outTWO << " c)Prints percentage of capital letters for every letter A-Z ";
outTWO << " d)print the percentage of small letters for every letter a-z. ";
outTWO << "5._Function: PROGRAMER INFO. Output programmer info ( along with TA name,...) ";
outTWO << " to the user defined output file. ";
outTWO << "6._Function: Creates an output file with program/programmer info ";
outTWO << " a)creates .txt file to folder ";
outTWO << " b)outputs programmer info ";
outTWO << " c)outputs DESCRIPTON OF THE PROGRAM ";
outTWO << " d)/outputs ALGORITHM ";
outTWO << "7._in MAIN: collection of variable declarations and function calls"<
outTWO.close();
}
int main(){
///variable declaration
struct Alfabeto Letras[26];
ifstream in;
ofstream out, outTWO;
int count = 0;
///initialize the array
for (int i =0; i <26; i++)
Letras[i].CountCapital=Letras[i].CountLowCap=0;
///Open Input/Output files
openFile(in, out);
///Counting the letters from the input file
count=counting(in, Letras);
///Print the results to the output file
printResult(out, Letras, count);
///Print programmer_info to the output file
programmer_info(out);
///Creates an output file with program/programmer info
programInfo(outTWO);
///Close the files
in.close();
in.clear();
return 0;
}
Explanation / Answer
PROGRAM CODE:
#include <iostream>
using namespace std;
int main() {
// your code goes here
return 0;
}//Program Description: This program works with record and files.
#include <http://ideone.com/Es8Xyu>
//#include
//#include
//#include
using namespace std;
/// defining the alphabet as an structure
//Changed - changed struct to class
class Alfabeto{
//made the varaibles private
private:
int CountCapital; //counting capital letters
int CountLowCap; //counting lower cap
//added public member functions for accessing the private varaibles
public:
void setCountCapital(int count)
{
CountCapital = count;
}
void setCountLowCap(int count)
{
CountLowCap = count;
}
void getCountCapital()
{
return CountCapital;
}
void getCountLowCap()
{
return CountLowCap;
}
void incrementCountCapital()
{
CountCapital++;
}
void incrementCountLowCap()
{
CountLowCap++;
}
};
///Open Input/Output files: pass the file streams as parameters (by reference)
void openFile(ifstream &in, ofstream &out){
char FileName[20];
cout << "Enter input file name: "; //ask the user for the name of the input file
cin >> FileName;
in.open(FileName);
if(!in){ //If the file does not exist, print an appropriate message and exit
cout << "Can not open input file "<< FileName << endl;
exit(0);
}
cout << "Enter output file name: "; //ask the user for the name of the output file.
cin >> FileName;
out.open(FileName);
if (!out){
cout << "Can not open output file "<< FileName << endl;
exit(0);
}
}
///Counting the letters from the text file opened in the function openFile
/// file identifier & struct-array are passed as a parameters
int counting(ifstream &in, Alfabeto let[]){
int count = 0;
char ch;
while (in){
in.get(ch);
//counts every occurrence of capital letters A-Z
//info goes into an array of structures { struct Alfabeto let[] }
if(64 < ch && ch < 92)
let[static_cast(ch) - 65].incrementCountCapital(); //changed here to a access from a function
else
//counts every occurrence of low-cap letters a-z
//info goes into an array of structures { struct Alfabeto let[] }
if(96 < ch && ch < 123)
let[static_cast(ch) - 97].incrementCountLowCap();//changed here to a access from a function
else
count--;
count++;
}//end of while
return count;
}
///Print the results to the output file
void printResult(ofstream &out, Alfabeto let[],int count){
int smallCount = 0, capitalCount = 0;
for(int i = 0; i < 26; ++i){
smallCount += let[i].getCountLowCap();//changed here to a access from a function
}
for(int i = 0; i < 26; ++i){
capitalCount += let[i].getCountCapital();//changed here to a access from a function
}
//Prints the number of capital letters and small letters
out << endl;
out << "Number of capital letters: " << capitalCount << endl;
out << "Number of small letters: " << smallCount << endl;
out << "Percentage of capital letters: "<< capitalCount * 100.0 / (capitalCount + smallCount)
<< "%" << endl;
out << "Percentage of small letters: " << smallCount * 100.0 / (capitalCount + smallCount)
<< "%"<< endl;
out<<"________________________________________ ";
//print the heading
out << "Letter Number Percentage"
<<" Letter Number Percentage";
//Print percentage of capital letters for every letter A-Z and
//the percentage of small letters for every letter a-z.
for(int i = 0; i < 26; ++i)
out <<" "<(i+65)
<<" "< < <<(let[i].getCountCapital()*100)/count<<"%"//changed here to a access from a function
<<" "<(i+97)
<<" "< < <<(let[i].getCountLowCap()*100)/count<<"%";//changed here to a access from a function
cout << " Results printed to the output file";
cout < }
/// PROGRAMER INFO
void programmer_info(ofstream& out)
{
out << " *************************************** ";
out << "* This program works with record and * ";
out << "* files. Counts every occurrence of * ";
out << "* capital letters and low-cap letters * ";
out << "* in the text file. Prints the number * ";
out << "* of capital letters and low-cap * ";
out << "* letters, as well as the percentage * ";
out << "* of capital letters for every letter * ";
out << "* A-Z and the percentage of small * ";
out << "* letters for every letter a-z. * ";
out << "* Finally, opens two output files. One* ";
out << "* chosen by the user and a second one * ";
out << "* named: name.txt which * ";
out << "* contains all required programmer * ";
out << "* info and algorithm. * ";
out << "*************************************** ";
}
///Creates an output file with program/programmer info
void programInfo(ofstream& outTWO){
char filename[]="Name_P4.txt";
outTWO.open(filename, ios::out|ios::trunc); //creates .txt file to folder
outTWO.fill(' ');
//outputs programmer info
outTWO << " *************************************** ";
outTWO << "* Project No. 4 * ";
outTWO << "*************************************** ";
//outputs DESCRIPTON OF THE PROGRAM
outTWO << "DESCRIPTON OF THE PROGRAM: ";
//outputs ALGORITHM
outTWO << "ALGORITHM: ";
outTWO << "1._ define the alphabet as an structure ";
outTWO << "2._ Function: Open Input/Output files ";
outTWO << " a)pass the file streams as parameters (by reference) ";
outTWO << " b)ask the user for the name of the input file ";
outTWO << " c)If the file does not exist, print an appropriate message and exit ";
outTWO << " d)ask the user for the name of the output file. ";
outTWO << "3._Function: Count the letters from the text file opened in the function ";
outTWO << " 'openFile'. File identifier & struct-array are passed as a parameters. ";
outTWO << " a)IF/ELSE statements: counts every occurrence of of capital letters A-Z ";
outTWO << " and low-cap letters a-z. ";
outTWO << " b)Info goes into an array of structures { struct Alfabeto let[] } ";
outTWO << "4._Function: Print the results to the output file ";
outTWO << " a)Prints the number of capital letters and small letters ";
outTWO << " b)prints the heading ";
outTWO << " c)Prints percentage of capital letters for every letter A-Z ";
outTWO << " d)print the percentage of small letters for every letter a-z. ";
outTWO << "5._Function: PROGRAMER INFO. Output programmer info ( along with TA name,...) ";
outTWO << " to the user defined output file. ";
outTWO << "6._Function: Creates an output file with program/programmer info ";
outTWO << " a)creates .txt file to folder ";
outTWO << " b)outputs programmer info ";
outTWO << " c)outputs DESCRIPTON OF THE PROGRAM ";
outTWO << " d)/outputs ALGORITHM ";
outTWO << "7._in MAIN: collection of variable declarations and function calls"<
outTWO.close();
}
int main(){
///variable declaration
Alfabeto Letras[26];
ifstream in;
ofstream out, outTWO;
int count = 0;
///initialize the array
for (int i =0; i <26; i++)
{
Letras[i].setCountCapital(0);//changed here to a access from a function
Letras[i].setCountLowCap(0);//changed here to a access from a function
}
///Open Input/Output files
openFile(in, out);
///Counting the letters from the input file
count=counting(in, Letras);
///Print the results to the output file
printResult(out, Letras, count);
///Print programmer_info to the output file
programmer_info(out);
///Creates an output file with program/programmer info
programInfo(outTWO);
///Close the files
in.close();
in.clear();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.