Classes in C++

Associate
Joined
6 Mar 2009
Posts
495
I have to create a quiz program in C++. It should have two classes. The first class called 'question' should hold all data and methods for a single question and the second class called 'quiz' should hold an array of questions, obtain the users answer and display there score.

Im not sure if im on the right track or not, and not sure how to go about adding the questions and answers etc.

Here is what i have done so far:

Code:
class question
{
public:
	string question;
	string possAnswers;
	int marks;
	string comment;
	string usersAnswer;
	int usersScore;
	
	void displayQuestion(string question);
	void usersAnswer(string usersAnswer);
	void usersScore(int usersScore);
	void displayComments(string comment);

};

class quiz
{
	string q [];
	void totalScore();
	void displayComments();

};
 
Think about it logically, what does a question have? What can you do with a question? A question is a very simple thing, keep it this way in your class.
Keep the quiz logic out of the question class, why should a question track the users score? It's down to the quiz to do that!
 
Im having a problem when i go to run my program. Its says "Unable to start program, file cannot be specified".

But it is showing no errors in the code:(
 
ok i have got it to work now. Im having problems passing parameters from my class to the object of the class.
#include <iostream>

using namespace std;

class Question
{
public:
string question;
string possAnswers;
int marks;
string comment;
string usersAnswer;

void displayQuestion(string question,string usersAnswer)
{
cout << "Question 1" << endl;
cout << "How do you declare a variable x to be a pointer to a constant interger?" << endl;
cout << "1) int * const x" << endl;
cout << "2) int const * const x" << endl;
cout << "3) int const *x" << endl;
cout << "4) int *x" <<endl;

}

};


int main()
{
cout << "Welcome to my Quiz on C++. Please press 1-4 to answer each question.";

Question q;
q.displayQuestion(string question,string usersAnswer);

system ("pause");
return 0;
}

The problem is at- q.displayQuestion(string question,string usersAnswer).

Sorry im not to good at this:(
 
With functions defined within a class, that function automatically gains all other class members within its scope. So in other words, I can write:

class Question
{
public:
string question;
string possAnswers;
int marks;
string comment;
string usersAnswer;

void displayQuestion();

};

void Question::displayQuestion()
{
cout << question << endl;
++marks;
cout << comment;
// or whatever
}

So I can access whatever I want when the function is defined within the class.

Aside: You'll also notice that I've declared the function prototype within the class, but actually put the function itself elsewhere. This is generally good practice with class design because it makes the objects smaller.


What do you actually want this class to do? I mean from what I can see here it looks like you want to go something like this:

1) Create an object of type Question
2) Set the variables within question. I.e. put the question you want answered in 'question' etc.
3) have main call displayQuestion to show the question held in the question string.
4) Have user input an answer
5) check their answer against a held 'correct response' which should be somewhere in the Question class.
6) return a 'correct/incorrect' and update player marks accordingly.
 
Im having problems Initializing an array inside my constructor.

class Question
{
public:

string question1, question2,question3,question4;
string answers1 [2];
string answers2 [4];
string answers3 [4];
string answers4 [4];

Question()
{
question1="Which of the statements in correct when using a header file:";
answers1= {"newClass","#include newClass"};

}

};

an error is appearing at answers1= part.

Any ideas??
 
Im having problems Initializing an array inside my constructor.



an error is appearing at answers1= part.

Any ideas??

You can only initialise strings that way when you're declaring them, not after you've already declared it.

What you want to do instead for C++ is:
Code:
	answers1[0]= "newClass";
	answers1[1]= "#include newClass";
 
Ok thanks:) Sorry for being a pain but i have a different piece of code from this thats getting errors.
class Question
{
public:
string question;
string possAnswers;
int marks;
string comment;
int usersAnswer;
int usersScore;


void displayQuestion();
void obtainAnswer();
void printUsersScore();
void displayQuestion2(int usersScore);
};


void Question::displayQuestion()
{

cout << endl;
cout << "Question 1" << endl;
cout << "How do you declare a variable x to be a pointer to a constant interger?" << endl;
cout << "1) int * const x" << endl;
cout << "2) int const * const x" << endl;
cout << "3) int const *x" << endl;
cout << "4) int *x" <<endl;
cout << "Please enter a number to correspond your answer:" << endl;

cin >> usersAnswer;

if(usersAnswer ==1)
usersScore +2;

else
usersScore +0;

}//displayQuestion

void Question::displayQuestion2(int usersScore)
{
cout << endl;
cout << "Question 2" << endl;
cout << "What is the statement used to print to the screen";
cout << "1) println" << endl;
cout << "2) cout" << endl;
cout << "3) cin" << endl;
cout << "System.out.print" << endl;
cout << "Please enter a number to correspond your answer:" << endl;

cin >> usersScore;

if(usersAnswer==2)
usersScore +2;

else
usersScore+0;

}//displayQuestion2

int main()
{
Question q;

cout << "Welcome to my Quiz on C++. Please press 1-4 to answer each question.";
cout <<endl;

q.displayQuestion();
q.displayQuestion2(usersScore);


system ("pause");
return 0;
}

What is the correct way to pass the usersScore into q.displayQuestion2();. As you can see i have done q.displayQuestion2(usersScore) but its getting errors:(
 
Ok thanks:) Sorry for being a pain but i have a different piece of code from this thats getting errors.

What is the correct way to pass the usersScore into q.displayQuestion2();. As you can see i have done q.displayQuestion2(usersScore) but its getting errors:(

Something like this:

Code:
#include <iostream>
#include <string>

using namespace std;

class Question
{
	public:
		string question;
		string possAnswers;
		int marks;
		string comment;
		int usersAnswer;
		int usersScore;


		void displayQuestion();
		void obtainAnswer();
		void printUsersScore();
		void displayQuestion2(int currentScore);
		int getUserScore() {return usersScore;};
};


void Question::displayQuestion()
{
	cout << endl;
	cout << "Question 1" << endl;
	cout << "How do you declare a variable x to be a pointer to a constant interger?" << endl;
	cout << "1) int * const x" << endl;
	cout << "2) int const * const x" << endl;
	cout << "3) int const *x" << endl;
	cout << "4) int *x" <<endl;
	cout << "Please enter a number to correspond your answer:" << endl;

	cin >> usersAnswer;

	if(usersAnswer ==1)
		usersScore += 2; // This is the same as usersScore = usersScore + 2
	else
		usersScore += 0; // This isn't required

}//displayQuestion

void Question::displayQuestion2(int currentScore)
{
	cout << endl;
	cout << "Question 2" << endl;
	cout << "What is the statement used to print to the screen";
	cout << "1) println" << endl;
	cout << "2) cout" << endl;
	cout << "3) cin" << endl;
	cout << "System.out.print" << endl;
	cout << "Please enter a number to correspond your answer:" << endl;

	cin >> usersAnswer;

	if(usersAnswer==2)
		usersScore = currentScore + 2;
	else
		usersScore+= 0;

}//displayQuestion2

int main()
{
	Question q;
	int userscore;

	cout << "Welcome to my Quiz on C++. Please press 1-4 to answer each question.";
	cout <<endl;

	q.displayQuestion();
	// Get the users current score after question 1
	userscore = q.getUserScore();
	q.displayQuestion2(userscore);

	system ("pause");
	return 0;
}

Couple of things:

1) The reason for the error is that you're trying to pass usersScore to displayQuestion2() but that variable is only visible from within the Question class. One way to fix this, is to add an accessor function (see getUserScore() above) that returns the current value of usersScore. Then you can pass the value to displayQuestion2(). However, are you sure you want to do this? The Question class already knows about the usersScore variable so you don't have to actually pass it to the function at all. You can just use it in displayQuestion2().

2) You can't do usersScore +2 as this will have no effect. What you want to do instead is either:
Code:
usersScore += 2;

or

Code:
usersScore = usersScore +2;

They both do the same thing.

3) Always post your code within [ code ] [/code] tags (remove the spaces) rather than quote tags, as it will keep the formatting intact.

Hope this helps. Let me know if anything I changed isn't clear.
 
Back
Top Bottom