开发者

How should i randomly call class member methods?

开发者 https://www.devze.com 2022-12-15 14:02 出处:网络
I am writing a small \"quiz program\". It looks similar to this: #include <cstdlib> #include <iostream>

I am writing a small "quiz program". It looks similar to this:

#include <cstdlib>
#include <iostream>
#include <time.h>

using namespace std;

using std::cout;

class cQuestion
{
  private:
    static short goodAnswers[20][2];

  public:
    static void checkAnswer(int questNumber)
    { 
      /* checking input, checking if answer is bad or good */
      /* putting the answer to cQuiz::answArr */
    };

    static void question1(void) { cout << "this is question 1"; };
    static void question2(void) { cout << "this is question 2"; };
    static void question3(void) { cout << "this is question 3"; };
    static void question4(void) { cout << "this is question 4"; };
    static void question5(void) { cout << "this is question 5"; };
    /*and so on to question 20*/
};

short cQuestion::goodAnswers[20][2] = {0,0};

class cQuiz
{
  private:
    static short questArr[5];
    static short answArr[5];

  public:
    void drawRandom(void)
    {
      srand ( ti开发者_如何学Pythonme(NULL) );

      for (int i = 0; i < 5; i++ )
        questArr[i] = rand() % 20 + 1;
    };

    void askQuestions(void)
    {
      for (int i = 0; i < 5; i++ )
      {
        /* call questions by question number from questArr */
        /* HOW SHOULD I CALL CERTAIN cQuestion CLASS MEMBER ?? */
        cQuestion::checkAnswer(questArr[i]);
      }
    };
};

short cQuiz::questArr[5] = {0};
short cQuiz::answArr[5] = {0};

int main(int argc, char *argv[])
{
  cQuiz quiz;
  quiz.drawRandom();
  quiz.askQuestions();

  system("PAUSE");
  return EXIT_SUCCESS;
}

I am wondering, how can (or should) I call class cQuestion member methods ? I was thinking about using an array of pointers to these members (cQuestion::question1, cQuestion::question2, and so on) or overloading subscript operator[].

I am not sure if either way is good or bad. Should i consider different solution or somehow use both together? Or am I completely missing the point?


This is not good design. Having to add a new method for each question means that you have to recompile each time you add aquestion to the quiz. And as you have found out, it is hard to call those functions randomly. A re-design is in order here.


Further to the OOP post above, how about:

class Question { // Make this a C++ interface
  public:
    Question(string q, string a)
      : QuestionText(q), Answer(a)
    {}
    string QuestionText;
    string Answer;    
} 

Then instantiate these using a factory or just in your initialisation function:

  q1 = Question("What is the secret of life", "DNA");
  q2 = Question("What is the answer to the great question", "42");

You should probably put these in a vector, rather than in local or global variables.


Apart from all the OOP dilemmas, maintain an array of function pointers to your member functions and randomly select one of them.


Why is each question in its own method? Why not make an array of strings to store the questions?


How about something like this?

string[] questions = {"Q1","Q2","Q3"};
void question(int i)
{
    cout << questions[i];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号