Java(作品)
具有 Manager 和 Worker 子类的抽象类 Personnell。getAnnualIncome() 是抽象函数。
Personnell employee[] =
{
new Manager("Thomas", "Nasa", 1337, 250000),
new Worker("Simon", "Netto", 1336, 6.98, 36)
};
System.out.println("Name\t\tAnnual Income");
for (int i = 0; i < employee.length; i++)
{
System.out.printf(employee[i].getName() + "\t\t£%.2f%n", employee[i].getAnnualIncome());
}
C++(不起作用)
Personnell employee[] =
{
Manager ("Tom", "Ableton", 1234, 400000),
Worker ("Simon","QuickiMart", 666, 40, 3.50)
};
cout << "Name\t\tJob\t\tAnnual Income"<< endl<<endl;
for (int i = 0; i < 3; i++)
{
cout << employee[i].getName() << "\t\t"<< employee[i].getDept()<<"\t\t"<< employee[0].getAnnualIncome() << endl;
}
错误:不允许抽象类“Personnell”的数组:函数“Personnell::getAnnualIncome”是纯虚函数
尝试了一些与指针有关的不同事情,但我仍然需要了解它们。谢谢,汤姆
编辑(添加定义和声明)Personnell 有
virtual double getAnnualIncome()=0;
经理有
double getAnnualIncome(); //declaration
double Manager::getAnnualIncome() //definition
{
return this->salary_;
}
工人有
double getAnnualIncome(); //declaration
double Worker::getAnnualIncome() //definition
{
return (this->hourlyRate_ * this->hoursPerWeek_)*52;
}
做 ajb 说的,输出是:
姓名 工作 年收入
汤姆·阿布尔顿 400000
Simon QuickiMart 400000 // 应该是 7280