Search code examples
c++arrayspointersallocation

Not understanding what is being asked and how to type them?


I am having trouble understanding some parts of the question for this program and would like to know why and how to type the program. Here are the parts I'm unable to understand:

The third member variable is a pointer to a double, pquiz. This will be used to dynamically allocate an array which will hold a student's quiz grades.

(Did I do this correctly?)

The fourth member variable is a double holding the average value of the quiz grades.

The class should have a one parameter constructor which accepts an int and will dynamically allocate the array of double quiz grades. Or the class can >have a two parameter constructor which accepts both a string and an int.

The int is the number of quiz grades

The constructor uses the new operator to allocate memory for the array of quiz grades.

If there are two parameters, the string is the student's name.

The class needs the usual mutator, accessor functions and a destructor function.

The class has an additional function, average(), which calculates the average of all the quiz grades held in the array pointed to by pquiz. It returns the double average value.

I'm suppose to print a students name, number of tests the student took, and the average. Here is my program so far:

#include <iostream>
#include <string>

using namespace std;

class TestScore{

private:
    string name;
    int grades;
    double *pquiz;
    double average;

public:
    TestScore();
    void setName(string);
    void setGrades(int);
    void setAverage(double);
    string getName();
    int getGrades();
    double getPquiz();
    double getAverage();
};

TestScore::TestScore()
{
name="?";
grades=0;
pquiz=0;
average=0;
}
void TestScore::setName(string name1)
{
name=name1;
getline(cin,name1);
}
void TestScore::setGrades(int grades1)
{
grades=grades1;
}
void TestScore::setAverage(double average1)
{
average=average1;
}
string TestScore::getName()
{
return name;
}
int TestScore::getGrades()
{
return grades;
}
double TestScore::getAverage()
{
return average;
}
int main()
{
TestScore exam;
TestScore *ts=&exam;
string name;
int grade;
double *pquiz;
double average;
double total=0.0;
int count;
cout<<"Enter student name: ";
exam.setName(name);

cout<<"How many quizzes are there? ";
exam.setGrades(grade);
cin>>grade;
pquiz=new double[grade];
for(count=0; count<grade; count++)
{
    cout<<"Quiz "<<(count+1)<<": ";
    cin>>pquiz[count];
}
for(count=0; count<grade; count++)
{
    total+=pquiz[count];
}
average=total/grade;
cout<<exam.getName()<<" has an average of "<<average<<endl;
delete [] pquiz;
pquiz=0;
return 0;
}

Solution

  • If I understand correctly member grades holds the length of the array pointed to by member pquiz. Whenever grades is set pquiz must also reflect new change (old array should be deleted, new one should be created). In constructor must only create the array. In setGrades() must delete old and create new.

    Because member pquiz is controlled (or owned) by the class TestScore it's only logical that it should be deleted when object TestScore is deleted (or in our lingo "when it goes out scope"). This is done in class destructor, which you should add to the class.

    According to text there should be one more member function, average(), that calculates the average and stores the value in member average. This member should be renamed, for the sake of your sanity.