继承的类不会写入文件
Inherited classes won't write to file
因此,我的继承类在将项写入文件时遇到问题。我将发布一个代码示例,因为它有多个实例,而且它不会将这些特定项目写入文件。我不确定我做错了什么,把父类中的所有内容都写在一个文件中,但子类中的内容不会写。这一部分已经在每个儿童班中完成:
//Parameterized constructor signature
Hourly::Hourly(int e, string n, string a, string p, double w, double h) :MyEmployee(e, n, a, p)
这是没有标题的驱动程序。
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 filen2 - 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;
}
}
这是父类的一部分,其中包括写入文件:
void MyEmployee::writeData(ofstream& out)
{
out << empNum << "n";
out << name << "n";
out << address << "n";
out << phoneNum << "n";
}
这是儿童班的一部分:
void Hourly::writeData(ofstream& out)
{
out << hoursWorked << "n";
out << wage << "n";
MyEmployee::writeData(out);
}
似乎是因为您的writeData()
函数未声明为虚拟函数。
当一个函数被声明为虚拟函数并在派生类中被覆盖时,它将在运行时计算该函数必须被精确调用的内容。(虚拟方法表)。
您的函数未声明为虚拟函数,因此从MyEmployee
指针调用它时,您将调用在MyEmployee
类中定义的writeData()
函数,而不是在其派生类中。这就是为什么它不将派生类中的任何数据写入文件的原因。
PS:
typeid(*empPtr) == typeid(Hourly)
typeid(*empPtr) == typeid(Salaried)
这些表达式将始终是false
。
相关文章:
- 头文件-继承c++
- 如何从另一个文件继承私有成员变量和公共函数
- C++两次从文件保存对象读取多重继承
- 在 c++ 中,当一个类继承头文件中的另一个类时出错
- 如何对继承的文件使用前向声明
- C++错误LNK2005和不同项目文件的常规继承
- C 从多个继承类写入二进制文件
- "Attribute is protected within this context"继承和 .h 和.cpp文件
- 在继承类中使用同一文件时,C 链接器错误
- C++继承 - 覆盖功能,包括使用 "::" s、.h 文件和.cpp文件
- 为什么多个头文件和源文件中的C++继承不起作用?
- 从同一文件构建基类和继承类
- 如何让头文件知道类继承
- python永远不能调用包含继承逻辑的c++dll或左右文件
- Pimpl习惯用法、单独的接口/实现文件和多个虚拟继承.如何
- 将继承的类存储到文件的更好方法
- 无法读取继承进程中的文件
- 类继承在多个文件c++中
- 使用#include和Inclusion Guards的独立文件中的C++继承
- 生成文件 - 继承的类和内部类