Let's say i want to create a quiz program. i have an abstract class Question and a subclass of it named QuestionMC.Then i have Class player which will obtain player information such as player name and filename(for fstream) from the user. How do i pass the information from class player (filename and playername) to class Question ?
class Question
{
public:
virtual void showQuestion()=0;
protected:
string filename;
string question;
string answer;
string questionType;
};
class Player
{
public:
Player(int score=0):score(score){}
void askdetails();
string getfilename();
string filename;
protected:
string fname;
string lname;
};
class QuestionMC:public Question
{
public:
void showQuestion();
void setfilename();
protected:
string mcQuestion;
string mcfilename;
};
int main()
{
Player p;
p.askdetails();
p.getfilename();
QuestionMC mc;
mc.setfilename(p.filename);
}
void Player::askdetails()
{
string filename,fname,lname;
cout << "Please enter your First name : ";
cin >> fname;
cout << "Please enter your last name : " ;
cin >> lname;
cout << "Please enter the filename your quiz is stored : " ;
cin >> filename;
ifstream infile;
infile.open(filename.c_str());
if (infile.fail())
{
cout << "Error Opening file, either file does not exist or invalid filename "<<endl;
exit(1);
}
this->fname=fname;
this->lname=lname;
this->filename=filename;
ofstream outfile;
outfile.open("Score.txt");
outfile << "Player name and score : " << fname <<" "<< lname << " "<< score<<endl ;
outfile.close();
}
string Player::getfilename()
{
return filename;
}
void QuestionMC::setfilename(filename)
{
mcfilename=filename;
}