I am creating a program using the inheritance. The superclass is person and subclass is employee,manager etc. I will prompt the user to choose which subclass he want to save the record to but i dont know how to write and display the record of different subclass to and from a txt file. May i know is there any example? Thanks a lot.
class Person
{
public:
virtual Save(string filename) = 0; // this makes it required to implement
};
class Employee : Person
{
public:
virtual Save(string filename)
{
// implement it for employees
}
};
class Manager : Person
{
public:
virtual Save(string filename)
{
// implement it for managers
}
};
int main( )
{
Person* p;
p = new Employee;
p->Save(filename); // calls employees save method
delete p;
p = new Manager;
p->Save(filename); // calls managers save method
delete p;
return 0;
}
C++ declares members private by default. You'll need to specify that the Save() function is public or at least protected in the base or superclass for this to work.