I am a newbie to C++ programming and currently working on a program using Abstract Factory design pattern and linked list to store the objects and display them.
The user is expected to select whether the entry is for a management staff or Junior staff then save the parameters in the linked list and display them too.
I have created the abstract factories and the linked list but I am stuck on how to save the objects and display them with the linked list.
I need help with how to pass the staff object to the linked list and display them.
I have five files - Staff.h, Staff.cpp, StaffList.h, StaffList.cpp and StaffMain.cpp
Why are you creating two instances of Staff - c and d?
Also, fix your naming so that someone reading the code can gets a better picture of the logic. Yourt newStaff should be renamed to staffFactory. And staffDetails() method should be renamed to createStaff. And c and d variables - you don't need d, but say, c can be renamed to newStaff.
posted 3 weeks ago
I have updated the main function as below:
However the print list function is not working
posted 3 weeks ago
I think you got this code from somewhere else, and trying to make it work. There are many things wrong with this code.
Your linked list is of StaffFactory instances, instead of Staff instances you create. Your print list does not print. There are just two things that stand out.
I suggest - instead of writing so many classes (abstractions) beforehand, approach the problem step by step.
1. Create a new staff of just one type first, add it to linked list, print the linked list.
2. Once this is working, create different types of Staff in your code (not from user input), and make sure the linked list is still working.
3. Add user input, and dynamic creation of different staff instances.
I'm more C than C++, but I can spot something that looks wrong. Your Staff class has Name, Address and age fields, which is fine. But then JuniorStaff is a sub-class of Staff, so it inherits Staff's Name, Address and age fields, and it shouldn't have its own Name, Address and age fields. Similarly for MgtStaff.
And JuniorStaff::display() only needs to call Staff::display() to display its inherited Name, Address and age fields, plus a line to display the JuniorStaff-specific staffLevel field. Similarly for MgtStaff::display().
To answer your question, I think the main problem with your linked list is that the nodes contain StaffFactory (pointers to) objects. I think they should be Staff?
I've changed StaffFactory to Staff in the StaffList.cpp/h files, changed List::PrintList() to call Staff::display(), and passed c and d to staffList.AddNode() in main(). I've also corrected the code as suggested in my previous post. And the code basically works.
Have a go at making these changes and see how it goes.