- #1
yungman
- 5,718
- 241
I am looking at the program 15.13 in the Gaddis book. My question is only regarding of int main(){} that using pointer tests of GradedActivity to point to an array. I am trying to understand how it works. I reviewed on pointer to dynamic allocation of memory for array and try to change the code, it obviously doesn't work.
This is the whole program:
The main() I am trying to use is:
This is the error message:
This is more the way I learn, I think there is a problem because the size of GradedActivity and PassFailExam must be different. Just want to verify this cannot be done or I just have some problem with the codes and can be fixed.
Thanks
This is the whole program:
C++:
#include <iostream>
#include <iomanip>
using namespace std;
class GradedActivity
{protected: double score; // To hold the numeric score
public:
GradedActivity() { score = 0.0; }
GradedActivity(double s) { score = s; }
void setScore(double s) { score = s; }
double getScore() const { return score; }
virtual char getLetterGrade() const
{ char letterGrade; // To hold the letter grade
if (score > 89)letterGrade = 'A';
else if (score > 79)letterGrade = 'B';
else if (score > 69)letterGrade = 'C';
else if (score > 59)letterGrade = 'D';
else letterGrade = 'F';
return letterGrade;
}
};
class PassFailActivity : public GradedActivity
{protected: double minPassingScore; // Minimum passing score.
public:
PassFailActivity() : GradedActivity()
{
minPassingScore = 0.0;
}
PassFailActivity(double mps) : GradedActivity()
{
minPassingScore = mps;
}
void setMinPassingScore(double mps)
{
minPassingScore = mps;
}
double getMinPassingScore() const
{
return minPassingScore;
}
virtual char getLetterGrade() const
{ char letterGrade;
if (score >= minPassingScore) letterGrade = 'P';
else letterGrade = 'F';
return letterGrade;
}
};
class PassFailExam : public PassFailActivity
{private: int numQuestions; double pointsEach; int numMissed;
public:
PassFailExam() : PassFailActivity() {
numQuestions = 0;
pointsEach = 0.0;
numMissed = 0;
}
PassFailExam(int questions, int missed, double mps) : PassFailActivity(mps)
{
set(questions, missed);
}
void set(int questions, int missed)
{ double numericScore;
numQuestions = questions;
numMissed = missed;
pointsEach = 100.0 / numQuestions;
numericScore = 100.0 - (missed * pointsEach);
setScore(numericScore);
}
double getNumQuestions() const { return numQuestions; }
double getPointsEach() const { return pointsEach; }
int getNumMissed() const { return numMissed; }
};
void displayGrade(const GradedActivity* activity)
{ cout << setprecision(1) << fixed<<" The activity's numeric score is "<<
activity->getScore()<< "\n The activity's letter grade is " <<
activity->getLetterGrade() << "\n\n";
}
int main()
{ const int NUM_TESTS = 4;
GradedActivity* tests[NUM_TESTS] =// tests is array of GradedActivity pointers.
{ new GradedActivity(88.0), // Each element of tests is initialized with the
new PassFailExam(100, 25, 70.0),// address of a dynamically allocated object.
new GradedActivity(67.0),
new PassFailExam(50, 12, 60.0)
};
for (int count = 0; count < NUM_TESTS; count++)
{ cout << "Test #" << (count + 1) << ":\n";
displayGrade(tests[count]);
cout << endl;
}
return 0;
}
The main() I am trying to use is:
C++:
int main()
{ const int NUM_TESTS = 4;
GradedActivity* tests;
tests = new GradedActivity[NUM_TESTS];
tests[0] = GradedActivity(88.0);
tests[1] = PassFailExam(100, 25, 70.0);
tests[2] = GradedActivity(67.0);
tests[3] = PassFailExam(50, 12, 60.0);
for (int count = 0; count < NUM_TESTS; count++)
{
cout << "Test #" << (count + 1) << ":\n";
displayGrade(tests[count]);
cout << endl;
}
return 0;
}
This is the error message:
This is more the way I learn, I think there is a problem because the size of GradedActivity and PassFailExam must be different. Just want to verify this cannot be done or I just have some problem with the codes and can be fixed.
Thanks
Last edited: