Help with understanding UML and function

I am having trouble with a class project. I am responsible for writing the 2nd part. I dont understand the add function, and I think I may have messed up the displayQuestion function. Any help is appreciated.

1. (10 pts ) Implement a class called Question. The UML below is a starting point. You may add other instance variables and methods if your group wants additional features represented. Please see the schedule for the review date of this class.
Question

-question: string
-answer : string
+Question() //constructor. Set instance variables
+Question(query : string, result : string) //constructor. Set instance variables
+getQuestion() : string //returns the question
+getAnswer : string //returns the answer to this question
+answerCorrect(candidateAnswer :string) : boolean //returns true if the candidate answer matches the answer
+displayQuestion() : void //displays this question and its answer



2. (10 pts) Implement a class called Quiz that manages an array of up to 25 Question objects. It keeps track of the correct and incorrect responses. The UML below is a starting point. Your group may add to this design and incorporate additional features. Please see the schedule for the review data of this class.
const int MAX_QUESTIONS = 25; //the size of the array
Quiz
-questions : Question[MAX_QUESTIONS] //an array named questions that holds Question objects
-current : int //value of the index of the current location available in the questions array
-correct : int // number of correct answers
-incorrect : int //number of incorrect answers
+Quiz( ) //constructor. Instance variables current, correct, and incorrect set to 0.
+add(newQuestion : Question) : void //add the specified question to the questions array if room available
+giveQuiz( ) : void // present each question to the user, accept an answer for each one, and keep track of the results (the number of correct and incorrect answers).
+getNumCorrect() : int //returns the number of correct answers
+getNumIncorrect() : int //returns the number of incorrect answers


3. (10 pts) Define a tester/driver class called QuizShow with a main method that creates a Quiz object. Create 5 Question objects (read in a question and answer from a file) and populate the quiz using the add method. Then give the quiz and print the final results.



4. (10 pts Extra Credit possible) Add the following enhancements:
a. In Question, add a point value to questions.
b. In Quiz, tally up a final score.
c. In QuizShow, add a function manuallyFillQuiz that populates the quiz by prompting the user for a question and answer.
d. In QuizShow, add a function fillQuizFromFile that populates the quiz with questions and answers read in from a file.
e. In QuizShow, give the user the option of using manuallyFillQuiz or fillQuizFromFile.
f. In QuizShow, show the user’s final score.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
 #include <iostream>
const int MAX_QUESTIONS = 25; // size of the array

using namespace std;

class Quiz
{
    



private:
    Question[MAX_QUESTIONS];
    int current;
    int correct;
    int incorrect;

    
public:
    Quiz();
    void add(Question newQuestion);
    void giveQuiz();
    int getNumCorrect();
    int getNumIncorrect();
    
    
};


/************************************
 *            Functions             *
 ***********************************/



// displays the question and verifies the answer

//Sets all variables to zero
Quiz::Quiz()
{
    int current           =   0;
    int correct           =   0;
    int incorrect         =   0;
}

// displays the questions and keeps track of correct/incorrect
void Quiz::giveQuiz() : Question
{
    
    cout << "******* Quiz ******* ";
    do {
        current++;
        cout << "Question # " << current << endl;
        cout << Question displayQuestion ;
        cin >> answer;                              // Need to define answer. How?
        if (Question answerCorrect == 1)
        {
            cout << "You are correct!" << endl;
            correct++;
        }
        else
        {
            cout >> "You are incorrect!" << endl;
            incorrect++;
        }
    } while (current < = 25);
}


 // Adds the questions to the questions array
void Quiz::add(Question newQuestion)
{
    Question
    for (int i =0; i < MAX_QUESTIONS; i++)
    {
        
    }
}


// returns the number of correct answers
int Quis::getNumCorrect()
{
    return correct;
}


// returns the number of incorrect answers
int getNumIncorrect()
{
    return incorrect;
}
Last edited on
The add function is just adding one question to the array. You should have a member variable which keeps track of the number of questions already added to the array, and is incremented for every successful call of add.

That variable also serves as the index at which the next question will be added to the array when add() is called.

Any more questions? :P
Im still not getting it... I have errors that don't really tell me anything.
What I dont understand is how to use the Question class that I have in my project and why I even need an add function. also how do I verify correct answers???

Thanks for any advice..

errors:
line 24 "expected class name" Question() is the class right??

line 30 "Unknown type name 'Question'" I dont even understand where I would use this

line 38 "Unknown type name'Question'" I tried with this add function but I still think Im doing it wrong....

lines 60-62 say that they are unused variables

line 66 "only constructors take base initializers" How else am I supposed to use the first class on mine?

line 75 " Expected a class or namespace

line 90 "Expected a class or namespace

line94 " Use of undeclared identifier 'newQuestion' did you mean 'Question'?

line 104 "definition of redeclaration of 'getNumCorrect' not allowed inside a function"

line 106 "expected expression"

line 107 expected a ";" this is the end of a function and I cant figure out why it would want it.

line 111,113 and 114 same as line 104, 106, and 107

l


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/************************************
 *             Header               *
 ************************************

Program Name: groupProject.cpp
Authors: Alex Brakeman, Cody Welch, Emanuel Guardado, & Emmanuel Vargas
Date written: In Progress
Class: CSC100
Teacher: Prof Baker
Discription: This header file is a class that will display the question from the "Question" class and keep track of right and wrong answers.
 
*/



/************************************
 *           Class Body             *
 ***********************************/
#include <iostream>
using namespace std;

const int MAX_QUESTIONS = 25; // size of the array

class Quiz : public Question()
{
    


protected:
    Question Question[MAX_QUESTIONS];
    int current;
    int correct;
    int incorrect;

    
public:
    Quiz();
    void add(Question newQuestion);
    void giveQuiz();
    int getNumCorrect();
    int getNumIncorrect();
    
    
};


/************************************
 *            Functions             *
 ***********************************/



// displays the question and verifies the answer

//Sets all variables to zero
Quiz::Quiz() : Question()
{
    Question();
    
    int current           =   0;
    int correct           =   0;
    int incorrect         =   0;
}

// displays the questions and keeps track of correct/incorrect
void Quiz::giveQuiz() : Question()
{
    string answer;
    cout << "******* Quiz ******* ";
    do {
        current++;
        cout << "Question # " << current << endl;
        cout << add (Question getQuestion);
        cin >> answer;                              // Need to define answer. How?
        if(Question::answerCorrect(answer) == 1 )
        {
            cout << "You are correct!" << endl;
            correct++;
        }
        else
        {
            cout << "You are incorrect!" << endl;
            incorrect++;
        }
    } while (current <= 25);
}


 // Adds the questions to the questions array
void Quiz::add(Question::newQuestion)
{
    if (current <= MAX_QUESTIONS)
    {
        cout << newQuestion;
    }
    else
    {
        cout << "You have reached your limit of questions." << endl;
 
}


// returns the number of correct answers
int Quiz::getNumCorrect()
{
    return correct;
}


// returns the number of incorrect answers
int Quiz :: getNumIncorrect()
{
    return incorrect;
}




/*
Group meeting 11/4/13:
1: How do we reference from one class to another?
2: What is the difference between correct/incorrect and getNumCorrect/getNumIncorrect? why do we need both?

*/
On line 24, you're not supposed to have the () after Question
The additional errors on line 30 and 38 are a consequence of the compiler not understanding line 24 (therefore causing a cascade of errors)
On line 66, your giveQuiz function isn't a constructor for your class, so you can't call the constructor to the Question class. IMO you shouldn't be inheriting from the Question class, since there isn't really any need to do that. All you need is an array of Question objects, and you should design the Question class so you can access all the needed functionality through public methods.
Your giveQuiz function isn't implemented correctly. It doesn't access any of the questions stored in your question array. something like:
1
2
3
4
5
6
7
Question q = questionArr[current];
cout << q.getQuestion() << endl;
cin >> answer;
if (q.answerCorrect(answer)
{
...
}

should go in your loop. since getQuestion and answerCorrect are member functions, you need to use the . operator to call them on objects of type Question.

I know you probably have many questions from this amount of stuff, so I'll stop here for now.
Line 94: You're supposed to be adding the question to the internal array of questions. Your code on line 94 is trying to print out the question. This only works for types with have the << operator overloaded. The assignment probably just wants you to use the getQuestion() function.
Line 99: You're missing a closing curly brace for your else.

Note: currently your array is named "Question", but you'll have to rename the question array to something like "questions" or "qArr" to keep the compiler from being confused between type and variable names.

You need the add() function because there's no other way to put questions into your Quiz class.
Topic archived. No new replies allowed.