I'm really stuck on this Assignment and I need to find a solution ASAP before I get too far behind. Basicly, we're fuddling with inheritence and I've created 3 child classes from a parent 'Employee'. Each is a different type of employee. Right now, what I have is

Manager	m ("John","Doe",12,25,1950,2,14,1999, 3500, 1, "Accounting");  
	Salesworker	s ("John","Doe",12,25,1950,2,14,1999, 3, 4, 2, 9, 1);
	Peiceworker	p ("John","Doe",12,25,1950,2,14,1999, 2, 2, 1, 1, 0);
	Hourly h ("John","Doe",12,25,1950,2,14,1999, 44);

*teacher insists on ugly constructor overrides

what I would LIKE is something like this!

Manager* m = new Manager[3];  
	Salesworker* s = new Salesworker[3];
	Peiceworker* p = new Peiceworker[3];
	Hourly* h = new Hourly[3];

Where I custom initialize them later on for aesthetic purposes.
In either case, I'm really strapped for a menu in the console. My plan is to have the user choose the type of employee that they want to work with so that I can know what array they want to use, and then use that pointer to print a list of names and then the user can choose what Employee they want to work with. I find myself writing 4 different functions that roughly do the same thing, each take a pointer of one type, and use that pointer to then print the names, get an array position, and then point to the array, and then use my << or >> overload operators. I'd much rather find a way to genericly reference object array and position.

something like

Manager* m = new Manager[3];  
	Salesworker* s = new Salesworker[3];
	Peiceworker* p = new Peiceworker[3];
	Hourly* h = new Hourly[3];
           
         igaPtr = ChooseArray( *m, *s, *p, *h);
                     printNames( *igaPtr)
         cout << igaPtr[1];

Where igaPtr is a ptr that the user chose and now we're using and we don't have to explicitly call the * to object. I'm not sure if this is possible, but my class is online and my teacher lacks some serious communication skills. Any advice you can give, I would appreciate it! Thank you!

The standard way to do it with inheritance is probably to have an array of Employee pointers

Employee* employees[10];

(You can also dynamically allocate it if you really wish. It will be like

Employee** employees = new Employee*[10];

)

and then put whatever employees you want in there

employees[0] = new Manager("John","Doe",12,25,1950,2,14,1999, 3500, 1, "Accounting");
employees[1] = new Salesworker("John","Doe",12,25,1950,2,14,1999, 3, 4, 2, 9, 1);
// etc.

and then have a virtual method in the Employee class called "print" or something, which prints the name; each subclass can override it to do custom printing if they wish. Then just call it on every object pointed to in the array.

for (int i = 0; i < 10; i++)
  employees[i]->print(); // assuming every employees[i] points to a valid object

Thats fantastic!! I had suspected maybe I should be using an array of Employees but I literally had no idea how to do it. Your spot on with the virtual function calls, too. I had planned to do this, but in a much more complicated way. Thank you so much!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.