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

时间:2016-03-06 02:34:56

标签: c++

我正在编写一个程序,要求用户开发两种类型的问题:真/假或多选。 我编写了QuestionTF和QuestionMC类 - 这两个类派生自他们的基类Question。 我已经为TF和MC实现了参数:

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

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

当我在main中调用构造函数时 - 它看起来像 QuestionTF(字符串,值,字符串)。 我如何编码构造函数参数,以便用户能够提出问题,输入值并选择他们的答案(所有这些都将在if语句中设置,询问用户他/她想要哪种类型的问题设置)?

实施例: QuestionTF(“问题”,0,“回答”);

1 个答案:

答案 0 :(得分:0)

这是你的答案。我在构造函数中传递了6个参数,其中3个是不正确的选项。同样,你也可以为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");
}