简体   繁体   English

在C ++中实现简单的Java程序(抽象类)

[英]Implementing simple Java program in C++ (Abstract class)

Java (Works) Java(Works)

Abstract class Personnell with subclasses of Manager and Worker. 抽象类Personnell,具有Manager和Worker的子类。 getAnnualIncome() is the abstract function. 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++ (Doesnt work) 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;
}

Error: array of Abstract class "Personnell" is not allowed: function "Personnell::getAnnualIncome" is a pure virtual function 错误:不允许抽象类“ Personnell”的数组:函数“ Personnell :: getAnnualIncome”是纯虚拟函数

Tried a few different things to do with pointers but I still need to get my head around them. 尝试过一些与指针有关的事情,但是我仍然需要弄清楚它们。 Thanks, Tom 谢谢汤姆

Edit (adding definitions and declarations) Personnell has 编辑(添加定义和声明)Personnell具有

virtual double getAnnualIncome()=0;

Manager has 经理有

double getAnnualIncome(); //declaration
double Manager::getAnnualIncome() //definition
{

return this->salary_;
}

Worker has 工人有

double getAnnualIncome(); //declaration
double Worker::getAnnualIncome() //definition
{
return (this->hourlyRate_ * this->hoursPerWeek_)*52;
}

doing what ajb said, the output is: 做ajb所说的,输出是:

Name Job Annual Income 姓名职务年收入

Tom Ableton 400000 汤姆·阿伯顿400000

Simon QuickiMart 400000 // Should be 7280 Simon QuickiMart 400000 //应该是7280

In C++, when you create an array with the type of the base class object, something called object slicing happens: ie when you declare an array of an abstract class and you put child classes in it, only the abstract parts (ie pure virtuals) are stored. 在C ++中,当使用基类对象的类型创建数组时,会发生称为对象切片的事情:即,当您声明一个抽象类的数组并将子类放入其中时,只有抽象部分(即纯虚函数)被存储。 And as you saw, you cannot call pure virtual functions because the objects in your employee array are sliced objects. 如您所见,您不能调用纯虚函数,因为employee数组中的对象是切片对象。

You can't have an array of Personnell objects, because Personnell is an abstract type, and the compiler won't know how big to make each array element. 您不能有一个Personnell对象数组,因为Personnell是抽象类型,并且编译器不知道将每个数组元素做成多大。 So you need to use pointers. 因此,您需要使用指针。 (It works in Java because, in effect, Java automatically uses pointers.) (它在Java中有效,因为Java实际上会自动使用指针。)

Change the employee definition to employee定义更改为

Personnell *employee[] = 
{
    new Manager ("Tom", "Ableton", 1234, 400000),
    new Worker ("Simon","QuickiMart", 666, 40, 3.50)
};

and instead of 而不是

cout << employee[i].getName() << "\t\t"<< employee[i].getDept()<<"\t\t"<< employee[0].getAnnualIncome() << endl;

use -> since employee[i] is now a pointer: 使用->因为employee[i]现在是一个指针:

cout << employee[i]->getName() << "\t\t"<< employee[i]->getDept()<<"\t\t"<< employee[i]->getAnnualIncome() << endl;

(The last employee[0] is a typo and should be changed to employee[i] ; also, for (int i = 0; i < 3; i++) looks like a typo since there are only two elements in the array.) (最后一个employee[0]是一个错字,应更改为employee[i] ;另外, for (int i = 0; i < 3; i++)看起来像是错字,因为数组中只有两个元素。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM