私はクイズプログラムを作成したいとしましょう。私は、抽象クラスのQuestionとそれのサブクラスQuestionMC.Then私はプレーヤーの名前とファイル名(fstreamの)のようなユーザーからプレーヤーの情報を取得するクラスのプレーヤーを持っている。どのように私はクラスプレーヤー(ファイル名とプレーヤー名)からクラスの質問に情報を渡すのですか?C++あるクラスから抽象クラスに値を渡す
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;
}
[The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)をご覧ください。スコットマイヤーズは特に興味深いものです。 –
親変数のアクセス設定と継承アクセスに応じて、親クラスのデータメンバーに直接アクセスすることができます。 –
私はクラスから継承していないクラスの値を別のクラスに渡したいと思いません – Teeban