-
Classes and objects
Need a help please. I have a program calculating average of students grades:
Code:
#include <string>
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
const int TOTAL_STUDENTS = 10;
const int TOTAL_GRADES = 10;
void readInput(fstream *file, string[], int grades[][TOTAL_GRADES]);
void calcAvg(string names[], int grades[][TOTAL_GRADES], float average[]);
void printData(string names[], int grades[][TOTAL_GRADES], float average[]);
int main()
{
string names[TOTAL_STUDENTS];
int grades[TOTAL_STUDENTS][TOTAL_GRADES];
float average[TOTAL_STUDENTS];
fstream inputfile;
string fileName;
cout << "Hello there!" << endl;
cout << "This is a program to calculate students' average test scores" << endl;
cout << "-------------------------------------------------------------" << endl;
cout << "Now, enter the file name: ";
cin >> fileName;
cout << endl;
inputfile.open(fileName.c_str());
if(inputfile.is_open())
{
readInput(&inputfile, names, grades);
calcAvg(names, grades, average);
printData(names, grades, average);
}
else
{
cout << "Please enter a correct file name or make sure file is in the same directory as the program." << endl;
return -1;
}
inputfile.close();
return 0;
}
void readInput(fstream *file, string names[], int grades[][TOTAL_GRADES])
{
for(int i = 0; i < TOTAL_STUDENTS; i++)
{
*file >> names[i];
for(int j = 0; j < TOTAL_GRADES; j++)
{
*file >> grades[i][j];
}
}
}
void calcAvg(string names[], int grades[][TOTAL_GRADES], float average[])
{
float sum;
for(int i = 0; i < TOTAL_STUDENTS; i++)
{
sum = 0;
for(int j = 0; j < TOTAL_GRADES; j++)
{
sum += grades[i][j];
}
sum = (float)sum/TOTAL_GRADES;
average[i] = sum;
}
}
void printData(string names[], int grades[][TOTAL_GRADES], float average[])
{
for(int i = 0 ; i < TOTAL_STUDENTS; i++)
{
cout << names[i] << " ";
for(int j = 0; j < TOTAL_GRADES; j++)
{
cout << grades[i][j] << " ";
}
cout << fixed << showpoint << setprecision(2) << average[i] << endl;
}
}
I need to re-write this program in classes. I have header file:
Code:
#ifndef STUDENTTYPE_H
#define STUDENTTYPE_H
#include <iomanip>
const int TOTAL_STUDENTS = 10;
const int TOTAL_GRADES = 10;
class StudentType
{
public:
StudentType();
readInput(ifstream&):void;
calcAvg():void;
printData(ostream&) const:void;
private:
string names;
int grades[TOTAL_GRADES];
float average;
};
#endif // STUDENTTYPE_H
I am OK with main program. But I don't know how the implementation file should look like. Any suggestions please?
Code:
#include "StudentType.h"
#include <iostream>
StudentType::StudentType()
{
name = "";
for (int i=0; i<TOTAL_STUDENTS; i++)
{
grades[i]={0};
}
average = 0.0;
}
??????
Thank you
-
You have to implement the functions (actually you have almost the body of them already).
As you implemented the constructor , you shall do the other functions.
I mean you write
Code:
StudentType::functionName(parameterList)
{
/*body of the function*/
/*because of the operator :: you get access to the private fields of StudentType class*/
}
I hope that I understood the problem and this helps :)
-
I suggest overloading operator>> and operator<< for I/O instead:
Code:
class StudentType
{
public:
StudentType();
void calculateAverage();
friend std::istream& operator>>(std::istream& in, StudentType& student);
friend std::ostream& operator<<(std::ostream& out, const StudentType& student);
private:
std::string name;
int grades[TOTAL_GRADES];
float average;
};
Take a look at your previous implementation of readInput. You have this part to read the data for one student:
Code:
*file >> names[i];
for(int j = 0; j < TOTAL_GRADES; j++)
{
*file >> grades[i][j];
}
So, all you need to do is to implement operator>> using the same thing, e.g.,
Code:
std::istream& operator>>(std::istream& in, StudentType& student)
{
in >> student.name;
for (int i = 0; i < TOTAL_GRADES; ++i)
{
in >> student.grades[i];
}
return in;
}
This way, your readInput function becomes:
Code:
void readInput(fstream& file, StudentType students[])
{
for (int i = 0; i < TOTAL_STUDENTS; ++i)
{
file >> students[i];
}
}
Notice that I made file an fstream& instead of an fstream*.
Likewise, your calcAvg function will loop over the students and call the calculateAverage member function for each of them (or you can change it such that operator>> will call calculateAverage), and then your printData function will loop over the students and use the operator<< to print each of them.
-
Thank you, guys. It did help a lot! :)