- #1
yungman
- 5,755
- 293
Hi
There is one line of code I don't understand and I cannot find it in the book:
It is line 76: PassFailExam exam(questions, missed, minPassing); I have no idea what this line is. Normally, when we declare an object we just write PassFailExam exam to declare exam is Object of PassFailExam class. But this one has 3 parameters. It is not a constructor declaration either. What is this? It's almost like a function that return an object of PassFailExam. Is this the case? I never seen this before.
Follow up question is if it's a function that return a class object, then you can call on the public member functions of the object like in line 77, 78,79? Like calling getScore().After this question, I finished Inheritance in the chapter, only Polymorphism left for the whole book. Inheritance seems quite straight forward...OR, am I missing something here? Just want to make sure it's that simple. Please let me know.
I read a few pages of Polymorphism, that seems much harder, I am not even sure I understand the few two pages yet!
Thanks
There is one line of code I don't understand and I cannot find it in the book:
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; }
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;}
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; }
};
int main()
{ int questions; int missed; double minPassing;
cout << "How many questions are on the exam? "; cin >> questions;
cout << "How many questions did the student miss? ";cin >> missed;
cout << "Enter the minimum passing score for this test: ";cin >> minPassing;
PassFailExam exam(questions, missed, minPassing);//What is this?
cout << fixed << setprecision(1)<< "\nEach question counts "
<< exam.getPointsEach() << " points.\n The minimum passing score is "
<< exam.getMinPassingScore() << "\n The student's exam score is "<<
exam.getScore()<<"\n The student's grade is "<<exam.getLetterGrade()<<"\n\n";
return 0;
}
It is line 76: PassFailExam exam(questions, missed, minPassing); I have no idea what this line is. Normally, when we declare an object we just write PassFailExam exam to declare exam is Object of PassFailExam class. But this one has 3 parameters. It is not a constructor declaration either. What is this? It's almost like a function that return an object of PassFailExam. Is this the case? I never seen this before.
Follow up question is if it's a function that return a class object, then you can call on the public member functions of the object like in line 77, 78,79? Like calling getScore().After this question, I finished Inheritance in the chapter, only Polymorphism left for the whole book. Inheritance seems quite straight forward...OR, am I missing something here? Just want to make sure it's that simple. Please let me know.
I read a few pages of Polymorphism, that seems much harder, I am not even sure I understand the few two pages yet!
Thanks
Last edited: