How should I accidentally call class methods?

I am writing a small “quiz program”. It looks something like 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 ( time(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 (or should) I call class methods of class cQuestion? I was thinking about using an array of pointers for these members (cQuestion :: question1, cQuestion :: question2, etc.), or overloading the index operator [].

I'm not sure if this is good or bad anyway. Should I consider a different solution or somehow use both together? Or am I completely missing the point?

+3
source share
5 answers

In addition to all the OOP dilemmas, maintain an array of pointers to member functions and randomly select one of them.

+1
source

. , , . , , . .

+5

In addition 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 create them using factory or only in your initialization 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 them in a vector, and not in local or global variables.

+2
source

Why is each question different? Why not create an array of strings to hold questions?

0
source

How about something like that?

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

Source: https://habr.com/ru/post/1727612/


All Articles