Virtual classes (Java interface style)

Hello. Ive been doing c++ for a while but then suddenly i felt for trying Java also.

I came across interfaces in java and i liked them immedeatly.

I couldnt understand virtual classes in c++ earlier but, for some reason java felt simplier. But now i wanted to make a test to see if i could understand them. So i tried to throw them into an array. (Is this even possible to do?) Please check my code and see if you can solve it.

What i want them to do is first say their name then do their own doWork function. The name line shows, but nothing happends when i call Staff[i].doWork();

Sorry for my bad enlish!

Thanks in advance.

EDIT: Solved Check Peter87 reply

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <string>

using namespace std;

class staff
{
public:
	staff(string name){this->name = name;}
	string Name(){return name;}
	virtual void doWork(){}
private:
	string name;
};

class grillMaster : public staff
{
public:
	grillMaster(string name):
	staff(name)
	{

	}

	void doWork()
	{
		cout << "I grill grill" << endl;
	}
};

class Clerk : public staff
{
public:

	Clerk(string name):
	staff(name)
	{

	}

	void doWork()
	{
		cout << "I serve serve" << endl;
	}
};

int main(int argc, char * argv[])
{
	staff Staff[4] = {grillMaster("Charles"), grillMaster("David"), grillMaster("John"), Clerk("Bella")};
	


	for(int i = 0; i < 4; i++)
	{
		cout << "Hello my name is " << Staff[i].Name() << endl;
		Staff[i].doWork();
		cout << endl;
	}

	char f;
	cin >> f;
	return 0;
}

Last edited on
You defined an array of type staff. In class staff function doWork is defined as

virtual void doWork(){}


that is it does nothing.

So the output will look as

Charles

David

John

Bella

Last edited on
The C++ equivalent of a Java interface is more or less a class with all member functions being pure virtual. It's not possible to create an object of a class containing a pure virtual functions. The derived classes has to implement the function to make it not pure and therefore possible to create objects of the class. To make doWork a pure virtual function in staff you write void doWork() = 0;

If I remember correctly Java interfaces can't have member variables so I guess staff is more like an abstract class in Java (that is if you change doWork to pure virtual).

So to your real problem. Staff is an array of 4 staff objects. The type of the objects in the array is fixed and can't be changed. What happens on line 49 is that the staff copy constructor is used to construct the staff objects from the temporary grillMaster and Clerk objects.

staff pointers can point to staff objects but also to objects of classes that inherits from staff. So what you have to do is use pointers (or references, or smart pointers).
staff* Staff[4] = {new grillMaster("Charles"), new grillMaster("David"), new grillMaster("John"), new Clerk("Bella")};
Ahh i see, Thank you for the reply again peter!

Solved!
Topic archived. No new replies allowed.