C++ Passing in a value from one class to an abstract class

Question

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;
}

Show source
| class   | inheritance   | oop   | c++   | abstract-class   2016-12-24 19:12 2 Answers

Answers to C++ Passing in a value from one class to an abstract class ( 2 )

  1. 2016-12-24 21:12

    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??

  2. 2016-12-24 23:12

    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).

Leave a reply to - C++ Passing in a value from one class to an abstract class

◀ Go back