简体   繁体   English

带用户输入的参数化构造函数 (C++)

[英]Parameterized Constructor with user input (C++)

I am writing a program that is asking the user to develop 2 types of questions: true/false or multiple choice.我正在编写一个程序,要求用户开发两种类型的问题:真/假或多项选择。 I have written the classes QuestionTF and QuestionMC - these two classes are derived from their base class Question.我编写了类 QuestionTF 和 QuestionMC - 这两个类派生自它们的基类 Question。 I have implemented parameters for both TF and MC:我已经为 TF 和 MC 实现了参数:

QuestionTF(string theQuestion, int pointValue, string theAnswer)
        : Question(theQuestion, pointValue)

AND

QuestionMC(string theQuestion, int pointValue, string theAnswer)
        : Question(theQuestion, pointValue)

When I call the constructor in main - it will look like QuestionTF(string, value, string).当我在 main 中调用构造函数时 - 它看起来像 QuestionTF(string, value, string)。 How would I code the constructors parameters so that the user has the ability to ask the question, input the value and select their answer (all of which will be set up in an if statement asking the user which type of question he/she would like to set up)?我将如何对构造函数参数进行编码,以便用户能够提出问题、输入值并选择他们的答案(所有这些都将设置在一个 if 语句中,询问用户他/她想要哪种类型的问题建立)?

Example: QuestionTF("question", 0, "answer");示例: QuestionTF("question", 0, "answer");

Here is your answer.这是你的答案。 I have passed 6 parameters in constructor among them, 3 are for the incorrect options.我在构造函数中传递了 6 个参数,其中 3 个是错误的选项。 similarly, you can implement this logic for true and false as well.同样,您也可以为 true 和 false 实现此逻辑。

class question{
public:
question(string question, int pointValue, string opt1,string opt2, string opt3, string opt4){
    int userinput;
    cout<<"Your question is :"<<endl<<question;
    cout<<endl<<"1. "+opt1<<endl<<"2. "+opt2<<endl<<"3. "+opt3<<endl<<"4. "+opt4<<endl;
    cin>>userinput;
    if (userinput== pointValue) {
        cout<<endl<<"Your answer is correct";
    }
    else
        cout<<endl<<"Your answer is incorrect";
}
question(string question, int pointValue){
    int userinput;
    cout<<"Your question is :"<<endl<<question<<endl;
    cout<<endl<<"1. True"<<endl<<"2. False"<<endl;
    cin>>userinput;
    if (userinput == pointValue) {
        cout<<"Your answer is correct";
    }
    else
        cout<<"Your answer is incorrect";
}

};

int main(){
question q = question("What is Capital of India ?",1,"Delhi","kolkata","Mumbai","Chennai");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM