I am a newbie to C++ programming and abstract design patterns.
The program is meant to allow the user to select whether the data he wants to enter is for a management staff or Junior staff then save the parameters in the linked list and display them too using the Linked List.
I have used abstract factory design to implement the selection of type of data to enter but having challenges with the linked list.
Prior to this exercise, I have a working linked list that stores integers and displays integers which I modified for this exercise.
The AddNode and PrintList functions of the Linked list aren't working (StaffMain.cpp), no error message displayed, the program compiles but the created object is not displayed when I call the list display function.
I have five files for the program (Staff.h, Staff.cpp, StaffList.h, StaffList.cpp and StaffMain.cpp)
I am sharing the part of the program having issues -linked list and the abstract factory codes below.
Kindly provide an insight on how to pass the selected factory class object to the linked list and display them.
Thank you.
using std::cout;
using std::endl;
List::List() //default constructor for List
{
head = NULL;
curr = NULL;
}
void List::AddNode(StaffFactory* c) //function to add node
{
List::Node* n = new List::Node;
n->next = NULL;
n->newstaffFactory = c;
if (head == NULL) //check if the list is empty
{
head = n;
}
else
{
curr = head; //start of the list and iterates till next points to end of the list
while (curr->next != NULL)
{
curr = curr->next;
}
curr->next = n;
}
cout << "New Node added" << endl;
}
void List::PrintList() //print nodes in the list
{
curr = head;
while (curr != NULL) //each node is printed till current pointer gets to end of the list
{
curr->newstaffFactory->staffDetails();
curr = curr->next;
}
cout << "The linked list printed" << endl;
}
sing std::cout;
using std::endl;
using std::cin;
class StaffFactory
{
public:
virtual Staff* staffDetails() = 0;
};
class JuniorStaffFactory :public StaffFactory
{
public:
Staff* staffDetails()
{
return new JuniorStaff();
}
};
class MgtStaffFactory :public StaffFactory
{
public:
Staff* staffDetails()
{
return new MgtStaff();
}
};
int main()
{
List stafflist;
int choice;
cout << "Select type of staff: " << endl;
cout << "1: Junior Staff" << endl;
cout << "2: Management Staff" << endl;
cout << "Selection: ";
cin >> choice;
cout << endl;
StaffFactory* newStaffFactory;
switch (choice)
{
case 1:
newStaffFactory = new JuniorStaffFactory;
break;
case 2:
newStaffFactory = new MgtStaffFactory;
break;
default:
cout << "Invalid selection!!!" << endl;
newStaffFactory = NULL;
break;
}
if (newStaffFactory != NULL)
{
Staff* c = newStaffFactory->staffDetails();
c->input();
stafflist.AddNode(newStaffFactory);
stafflist.PrintList();
}
}
Aucun commentaire:
Enregistrer un commentaire