-1

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;
}
Teeban
  • 35
  • 10
  • Please take a look at the [The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Scott Meyers' are particularly interesting ones. –  Dec 24 '16 at 18:12
  • You may be able to access a parent classes data members directly, depending on the access settings of the parent variables and the inheritance access. – Thomas Matthews Dec 24 '16 at 18:45
  • No you see i want to pass value from a class(which does not inherit) to a another class – Teeban Dec 24 '16 at 19:28

2 Answers2

0

as long as class QuestionMC is derived from class Question all the public and protected members of Question are inherited in QuestionMC.

to set filename of class Question then set it as a member of QuestionMC so add this to your

void QuestionMC::setfilename(filename)
{
    this->filename = filename;
    mcfilename = filename;
}

it works but one question: what is the purpose of inheritance? as long as member filename in base class why you declare it again in derived class??

Raindrop7
  • 3,889
  • 3
  • 16
  • 27
0

My solution to this would be to create a third class that stored the pointer to everything you use- In your case, it would store:

A pointer to the class Player.

A pointer to the class Question, or if it is created afterwards, to a vector that holds the pointers of all the questions.

Then, you add that class' pointer as a member to both of those classes (Player and Question), which will give them access to eachother.

This class would be a global variable, and should never be deleted until the end of the program (otherwise whenever trying to access to other classes it would crash).

ZeroZ30o
  • 375
  • 2
  • 18