Question

So, I'm having a problem with my inherited classes writing items to a file. I'll post a sample of the code because there are multiple instances of it and it won't write these certain items to a file. I'm not sure what I'm doing wrong, it's writing everything in the parent class just fine to a file, but those things from a child class won't write. This part has already been done in each child class:

//Parameterized constructor signature
Hourly::Hourly(int e, string n, string a, string p, double w, double h) :MyEmployee(e, n, a, p)

Here's the driver without the headers.

string fileInput;
    string employCategory = "";
    const string HOURLY = "Hourly";
    const string SALARIED = "Salaried";
    int answer;
    int count = 0;
    const int ONE = 1;
    const int TWO = 2;
    const int ARRAY_SIZE = 4;
    MyEmployee* payroll[ARRAY_SIZE];
    ifstream myOpenFile;
    ofstream myWrittenFile;
    payroll[0] = new Hourly(1, "H. Potter", "Privet Drive", "201-9090", 40, 12.00);
    payroll[1] = new Hourly(3, "R. Weasley", "The Burrow", "892-2000", 40, 10.00);
    payroll[2] = new Salaried(2, "A. Dumbledore", "Hogwarts", "803-1230", 1200);
    payroll[3] = new Salaried(4, "R. Hagrid", "Hogwarts", "910-8765", 1000);
    cout << "This program has two options:\n1 - Create a data file\n2 - Read data from a file and print paychecks.";
    //This loop will test if the user's input is valid.
    do
    {
        //Here, the user will enter a value to be used to either print checks or write a file.
        cout << "\nPlease enter <1> to create a file or <2> to print checks: ";
        cin >> answer;
        //The user entered one, so we'll write a file.
        if (answer == ONE)
        {
            cin.sync();
            cin.clear();
            cout << "\nPlease enter in the name of the file you wish to write to. Please don't forget to add .txt to the end: ";
            getline(cin, fileInput);
            myWrittenFile.open(fileInput);
            for (int i = 0; i < ARRAY_SIZE; i++)
            {
                MyEmployee* empPtr = payroll[i];
                if (typeid(*empPtr) == typeid(Hourly))
                {
                    Hourly* empHPtr = static_cast<Hourly*>(empPtr);
                }
                else if (typeid(*empPtr) == typeid(Salaried))
                {
                    Salaried* empSPtr = static_cast<Salaried*>(empPtr);
                }
            }
            for (int i = 0; i < ARRAY_SIZE; i++)
            {
                payroll[i]->writeData(myWrittenFile);
            }
            myWrittenFile.close();
            cout << "\nData saved .....";
            for (int i = 0; i < ARRAY_SIZE; i++)
            {
                delete payroll[i];
                payroll[i] = NULL;
            }


}

Here's a part from the parent class which includes writing to a file:

void MyEmployee::writeData(ofstream& out)
{
    out << empNum << "\n";
    out << name << "\n";
    out << address << "\n";
    out << phoneNum << "\n";
}

Here's a part of the child class:

void Hourly::writeData(ofstream& out)
{
    out << hoursWorked << "\n";
    out << wage << "\n";
    MyEmployee::writeData(out);
}
Was it helpful?

Solution

Seems, it is because your writeData() function is not declared as virtual.

When a function is declared as virtual and was overriden in the derived classes, it will be computed in the runtime what the function must be exactly called. (Virtual method tables).

Your function is not declared as virtual, so calling it from the MyEmployee pointer, you call the writeData() function which was defined in MyEmployee class, not in its derived classes. This is why it doesn't write to the file any data from the derived class.

PS:

typeid(*empPtr) == typeid(Hourly)
typeid(*empPtr) == typeid(Salaried)

These expressions will be always false.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top