C++ 派生 class 未正常运行
C++ derived class not functioning as it should
我是编程新手,我遇到了这个我无法解决的问题。我已经尝试了所有我能想到的。我已经做好了这么简单的错误的准备。
main.cpp
#include <iostream>
#include <iomanip>
#include "new_employee.h"
#include "new_employee.cpp"
#include "permanent_employee.cpp"
using namespace std;
int in_employee[4] = {101, 102, 103, 104};
int in_bankaccount[4] = {80045001, 80045002, 80045003, 80045004};
float in_hours[4] = {40, 50, 50, 51};
float in_rate[4] = {22, 22, 24, 26};
int main()
{
for(int i=0;i<4;i++)
{
new_employee employee[i](in_employee[i], in_bankaccount[i]);
}
///permanent_employee employee2(in_employee[1], in_bankaccount[1]);
///permanent_employee employee3(in_employee[2], in_bankaccount[2]);
///permanent_employee employee4(in_employee[3], in_bankaccount[3]);
}
new_employee.h
#if !defined NEW_EMPLOYEE
#define NEW_EMPLOYEE
class new_employee
{
public:
new_employee();
new_employee(int employee_number, int account_number);
private:
int employee_no, account_no;
float hourly_rate, hours_worked;
};
class permanent_employee : public new_employee
{
public:
permanent_employee();
permanent_employee(int employee_number, int account_number);
private:
float union_deduction, vhi_deduction;
};
#endif
new_employee.cpp
#include <iostream>
#include <iomanip>
#include "new_employee.h"
using namespace std;
new_employee::new_employee()
{
employee_no = 0;
account_no = 0;
}
new_employee::new_employee(int employee_number, int account_number)
{
employee_no = employee_number;
account_no = account_number;
}
permanent_employee.cpp
#include <iostream>
#include <iomanip>
#include "new_employee.h"
using namespace std;
permanent_employee::permanent_employee()
{
employee_no = 0;
account_no = 0;
}
permanent_employee::permanent_employee(int employee_number, int account_number)
{
employee_no = employee_number;
account_no = account_number;
}
因此,由于直接从 Codeblocks 复制的以下错误,我什至没有尝试正确 运行 程序的原始功能。
Z:\C++\Assignment 3 - Payroll Processing\new_employee.h||In
constructor 'permanent_employee::permanent_employee()':|
Z:\C++\Assignment 3 - Payroll Processing\new_employee.h|10|error: 'int
new_employee::employee_no' is private|
Z:\C++\Assignment 3 - Payroll
Processing\permanent_employee.cpp|9|error: within this context|
Z:\C++\Assignment 3 - Payroll Processing\new_employee.h|10|error: 'int
new_employee::account_no' is private|
Z:\C++\Assignment 3 - Payroll
Processing\permanent_employee.cpp|10|error: within this context|
Z:\C++\Assignment 3 - Payroll Processing\new_employee.h||In
constructor 'permanent_employee::permanent_employee(int, int)':|
Z:\C++\Assignment 3 - Payroll Processing\new_employee.h|10|error: 'int
new_employee::employee_no' is private|
Z:\C++\Assignment 3 - Payroll
Processing\permanent_employee.cpp|15|error: within this context|
Z:\C++\Assignment 3 - Payroll Processing\new_employee.h|10|error: 'int
new_employee::account_no' is private|
Z:\C++\Assignment 3 - Payroll
Processing\permanent_employee.cpp|16|error: within this context|
Z:\C++\Assignment 3 - Payroll Processing\main.cpp||In function 'int
main()':|
Z:\C++\Assignment 3 - Payroll Processing\main.cpp|18|error:
variable-sized object 'employee' may not be initialized|
Z:\C++\Assignment 3 - Payroll Processing\main.cpp|18|warning: unused
variable 'employee' [-Wunused-variable]| ||=== Build finished: 9
errors, 1 warnings (0 minutes, 0 seconds) ===|
我正在尝试使用派生的 class permanent_employee 创建基础 class new_employee。在我看来,每个人都在尝试访问对方的变量。如果有任何反馈,我将不胜感激。
感谢您的帮助。
PS。我是这个网站的新手,如果我发错了,我很抱歉。
变化:
permanent_employee::permanent_employee(int employee_number, int account_number)
{
employee_no = employee_number;
account_no = account_number;
}
要调用基础 class 构造函数:
permanent_employee::permanent_employee(int employee_number, int account_number)
: new_employee(employee_number, account_number)
{
}
由于 private
为 new_employee
声明了成员变量,即使派生的 classes 也无法访问它们。如果您希望派生的 classes 能够修改它们,您可以将它们声明为 protected
(但有时出于不变保存等原因,您不会这么做)。
您的成员变量的可访问性有问题。一般来说,可访问性是这样的:
Public : 任何人都可以看到并改变这些人。
受保护:包含这些变量作为成员的 class 和任何派生的 class 都可以更改这些变量。 classes 之外无法访问它们。
私有:只有包含这些成员变量的class可以以任何方式更改或使用它们。
错误是由于 permanent_employee
class 试图访问 new_employee
class 的私有成员造成的。您也可以尝试从派生构造函数调用基础 class 构造函数。
无论你走哪条路,我都强烈建议你在做任何其他事情之前花一些时间来充分理解 public、protected 和 private 成员变量和函数之间的区别。从长远来看,这将使您的生活更加轻松 运行。
我是编程新手,我遇到了这个我无法解决的问题。我已经尝试了所有我能想到的。我已经做好了这么简单的错误的准备。
main.cpp
#include <iostream>
#include <iomanip>
#include "new_employee.h"
#include "new_employee.cpp"
#include "permanent_employee.cpp"
using namespace std;
int in_employee[4] = {101, 102, 103, 104};
int in_bankaccount[4] = {80045001, 80045002, 80045003, 80045004};
float in_hours[4] = {40, 50, 50, 51};
float in_rate[4] = {22, 22, 24, 26};
int main()
{
for(int i=0;i<4;i++)
{
new_employee employee[i](in_employee[i], in_bankaccount[i]);
}
///permanent_employee employee2(in_employee[1], in_bankaccount[1]);
///permanent_employee employee3(in_employee[2], in_bankaccount[2]);
///permanent_employee employee4(in_employee[3], in_bankaccount[3]);
}
new_employee.h
#if !defined NEW_EMPLOYEE
#define NEW_EMPLOYEE
class new_employee
{
public:
new_employee();
new_employee(int employee_number, int account_number);
private:
int employee_no, account_no;
float hourly_rate, hours_worked;
};
class permanent_employee : public new_employee
{
public:
permanent_employee();
permanent_employee(int employee_number, int account_number);
private:
float union_deduction, vhi_deduction;
};
#endif
new_employee.cpp
#include <iostream>
#include <iomanip>
#include "new_employee.h"
using namespace std;
new_employee::new_employee()
{
employee_no = 0;
account_no = 0;
}
new_employee::new_employee(int employee_number, int account_number)
{
employee_no = employee_number;
account_no = account_number;
}
permanent_employee.cpp
#include <iostream>
#include <iomanip>
#include "new_employee.h"
using namespace std;
permanent_employee::permanent_employee()
{
employee_no = 0;
account_no = 0;
}
permanent_employee::permanent_employee(int employee_number, int account_number)
{
employee_no = employee_number;
account_no = account_number;
}
因此,由于直接从 Codeblocks 复制的以下错误,我什至没有尝试正确 运行 程序的原始功能。
Z:\C++\Assignment 3 - Payroll Processing\new_employee.h||In constructor 'permanent_employee::permanent_employee()':|
Z:\C++\Assignment 3 - Payroll Processing\new_employee.h|10|error: 'int new_employee::employee_no' is private|
Z:\C++\Assignment 3 - Payroll Processing\permanent_employee.cpp|9|error: within this context|
Z:\C++\Assignment 3 - Payroll Processing\new_employee.h|10|error: 'int new_employee::account_no' is private|
Z:\C++\Assignment 3 - Payroll Processing\permanent_employee.cpp|10|error: within this context|
Z:\C++\Assignment 3 - Payroll Processing\new_employee.h||In constructor 'permanent_employee::permanent_employee(int, int)':|
Z:\C++\Assignment 3 - Payroll Processing\new_employee.h|10|error: 'int new_employee::employee_no' is private|
Z:\C++\Assignment 3 - Payroll Processing\permanent_employee.cpp|15|error: within this context|
Z:\C++\Assignment 3 - Payroll Processing\new_employee.h|10|error: 'int new_employee::account_no' is private|
Z:\C++\Assignment 3 - Payroll Processing\permanent_employee.cpp|16|error: within this context|
Z:\C++\Assignment 3 - Payroll Processing\main.cpp||In function 'int main()':|
Z:\C++\Assignment 3 - Payroll Processing\main.cpp|18|error: variable-sized object 'employee' may not be initialized|
Z:\C++\Assignment 3 - Payroll Processing\main.cpp|18|warning: unused variable 'employee' [-Wunused-variable]| ||=== Build finished: 9 errors, 1 warnings (0 minutes, 0 seconds) ===|
我正在尝试使用派生的 class permanent_employee 创建基础 class new_employee。在我看来,每个人都在尝试访问对方的变量。如果有任何反馈,我将不胜感激。
感谢您的帮助。
PS。我是这个网站的新手,如果我发错了,我很抱歉。
变化:
permanent_employee::permanent_employee(int employee_number, int account_number)
{
employee_no = employee_number;
account_no = account_number;
}
要调用基础 class 构造函数:
permanent_employee::permanent_employee(int employee_number, int account_number)
: new_employee(employee_number, account_number)
{
}
由于 private
为 new_employee
声明了成员变量,即使派生的 classes 也无法访问它们。如果您希望派生的 classes 能够修改它们,您可以将它们声明为 protected
(但有时出于不变保存等原因,您不会这么做)。
您的成员变量的可访问性有问题。一般来说,可访问性是这样的:
Public : 任何人都可以看到并改变这些人。 受保护:包含这些变量作为成员的 class 和任何派生的 class 都可以更改这些变量。 classes 之外无法访问它们。 私有:只有包含这些成员变量的class可以以任何方式更改或使用它们。
错误是由于 permanent_employee
class 试图访问 new_employee
class 的私有成员造成的。您也可以尝试从派生构造函数调用基础 class 构造函数。
无论你走哪条路,我都强烈建议你在做任何其他事情之前花一些时间来充分理解 public、protected 和 private 成员变量和函数之间的区别。从长远来看,这将使您的生活更加轻松 运行。