Abstract Factory Design/Linked List Problem
-
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. The AddNode and PrintList functions of the Linked list aren't working. (StaffMain.cpp). Thank you. I have five files - Staff.h, Staff.cpp, StaffList.h, StaffList.cpp and StaffMain.cpp The codes are below Staff.h
#pragma once
#includeclass Staff
{
private:
std::string Name;
std::string Address;
int age;public:
Staff() //default constructor is assigned default values { Name = ""; Address = ""; age = 0; } Staff(std::string zName, std::string zAddress, int zage) //parameterized constructor is initialized to the values of the private members. { Name = zName; Address = zAddress; age = zage; } virtual void display() = 0; virtual void input() = 0;
};
class JuniorStaff : public Staff {
private:
std::string Name;
std::string Address;
int age;
std::string staffLevel;public:
JuniorStaff() //default constructor is assigned default values { Name = ""; Address = ""; age = 0; staffLevel = ""; } void input(); void display();
};
class MgtStaff : public Staff { //declaring a derived class "FamilyCar" with base class "Car"
private:
std::string Name;
std::string Address;
int age;
std::string mgrLevel;public:
MgtStaff() //default constructor is assigned default values { Name = ""; Address = ""; age = 0; mgrLevel = ""; } void input(); void display();
};
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 ne -
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. The AddNode and PrintList functions of the Linked list aren't working. (StaffMain.cpp). Thank you. I have five files - Staff.h, Staff.cpp, StaffList.h, StaffList.cpp and StaffMain.cpp The codes are below Staff.h
#pragma once
#includeclass Staff
{
private:
std::string Name;
std::string Address;
int age;public:
Staff() //default constructor is assigned default values { Name = ""; Address = ""; age = 0; } Staff(std::string zName, std::string zAddress, int zage) //parameterized constructor is initialized to the values of the private members. { Name = zName; Address = zAddress; age = zage; } virtual void display() = 0; virtual void input() = 0;
};
class JuniorStaff : public Staff {
private:
std::string Name;
std::string Address;
int age;
std::string staffLevel;public:
JuniorStaff() //default constructor is assigned default values { Name = ""; Address = ""; age = 0; staffLevel = ""; } void input(); void display();
};
class MgtStaff : public Staff { //declaring a derived class "FamilyCar" with base class "Car"
private:
std::string Name;
std::string Address;
int age;
std::string mgrLevel;public:
MgtStaff() //default constructor is assigned default values { Name = ""; Address = ""; age = 0; mgrLevel = ""; } void input(); void display();
};
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 neEjireK wrote:
The AddNode and PrintList functions of the Linked list aren't working.
You will need to be much more specific than that. Exactly what does "not working" mean, and where in the code do the failures occur. As a quick exercise you could write a simple linked list that holds basic types (integers for example) and work on that until you can see exactly how to make it work correctly. You can then adapt that skeleton to handle any object type. Next step is to create the basic staff type and process objects of that type into the list. Finally you can expand it to add the other child types. Taking things a step at a time is much better, and easier, than trying to create a full system in one go.
-
EjireK wrote:
The AddNode and PrintList functions of the Linked list aren't working.
You will need to be much more specific than that. Exactly what does "not working" mean, and where in the code do the failures occur. As a quick exercise you could write a simple linked list that holds basic types (integers for example) and work on that until you can see exactly how to make it work correctly. You can then adapt that skeleton to handle any object type. Next step is to create the basic staff type and process objects of that type into the list. Finally you can expand it to add the other child types. Taking things a step at a time is much better, and easier, than trying to create a full system in one go.
For clarity, my challenge is with passing the abstract factory class object to the Linked list to create the node and to display it. The program does not generate any error currently as it successfully prompts the user to select which type of staff to create then prompts the user to specify the parameters.The linked list works with creation of staff object without the abstract factory but the exercise specifies states that I should use abstract factory with linked list to save the staff objects and display it. I’ve been unable to use the linked list with the abstract factory class object. I think my problem is in the StaffMain.cpp where I’m to specify the factory object to pass to the linked list but can’t seem to get that working. Thank you.
-
For clarity, my challenge is with passing the abstract factory class object to the Linked list to create the node and to display it. The program does not generate any error currently as it successfully prompts the user to select which type of staff to create then prompts the user to specify the parameters.The linked list works with creation of staff object without the abstract factory but the exercise specifies states that I should use abstract factory with linked list to save the staff objects and display it. I’ve been unable to use the linked list with the abstract factory class object. I think my problem is in the StaffMain.cpp where I’m to specify the factory object to pass to the linked list but can’t seem to get that working. Thank you.
I am afraid that I do not have the time to analyse all that code. As I mentioned above, get a working linked list first that you know can handle simple objects. Once you have that logic working, then adapting it to handle any other object type is fairly trivial.
-
I am afraid that I do not have the time to analyse all that code. As I mentioned above, get a working linked list first that you know can handle simple objects. Once you have that logic working, then adapting it to handle any other object type is fairly trivial.
Thank you for your reply. As advised, I have worked on a simple linked list storing integer values. My simple linked list program has add node and display functions. The add node is working fine when the parameters are passed in the main program but the display function only displays the last item added. the other integers added previously are not showing. E.g for the program below, it only outputs 10 and the other integers are not displayed.
int List::add(int c)
{
cout << "Adding " << c << endl;Link\* temp; temp = new Link(c); if (temp == 0) { cout << "Memory allocation failed" << endl; // shows error in memory allocation } head = temp; return c;
}
void List::display()
{// pointer 'temp' used to iterate through the list Link\* temp = head; cout << "Displaying list:" << endl; while (temp !=NULL) { cout << "Value of object is " << temp->i <Next; }
}
int main()
{
List list;
list.add(6);
list.add(8);
list.add(10);
cout << endl;list.display(); cout << endl;
}
-
Thank you for your reply. As advised, I have worked on a simple linked list storing integer values. My simple linked list program has add node and display functions. The add node is working fine when the parameters are passed in the main program but the display function only displays the last item added. the other integers added previously are not showing. E.g for the program below, it only outputs 10 and the other integers are not displayed.
int List::add(int c)
{
cout << "Adding " << c << endl;Link\* temp; temp = new Link(c); if (temp == 0) { cout << "Memory allocation failed" << endl; // shows error in memory allocation } head = temp; return c;
}
void List::display()
{// pointer 'temp' used to iterate through the list Link\* temp = head; cout << "Displaying list:" << endl; while (temp !=NULL) { cout << "Value of object is " << temp->i <Next; }
}
int main()
{
List list;
list.add(6);
list.add(8);
list.add(10);
cout << endl;list.display(); cout << endl;
}
Why would you expect it to do anything else at no point do you chain the entries you just throw earlier entries away :-) I suggest you look at what List::add does because all you are doing is overwriting "head" Contrast it to what List::AddNode does in the code above which does actually create a chain
In vino veritas
-
Thank you for your reply. As advised, I have worked on a simple linked list storing integer values. My simple linked list program has add node and display functions. The add node is working fine when the parameters are passed in the main program but the display function only displays the last item added. the other integers added previously are not showing. E.g for the program below, it only outputs 10 and the other integers are not displayed.
int List::add(int c)
{
cout << "Adding " << c << endl;Link\* temp; temp = new Link(c); if (temp == 0) { cout << "Memory allocation failed" << endl; // shows error in memory allocation } head = temp; return c;
}
void List::display()
{// pointer 'temp' used to iterate through the list Link\* temp = head; cout << "Displaying list:" << endl; while (temp !=NULL) { cout << "Value of object is " << temp->i <Next; }
}
int main()
{
List list;
list.add(6);
list.add(8);
list.add(10);
cout << endl;list.display(); cout << endl;
}
In addition to Leon's comments above ... think about the logic. A linked list must have a constant pointer to the start of the list, the 'head'. Each item in the list should contain one or more data items, and a pointer to the next item. The head pointer should only be allocated once, when the list is first created. Each subsequent item should be chained to the node at the end of the chain. You can keep a pointer to the last item, or let the add method iterate through the list to find it.
-
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. The AddNode and PrintList functions of the Linked list aren't working. (StaffMain.cpp). Thank you. I have five files - Staff.h, Staff.cpp, StaffList.h, StaffList.cpp and StaffMain.cpp The codes are below Staff.h
#pragma once
#includeclass Staff
{
private:
std::string Name;
std::string Address;
int age;public:
Staff() //default constructor is assigned default values { Name = ""; Address = ""; age = 0; } Staff(std::string zName, std::string zAddress, int zage) //parameterized constructor is initialized to the values of the private members. { Name = zName; Address = zAddress; age = zage; } virtual void display() = 0; virtual void input() = 0;
};
class JuniorStaff : public Staff {
private:
std::string Name;
std::string Address;
int age;
std::string staffLevel;public:
JuniorStaff() //default constructor is assigned default values { Name = ""; Address = ""; age = 0; staffLevel = ""; } void input(); void display();
};
class MgtStaff : public Staff { //declaring a derived class "FamilyCar" with base class "Car"
private:
std::string Name;
std::string Address;
int age;
std::string mgrLevel;public:
MgtStaff() //default constructor is assigned default values { Name = ""; Address = ""; age = 0; mgrLevel = ""; } void input(); void display();
};
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 neQuote:
The AddNode and PrintList functions of the Linked list aren't working. (StaffMain.cpp).
I don't know about AddNode(), but your PrintList() function doesn't print anything: it calls the function staffDetails(), and maybe you intended that function to print out some information, but what it does instead is create a new object, which by the way creates a memory leak because you're not using the return value. I think your problem is that your list stores factories, not staff, and that some(?) of the functions don't do what their name implies.
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)