30 Replies - 1461 Views - Last Post: 29 February 2012 - 05:42 PM
#16
Re: Author's mistakes: Visual C displaying Derived&Base destructor
Posted 27 February 2012 - 08:32 PM
True, but another objection is that console applications in general are utility programs intended to do a job and immediately return control to the shell so they can be run as part of a script. Using system("pause") prevents the program from exiting normally -- which isn't really the goal. The goal is to keep the terminal open, not to prevent the program from completing. This goal can be accomplished easily by either:
running the program from a terminal in the first place
or
using MSVS's "Start without debugging" feature
or
checking the "pause when execution ends" box in CodeBlocks project options
or ...
(I don't know about Xcode)
All of those 3 options allow the program to exit normally while still keeping the terminal open to see any output that it produced (and also tell what value the program returned -- since main must return an int, we might as well see what it was).
running the program from a terminal in the first place
or
using MSVS's "Start without debugging" feature
or
checking the "pause when execution ends" box in CodeBlocks project options
or ...
(I don't know about Xcode)
All of those 3 options allow the program to exit normally while still keeping the terminal open to see any output that it produced (and also tell what value the program returned -- since main must return an int, we might as well see what it was).
#17
Re: Author's mistakes: Visual C displaying Derived&Base destructor
Posted 27 February 2012 - 08:53 PM
#18
Re: Author's mistakes: Visual C displaying Derived&Base destructor
Posted 27 February 2012 - 09:28 PM
Ach, yes of course. It's getchar() and cin.get() that do that dirty deed. 
But still it doesn't let you see the return value from main, as the other solutions do.
But still it doesn't let you see the return value from main, as the other solutions do.
#19
Re: Author's mistakes: Visual C displaying Derived&Base destructor
Posted 27 February 2012 - 09:32 PM
Unfotunately GradedActivity.cpp is generating an error for the derivingClass statement of line 8,
Error: declaration is incompatible with "char GradedActivity::determineGrade()" (declared at line 13 of GradedActivity.h)
It seems it may have something to do with determineGrade being a private member function in the header file.
Error: declaration is incompatible with "char GradedActivity::determineGrade()" (declared at line 13 of GradedActivity.h)
It seems it may have something to do with determineGrade being a private member function in the header file.
//GradedActivity class is designed to hold the numeric score
//and letter grade of a graded activity.
#ifndef GRADEDACTIVITY_H
#define GRADEDACTIVITY_H
// GradedActivity class declaration
class GradedActivity
{
private:
char letter; // To hold the letter grade
double score; // To hold the numeric score
char determineGrade(); // Determines the letter grade
public:
// Default constructor: initializes the score member variable to 0.0
GradedActivity()
{ letter = ' '; score = 0.0;}
// Mutator function: accepts an argument for the score variable
void setScore(double s)
{ score = s; letter = determineGrade(); }
// Accessor functions: returns the letter grade that corresponds to the value in score
double getScore() const
{ return score; return letter; }
char getLetterGrade() const //Needed for line 31 in intMain
{ return letter; } //This following line was excluded from Pr15-02
};
#endif
#20
Re: Author's mistakes: Visual C displaying Derived&Base destructor
Posted 27 February 2012 - 09:37 PM
You didn't make the complete correction on line 13 that I showed in post #12. It has to be const:
char determineGrade() const; // Determines the letter grade
#21
Re: Author's mistakes: Visual C displaying Derived&Base destructor
Posted 27 February 2012 - 09:47 PM
simeesta, on 26 February 2012 - 03:20 PM, said:
As for setscore(), it isn't static so you can't call it like this GradedActivity::setScore(rawScore * percentage); you have to call it on an object, not a class.
By the way, I forgot to mention this earlier. The above comment is not correct. That call to GradedActivity::setScore was done inside the setScore method of the CurvedActivity class, which derives from the GradedActivity class so there's nothing at all wrong with using GradedActivity:: and in fact it's probably necessary to disambiguate between the setScore method of the base class and the setScore method of the derived class. (And if it turns out not to be necessary, it is certainly not harmful.)
#22
Re: Author's mistakes: Visual C displaying Derived&Base destructor
Posted 28 February 2012 - 07:40 PM
Thanks alot for pointing out the careless mistake, for me.
I was able to get the following program to compile, but unfortunately there's a logical error:
Output is supposed to be:
How many questions are on the exam? 100
How many questions did the student miss? 25
Enter the minimum passing score for this test: 60
Each question counts 1.0 points .
The minimum passing score is 60.0
The student's exam score is 75.0
The student's grade is P
The problem for me is that the last line states The student's grade is F.
Changing Line 11 to score <= minPassingScore, fixed this, but I'm wondering are there any other solutions to fix this?
As the comment notes, I put in the source code exactly as the author, and had to resort to adding variable declaration to the header file of PassFailActivity.h. Author doesn't seem to make any modifications to GradedActivity.h or its implementation file.
PassFailActivity.cpp
///////////////////////////These seem in order.
PaasFailExam.h
PassFailExam.cpp
Pr15-08 int main
I was able to get the following program to compile, but unfortunately there's a logical error:
Output is supposed to be:
How many questions are on the exam? 100
How many questions did the student miss? 25
Enter the minimum passing score for this test: 60
Each question counts 1.0 points .
The minimum passing score is 60.0
The student's exam score is 75.0
The student's grade is P
The problem for me is that the last line states The student's grade is F.
Changing Line 11 to score <= minPassingScore, fixed this, but I'm wondering are there any other solutions to fix this?
As the comment notes, I put in the source code exactly as the author, and had to resort to adding variable declaration to the header file of PassFailActivity.h. Author doesn't seem to make any modifications to GradedActivity.h or its implementation file.
PassFailActivity.cpp
#include "PassFailActivity.h"
//*************************************************************
// Member function PassFailActivity::getLetterGrade
// This function returns 'P' if the score is passing,
// otherwise it returns 'F'.
//*************************************************************
char PassFailActivity::getLetterGrade() const
{
char letterGrade;
if (score >= minPassingScore) //Error: member "GradedActivity::score" not accessible in GradedActivity.h
letterGrade = 'P'; //So, I decided to add double score variable to PassFailActivity.h
else
letterGrade = 'F';
return letterGrade;
}
#ifndef PASSFAILACTIVITY_H
#define PASSFAILACTIVITY_H
#include "GradedActivity.h"
class PassFailActivity : public GradedActivity
{
protected:
double score; //Author ommitted this from the example
double minPassingScore; // Minimum passing score. Private member variable
public:
// Default constructor: sets variable to 0
PassFailActivity() : GradedActivity()
{ minPassingScore = 0.0; }
// Constructor: overloaded constructor, accepts a double argument
PassFailActivity(double mps) : GradedActivity()
{ minPassingScore = mps; }
//The following 3 functions will be inherited
// Mutator
void setMinPassingScore(double mps)
{ minPassingScore = mps; }
// Accessors
double getMinPassingScore() const
{ return minPassingScore; }
char getLetterGrade() const;
};
#endif
///////////////////////////These seem in order.
PaasFailExam.h
#ifndef PASSFAILEXAH_H
#define PASSFAILEXAM_H
#include "PassFailActivity.h"
class PassFailExam : public PassFailActivity
{
private:
int numQuestions; // Number of questions
double pointsEach; // Points for each question
int numMissed; // Number of questions missed
public:
// Default constructor
PassFailExam() : PassFailActivity()
{ numQuestions = 0;
pointsEach = 0.0;
numMissed = 0; }
// Constructor
PassFailExam(int questions, int missed, double mps) :
PassFailActivity(mps)
{ set(questions, missed); }
//None of the following 4 functions will be inherited
// Mutator function
void set(int, int); // Defined in PassFailExam.cpp
// Accessor functions
double getNumQuestions() const
{ return numQuestions; }
double getPointsEach() const
{ return pointsEach; }
int getNumMissed() const
{ return numMissed; }
};
#endif
PassFailExam.cpp
#include "PassFailExam.h"
//*************************************************************
// set function
// The parameters are the number of questions and the
// number of questions missed.
//*************************************************************
void PassFailExam::set(int questions, int missed)
{
double numericScore; // To hold the numeric score
// Set the number of questions and number missed.
numQuestions = questions;
numMissed = missed;
// Calculate the points for each question.
pointsEach = 100.0 / numQuestions;
// Calculate the numeric score for this exam.
numericScore = 100.0 - (missed * pointsEach);
// Call the inherited setScore function to set the numeric score.
setScore(numericScore);
}
Pr15-08 int main
// This program demonstrates the PassFailExam class.
#include <iostream>
#include <iomanip>
#include "PassFailExam.h"
using namespace std;
int main()
{
int questions; // Number of questions
int missed; // Number of questions missed
double minPassing; // The minimum passing score
// Get the number of questions on the exam.
cout << "How many questions are on the exam? ";
cin >> questions;
// Get the number of questions the student missed.
cout << "How many questions did the student miss? ";
cin >> missed;
// Get the minimum passing score.
cout << "Enter the minimum passing score for this test: ";
cin >> minPassing;
// Define a PassFailExam object.
PassFailExam exam(questions, missed, minPassing);
// Display the test results.
cout << fixed << setprecision(1);
cout << "\nEach question counts " << exam.getPointsEach() << " points.\n";
cout << "The minimum passing score is " << exam.getMinPassingScore() << endl;
cout << "The student's exam score is " << exam.getScore() << endl;
cout << "The student's grade is " << exam.getLetterGrade() << endl;
system("pause");
return 0;
}
This post has been edited by mgrex: 28 February 2012 - 07:42 PM
#23
Re: Author's mistakes: Visual C displaying Derived&Base destructor
Posted 28 February 2012 - 08:05 PM
Sorry, your "correction" is incorrect.
Eliminate the new "score" data member that you added to PassFailActivity, and instead change the score member of GradedActivity from private to protected.
That will make it accessible to the derived class (PassFailActivity).
Eliminate the new "score" data member that you added to PassFailActivity, and instead change the score member of GradedActivity from private to protected.
That will make it accessible to the derived class (PassFailActivity).
#24
Re: Author's mistakes: Visual C displaying Derived&Base destructor
Posted 29 February 2012 - 02:30 PM
Thank you.
Now an error for the next exercise reads:
"Error: no instance of constructor "GradedActivity::GradedActivity" matches the argument list.
I notice that in line 22, 3 arguments are passed because PassFailExam.h's constructor has a constructor with 3 different arguments in the function's definition. Since GradedActivity has a default constructor with 2 different arguments, i decided to modify line 17 by doing: GradedActivity test1('B', 88.0);
But there was no luck in doing that.
Compile error:
int main
GradedActivity.h - Author made line 34 a virtual to demonstrate polymorphism in a previous example. Removing it resorted in a worse compile error. No modifications were made to the GradedActivity.cpp, which is in a previous post.
PassFailActivity.h, line 29 has virtual added to it to correspond to that in GradedActivity.h, it's implementation is in a previous post and no modifications were made.
No modification made to PassFailExam.h/.cpp
Now an error for the next exercise reads:
"Error: no instance of constructor "GradedActivity::GradedActivity" matches the argument list.
I notice that in line 22, 3 arguments are passed because PassFailExam.h's constructor has a constructor with 3 different arguments in the function's definition. Since GradedActivity has a default constructor with 2 different arguments, i decided to modify line 17 by doing: GradedActivity test1('B', 88.0);
But there was no luck in doing that.
Compile error:
1>------ Build started: Project: 15_11, Configuration: Debug Win32 ------ 1> Pr15-11.cpp 1>c:\path\chapter 15\15_11\15_11\pr15-11.cpp(17): error C2664: 'GradedActivity::GradedActivity(const GradedActivity &)' : cannot convert parameter 1 from 'double' to 'const GradedActivity &' 1> Reason: cannot convert from 'double' to 'const GradedActivity' 1> No constructor could take the source type, or constructor overload resolution was ambiguous ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
int main
//*************************************************************
//This will demonstrate polymorphism
//Now that the getLetterGrade function is declared virtual, the program works properly (as, Pr15-08)
//*************************************************************
#include <iostream>
#include <iomanip>
#include "PassFailExam.h"
using namespace std;
// Function prototype
void displayGrade(const GradedActivity &);
int main()
{
// Create a PassFailActivity object. Minimum passing score is 88.
GradedActivity test1(88.0); //ERROR: no instance of constructor "GradedActivity::GradedActivity" matches the argument list.
// Create a PassFailExam object. There are 100 questions,
// the student missed 25 of them, and the minimum passing
// score is 70.
PassFailExam test2(100, 25, 70.0);
// Display the object's grade data for both objects.
cout << "Test 1:\n";
displayGrade(test1); //GradedActivity object
cout << "Test 2:\n";
displayGrade(test1); //PassFailExam object
cin.get();
return 0;
}
//*************************************************************
// The displayGrade function displays a GradedActivity object's
// numeric score and l etter grade.
//*************************************************************
void displayGrade(const GradedActivity &activity)
{
cout << setprecision(1) << fixed;
cout << "The activity's numeric score is " << activity.getScore() << endl;
cout << "The activity's letter grade is " << activity.getLetterGrade() << endl;
}
GradedActivity.h - Author made line 34 a virtual to demonstrate polymorphism in a previous example. Removing it resorted in a worse compile error. No modifications were made to the GradedActivity.cpp, which is in a previous post.
//GradedActivity class is designed to hold the numeric score
//and letter grade of a graded activity.
#ifndef GRADEDACTIVITY_H
#define GRADEDACTIVITY_H
// GradedActivity class declaration
class GradedActivity
{ //Had to be changed from Private, to Protected, so that
protected: //the derived class, PassFailActivity.cpp in line 11 could utilze line 12
char letter; // To hold the letter grade
double score; // To hold the numeric score
char determineGrade() const; // Determines the letter grade
public:
// Default constructor: initializes the score member variable to 0.0
GradedActivity()
{ letter = ' '; score = 0.0;}
// Mutator function: accepts an argument for the score variable
void setScore(double s)
{ score = s; letter = determineGrade(); }
// Accessor functions: returns the letter grade that corresponds to the value in score
double getScore() const
{ return score; return letter; }
//****************************************************
// virtual before the return type, tells the compiler not to bind calls to getLetterGrade with
// the function at compile time. Instead, calls to the function will be bound dynamically to the
// function at runtime. Not neccessary to declare the getLetterGrade function in the PassFailActivity
// class as virtual, but it is still a good idea to declare the function virtual, though.
//****************************************************
virtual char getLetterGrade() const //Needed for line 31 in intMain
{ return letter; }
};
#endif
PassFailActivity.h, line 29 has virtual added to it to correspond to that in GradedActivity.h, it's implementation is in a previous post and no modifications were made.
#ifndef PASSFAILACTIVITY_H
#define PASSFAILACTIVITY_H
#include "GradedActivity.h"
class PassFailActivity : public GradedActivity
{
protected:
//double score; //doing this resulted in the opposite of the If statement, being displayed
double minPassingScore; // Minimum passing score. Private member variable
public:
// Default constructor: sets variable to 0
PassFailActivity() : GradedActivity()
{ minPassingScore = 0.0; }
// Constructor: overloaded constructor, accepts a double argument
PassFailActivity(double mps) : GradedActivity()
{ minPassingScore = mps; }
//The following 3 functions will be inherited
// Mutator
void setMinPassingScore(double mps)
{ minPassingScore = mps; }
// Accessors
double getMinPassingScore() const
{ return minPassingScore; }
virtual char getLetterGrade() const;
};
#endif
No modification made to PassFailExam.h/.cpp
#25
Re: Author's mistakes: Visual C displaying Derived&Base destructor
Posted 29 February 2012 - 02:38 PM
Quote
1>------ Build started: Project: 15_11, Configuration: Debug Win32 ------
1> Pr15-11.cpp
1>c:\path\chapter 15\15_11\15_11\pr15-11.cpp(17): error C2664: 'GradedActivity::GradedActivity(const GradedActivity &)' : cannot convert parameter 1 from 'double' to 'const GradedActivity &'
1> Reason: cannot convert from 'double' to 'const GradedActivity'
1> No constructor could take the source type, or constructor overload resolution was ambiguous
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
1> Pr15-11.cpp
1>c:\path\chapter 15\15_11\15_11\pr15-11.cpp(17): error C2664: 'GradedActivity::GradedActivity(const GradedActivity &)' : cannot convert parameter 1 from 'double' to 'const GradedActivity &'
1> Reason: cannot convert from 'double' to 'const GradedActivity'
1> No constructor could take the source type, or constructor overload resolution was ambiguous
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Your constructor is:
GradedActivity()
Note the lack of any parameters. I don't see any constructor that takes any arguments.
Jim
#26
Re: Author's mistakes: Visual C displaying Derived&Base destructor
Posted 29 February 2012 - 03:05 PM
After doing the following in GradedActivity.h
The error went away in the int main but a compile error still resulted
GradedActivity(double score)
{ letter = ' '; score = 0.0;}
The error went away in the int main but a compile error still resulted
1>------ Build started: Project: 15_11, Configuration: Debug Win32 ------ 1> Pr15-11.cpp 1>c:\path\chapter 15\15_11\15_11\passfailactivity.h(14): error C2512: 'GradedActivity' : no appropriate default constructor available 1>c:\path\chapter 15\15_11\15_11\passfailactivity.h(18): error C2512: 'GradedActivity' : no appropriate default constructor available 1> PassFailExam.cpp 1>c:\path\chapter 15\15_11\15_11\passfailactivity.h(14): error C2512: 'GradedActivity' : no appropriate default constructor available 1>c:\path\chapter 15\15_11\15_11\passfailactivity.h(18): error C2512: 'GradedActivity' : no appropriate default constructor available 1> PassFailActivity.cpp 1>c:\path\chapter 15\15_11\15_11\passfailactivity.h(14): error C2512: 'GradedActivity' : no appropriate default constructor available 1>c:\path\chapter 15\15_11\15_11\passfailactivity.h(18): error C2512: 'GradedActivity' : no appropriate default constructor available 1> Generating Code... ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
#27
Re: Author's mistakes: Visual C displaying Derived&Base destructor
Posted 29 February 2012 - 03:17 PM
Okay so you modified your constructor and no longer have a constructor with no parameters. But you still have places where you are trying to use a constructor that has no parameters. You will either need to create another constructor that has no constructor of alter all the calls that are trying to use the no parameter constructor to use your one constructor.
Also the following constructor:
Doesn't really make much sense. Why are you supplying a parameter yet never using this parameter? Also you should rename your parameter to something else, so it is not the same as you member variable with the same name. If you pass the parameter it would make more sense to use that parameter to assign the value to your member variable.
Jim
Also the following constructor:
GradedActivity(double score)
{ letter = ' '; score = 0.0;}
Doesn't really make much sense. Why are you supplying a parameter yet never using this parameter? Also you should rename your parameter to something else, so it is not the same as you member variable with the same name. If you pass the parameter it would make more sense to use that parameter to assign the value to your member variable.
Jim
#28
Re: Author's mistakes: Visual C displaying Derived&Base destructor
Posted 29 February 2012 - 04:00 PM
All i'm doing for now, is just entering the source code as it is in the textbook. If there are extraneous statements it is because of author.
I reverted the edit & added the following instead:
The program compiled but it resulted in a logical error. adding double score in PassFailActivity derived class, resulted in worse compile error.
http://i.imgur.com/2lP7T.jpg
Output should be
Test 1:
The activity's numeric score is 88.0
The activity's letter grade is B
Test 2:
The activity's numeric score is 75.0
The activity's letter grade is P
I've also noticed that in Line 29, return letter isn't needed, but it is in line 38.
I reverted the edit & added the following instead:
GradedActivity(double score)
{ score = 0.0; }
The program compiled but it resulted in a logical error. adding double score in PassFailActivity derived class, resulted in worse compile error.
http://i.imgur.com/2lP7T.jpg
Output should be
Test 1:
The activity's numeric score is 88.0
The activity's letter grade is B
Test 2:
The activity's numeric score is 75.0
The activity's letter grade is P
I've also noticed that in Line 29, return letter isn't needed, but it is in line 38.
//GradedActivity class is designed to hold the numeric score
//and letter grade of a graded activity.
#ifndef GRADEDACTIVITY_H
#define GRADEDACTIVITY_H
// GradedActivity class declaration
class GradedActivity
{ //Had to be changed from Private, to Protected, so that
protected: //the derived class, PassFailActivity.cpp in line 11 could utilze line 12
char letter; // To hold the letter grade
double score; // To hold the numeric score
char determineGrade() const; // Determines the letter grade
public:
// Default constructor: initializes the score member variable to 0.0
GradedActivity()
{ letter = ' '; score = 0.0;}
GradedActivity(double score)
{ score = 0.0; }
// Mutator function: accepts an argument for the score variable
void setScore(double s)
{ score = s; letter = determineGrade(); }
// Accessor functions: returns the letter grade that corresponds to the value in score
double getScore() const
{ return score; return letter; }
//****************************************************
// virtual before the return type, tells the compiler not to bind calls to getLetterGrade with
// the function at compile time. Instead, calls to the function will be bound dynamically to the
// function at runtime. Not neccessary to declare the getLetterGrade function in the PassFailActivity
// class as virtual, but it is still a good idea to declare the function virtual, though.
//****************************************************
virtual char getLetterGrade() const //Needed for line 31 in intMain
{ return letter; }
};
#endif
This post has been edited by mgrex: 29 February 2012 - 04:00 PM
#29
Re: Author's mistakes: Visual C displaying Derived&Base destructor
Posted 29 February 2012 - 04:34 PM
Quote
I've also noticed that in Line 29, return letter isn't needed, but it is in line 38.
That's because letter was declared to be a char and score is a double. The function getScore is supposed to return a double and the function getLetterGrade is supposed to return a char. When you define a function to return a certain type, you should make sure that that is actually what you're returning.
#30
Re: Author's mistakes: Visual C displaying Derived&Base destructor
Posted 29 February 2012 - 04:57 PM
I'm not sure where you are in this process by now, but I suspect you're doing much too much.
Looking back to the code in Post #24, the only change needed is to add the following constructor to GradedActivity.h:
Looking back to the code in Post #24, the only change needed is to add the following constructor to GradedActivity.h:
// added the following constructor for post24main:
GradedActivity(double s)
{ score = s;
letter = determineGrade();
}
|
|

New Topic/Question
Reply




MultiQuote



|