0

I am working on my first separate class file. I have been given a driver program which I am not supposed to change and I am to create a class file that runs off the driver.

#include <iostream>
#include <iomanip>
using namespace std;

#include "Question.h"

int main()
{
    string q2Answers [] = {"China","India","Mexico","Australia"};
    Question q2("Which country is home to the Kangaroo?",q2Answers,'D');

    q2.display();
    cout << endl;
}

The driver, simplified above, appears to be passing arguments to the class via arguments. My class header file is built in the following fashion.

#ifndef QUESTION_H
#define QUESTION_H
#include <string>
using namespace std;

class Question
{
    public:
        void setStem(string newStem);
        void setAnswers(string newAnswers[]); 
        void setKey(char newKey);
        void display();
        string getStem();
        string getAnswer(int index);
        char getKey();

    private:
        string stem;
        string answers[4];
        char key;

};

#endif // QUESTION_H

How can I execute the functions in my class using arguments passed into an object? I am confused as to how the line,

Question q2("Which country is home to the Kangaroo?",q2Answers,'D');

has any way of pushing those arguments into the functions. Any insight into this would be very appreciated.

  • 1
    http://www.geeksforgeeks.org/constructors-c/ – OldProgrammer Nov 06 '17 at 03:20
  • 1
    If this is an accurate representation of this so-called "driver program", then [whoever gave it to you is incompetent in C++](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice), and you should find a better C++ teacher. – Sam Varshavchik Nov 06 '17 at 03:29
  • If one doesn't know what `using namespace std` does, I think it's best to keep it until one gets a better grip of C++. Unless it's a big, serious project. – Jack Of Blades Nov 06 '17 at 03:53

1 Answers1

1

If I understood you correctly, you're asking for how to make a constructor(See OldProgrammer's link in the comment to your question):

You can either make it right in the header file, like so:

Question(const std::string& theQuestion, 
         const std::string theOptions[], const char& correctAnswer)
{
    this->stem = theQuestion;
    for(int i=0; i<4; i++){
        this->answers[i] = theAnswers[i];
    }
    this->key = correctAnswer;
}
~Question(){} 
//This is called the "Destructor", it is a function called when the object is destroyed

(You can imagine the const std::string&-part as string or const char&-part as just char if you don't know what they mean, since it's not very important right now.)

Or you can make it in the separate .cpp-file like so:

Question::Question(const std::string& theQuestion, 
         const std::string& theOptions[], const char& correctAnswer)
{
    this->stem = theQuestion;
    for(int i=0; i<4; i++){
        this->answers[i] = theAnswers[i];
    }
    this->key = correctAnswer;
}
Question::~Question(){} 

You might be asking why we use destructors; it's because sometimes there are things we need to do before the object is removed. Like for instance if you want to save certain info or make a change, or more commonly to free dynamic memory you allocated when you made the object. Otherwise you get memory leaks, which are bad.

Then you can construct/create an object as such:

Question q2("Which country is home to the Kangaroo?",q2Answers,'D');

You can also "overload" the constructor, i.e. you can make other versions of it. For example if you imagine that the constructor's only parameter was the question:

Question(std::string q){
      this->stem = q;
}
Question(char c[]){
      this->stem = c;
}

Now you can pass either a string or an array of characters to the object. But if you only have one, you can't do the other too, so if we only have the first constructor, we can't pass an array of characters to do the exact same thing. And you can make as many of these as you like, but it doesn't necessarily mean that it's better just because it has a ton of constructors.

Jack Of Blades
  • 495
  • 7
  • 16