C++ 从基类派生函数
我可能走错了方向。我被要求创建一个特定类的对象数组。但是,该类有两个派生类C++ 从基类派生函数,c++,class,derived-class,C++,Class,Derived Class,我可能走错了方向。我被要求创建一个特定类的对象数组。但是,该类有两个派生类 class Employee { // Some stuff here about the backbone of the Employee class } class Salary: public Employee { // Manipulation for Salary employee } class Hourly: public Employee { // Manipulatio
class Employee {
// Some stuff here about the backbone of the Employee class
}
class Salary: public Employee {
// Manipulation for Salary employee
}
class Hourly: public Employee {
// Manipulation for hourly Employee
}
// Main Program
int main (int argc, char**argv) {
Employee data[100]; // Creates an array of Employee objects
while (employeecount > 100 || employeecount < 1) {
cout << "How many employees? ";
cin >> employeecount;
}
for(int x = 0; x <= employeecount; x++) {
while (status != "S" || status != "s"|| status != "H" || status != "h") {
cout << "Employee Wage Class (enter H for hourly or S for Salary)";
cin >> status;
}
if (status == "S" || status == "s") { // Salaried Employee
new
} else { // We valid for hourly or salary, so if its not Salaried it's an hourly
}
}
return 0;
}
class员工{
//这里有一些关于雇员阶级骨干的东西
}
类别薪金:公职人员{
//操纵雇员薪酬
}
每小时班:公共雇员{
//小时员工操作
}
//主程序
int main(int argc,字符**argv){
员工数据[100];//创建员工对象数组
而(employeecount>100 | | employeecount<1){
cout>employeecount;
}
对于(int x=0;x状态;
}
如果(状态==“S”| |状态==“S”){//受薪员工
新的
}否则{//我们对小时工资或薪水有效,所以如果没有薪水,则为小时工资
}
}
返回0;
}
我想问的问题是,基类能否调用派生类方法?例如,如果我为名为getgross
的Salary类创建了一个方法:我能否调用这样的方法:Employee.getgross()
?如果不能,我如何调用子类方法?声明getgross()
在Employee
类中作为virtual
例如:
class Employee {
virtual int getgross();
}
class Salary: public Employee {
virtual int getgross();
}
无论何时对指向Salary
对象的Employee*
调用getgross()
,都会调用Salary
的getgross()
。我也将
virtual
添加到Salary::getgross()
中,目前不需要这样做,但最好现在就包含它,因为您可能希望以后派生类表单Salary
数组需要是一个指针数组,以避免错误。更好的方法是使用智能指针的向量。在Employee
类中将getgross()
声明为virtual
例如:
class Employee {
virtual int getgross();
}
class Salary: public Employee {
virtual int getgross();
}
无论何时对指向Salary
对象的Employee*
调用getgross()
,都会调用Salary
的getgross()
。
我也将virtual
添加到Salary::getgross()
中,目前不需要这样做,但最好现在就包含它,因为您可能希望以后派生类表单Salary
数组需要是指针数组,以避免出现错误。更好的做法是使用智能指针的向量。要避免切片,需要存储指向对象的指针
Employee* data[100];
然后,您可以从各种派生类创建对象,并将它们放入数组中,例如
data[0] = new Salary;
为了调用正确的方法,您需要在基类中声明一个虚拟的方法,然后在派生类中重写该方法。为了避免切片,您需要存储指向对象的指针
class Employee {
// Some stuff here about the backbone of the Employee class
}
class Salary: public Employee {
// Manipulation for Salary employee
}
class Hourly: public Employee {
// Manipulation for hourly Employee
}
// Main Program
int main (int argc, char**argv) {
Employee data[100]; // Creates an array of Employee objects
while (employeecount > 100 || employeecount < 1) {
cout << "How many employees? ";
cin >> employeecount;
}
for(int x = 0; x <= employeecount; x++) {
while (status != "S" || status != "s"|| status != "H" || status != "h") {
cout << "Employee Wage Class (enter H for hourly or S for Salary)";
cin >> status;
}
if (status == "S" || status == "s") { // Salaried Employee
new
} else { // We valid for hourly or salary, so if its not Salaried it's an hourly
}
}
return 0;
}
Employee* data[100];
然后,您可以从各种派生类创建对象,并将它们放入数组中,例如
data[0] = new Salary;
为了调用正确的方法,您需要在基类中声明一个虚拟的方法,然后在派生类中重写该方法。最好的方法是将方法getGross()置于基类中(虚拟方法),以便派生类可以通过继承获取它。最好的方法是将方法getGross()设置在基类(虚拟方法)中,以便派生类可以通过继承获取它。一些想法:
class Employee {
// Some stuff here about the backbone of the Employee class
}
class Salary: public Employee {
// Manipulation for Salary employee
}
class Hourly: public Employee {
// Manipulation for hourly Employee
}
// Main Program
int main (int argc, char**argv) {
Employee data[100]; // Creates an array of Employee objects
while (employeecount > 100 || employeecount < 1) {
cout << "How many employees? ";
cin >> employeecount;
}
for(int x = 0; x <= employeecount; x++) {
while (status != "S" || status != "s"|| status != "H" || status != "h") {
cout << "Employee Wage Class (enter H for hourly or S for Salary)";
cin >> status;
}
if (status == "S" || status == "s") { // Salaried Employee
new
} else { // We valid for hourly or salary, so if its not Salaried it's an hourly
}
}
return 0;
}
您需要存储指向员工的指针数组。简单的员工数组不起作用。每次向数组中添加员工时,该数组将被“截断”到基类,并且任何额外的信息都将丢失
您可以使用dynamic_cast测试指针的类型,以便在进行调用之前检查类型。否则,您不能在给定基类指针(或引用)的情况下调用仅派生的成员函数
旧的指导方针“宁可遏制也不要继承”可以以一种完全不同的方式解决这个问题。只是将薪酬类型作为员工的财产,而完全失去继承权
一些想法:
您需要存储指向员工的指针数组。简单的员工数组不起作用。每次向数组中添加员工时,该数组将被“截断”到基类,并且任何额外的信息都将丢失
您可以使用dynamic_cast测试指针的类型,以便在进行调用之前检查类型。否则,您不能在给定基类指针(或引用)的情况下调用仅派生的成员函数
旧的指导方针“宁可遏制也不要继承”可以以一种完全不同的方式解决这个问题。只是将薪酬类型作为员工的财产,而完全失去继承权
正如其他人所说,必须使用指向Employee对象的指针,而不是数组中的Employee对象。这样,Employee指针就可以指向Employee派生的任何类
我认为您要做的是在Employee基类中实现该方法,并使其可供所有派生类使用
(从这里借用了这个答案):
“使用Base::f()
让我们从一个简单的例子开始。当调用非虚函数时,编译器显然不使用虚函数机制。而是使用成员函数的完全限定名来调用函数。例如,下面的C++代码…< /P>
void mycode(Fred* p)
{
p->goBowling(); // Pretend, Fred::goBowling() is non-virtual
}
…可能被编译成类似C的代码(p参数成为成员函数中的this对象):
实际的名称混乱方案比上面简单的方案更复杂,但你明白了。问题是,这种特殊情况没有什么奇怪的,它或多或少会分解为一个普通函数,类似于printf()
现在来看上面问题中讨论的情况:当您完全使用虚拟函数调用虚拟函数时