dimanche 1 décembre 2019

How to inherited variable content from base class?

User class as Base class and Student class as derived class from User.

class User{
protected:
    string name;
    string id;
public:
    ...set and get function..

    virtual void Serialize(ofstream& fstream){
    fstream << id << "," << name << ",\n";
    }
}

class Student :public User{
private:
    string school;
public:
    ...set and get function...

    virtual void Serialize(ofstream& fstream){
    fstream << id << "," << name << "," << school << ",\n";
    }
}

Here are the main class.

void save(Save& i_save){
    ofstream outfile;
    outfile.open("save.csv", ios::app);
    i_save.Serialize(outfile);
}


int main() {

    User current_user;
    User current_student;

    string user_name;
    string user_id;
    string user_school;

    cin >> user_name;
    current_user.setName(user_name);

    cin >> user_id;
    current_user.setID(user_id);

    cin >> user_school;
    current_student.setSchool(user_user_school);

    save(current_student);

    return 0;

} 

Example input: SAM <- name, S02 <- id, and Rock <- school.

I want to save all this input in the CSV file, but when I run, it only saves school content without name and id.

How to solve this problem? thanks.

Aucun commentaire:

Enregistrer un commentaire