• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Joe Ess
  • salvin francis

Need Help with Abstract Factory Pattern/Linked List Exercise

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Thank you.

I have five files - Staff.h, Staff.cpp, StaffList.h, StaffList.cpp and StaffMain.cpp

The codes are below

Staff.h



Staff.cpp




StaffList.h




StaffList.cpp



StaffMain.cpp


 
Ranch Hand
Posts: 136
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Debie Ade
Greenhorn
Posts: 2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have updated the main function as below:



However the print list function is not working

 
Salil Wadnerkar
Ranch Hand
Posts: 136
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 126
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Debie

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().

I suggest reading up on C++ inheritance before going any further - something like this https://www.geeksforgeeks.org/inheritance-in-c/ although others might have better suggestions.

Cheers
John
 
John Matthews
Ranch Hand
Posts: 126
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Cheers
John
 
a wee bit from the empire
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!