Click here to Skip to main content
14,452,666 members

C / C++ / MFC

 
AnswerRe: setw irrational behaviour Pin
CPallini6-Jan-20 22:30
mveCPallini6-Jan-20 22:30 
Questionerror C2065: '_S_IFDIR' : undeclared identifier Pin
_Flaviu5-Jan-20 8:42
Member_Flaviu5-Jan-20 8:42 
AnswerRe: error C2065: '_S_IFDIR' : undeclared identifier Pin
k50545-Jan-20 11:31
Memberk50545-Jan-20 11:31 
AnswerRe: error C2065: '_S_IFDIR' : undeclared identifier Pin
Richard MacCutchan5-Jan-20 22:28
communityengineerRichard MacCutchan5-Jan-20 22:28 
QuestionC++ control textBox of Form1 and Form2 Pin
HeeMongKorea29-Dec-19 21:08
professionalHeeMongKorea29-Dec-19 21:08 
AnswerRe: C++ control textBox of Form1 and Form2 Pin
Richard MacCutchan29-Dec-19 23:02
communityengineerRichard MacCutchan29-Dec-19 23:02 
AnswerRe: C++ control textBox of Form1 and Form2 Pin
Victor Nijegorodov30-Dec-19 1:37
MemberVictor Nijegorodov30-Dec-19 1:37 
QuestionAbstract Factory Design/Linked List Problem Pin
EjireK28-Dec-19 5:59
MemberEjireK28-Dec-19 5:59 
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

<pre>#pragma once
#include<string>
 
 
 
class 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 new MgtStaff();
    }
};





Staff.cpp

#include<iostream>
#include<string>
#include"Staff.h"
 
using namespace std;
 
void Staff::input() 
{
 
}
 
void Staff::display()
{
 
    cout << "The staff name is " << Name << endl;
    cout << "The address of staff is " << Address << endl;
    cout << "The staff age is " << age << "years" << endl;
}
 
void JuniorStaff::input()
{
    cout << "Enter staff name: " << endl;
    cin.ignore();
    getline(cin, Name);
    cout << "Enter address of staff: " << endl;
    cin.ignore();
    getline(cin, Address);
    cout << "Enter staff age: " << endl;
    cin.ignore();
    cin >> age;
    cout << "Enter staff level: " << endl;
    cin.ignore();
    getline(cin, staffLevel);
     
}
 
void JuniorStaff::display() 
{
    cout << "The staff name is " << Name << endl;
    cout << "The address of staff is " << Address << endl;
    cout << "The staff age is " << age << "years" << endl;
    cout << "The staff level is " << staffLevel << endl;
 
}
 
void MgtStaff::input()
{
    cout << "Enter staff name: " << endl;
    cin.ignore();
    getline(cin, Name);
    cout << "Enter address of staff: " << endl;
    cin.ignore();
    getline(cin, Address);
    cout << "Enter staff age: " << endl;
    cin.ignore();
    cin >> age;
    cout << "Enter Management level: " << endl;
    cin.ignore();
    getline(cin, mgrLevel);
    mgrLevel = mgrLevel;
 
}
 
void MgtStaff::display()
{
    cout << "The staff name is " << Name << endl;
    cout << "The address of staff is " << Address << endl;
    cout << "The staff age is " << age << "years" << endl;
    cout << "The Manager level is " << mgrLevel << endl;
}


StaffList.h

#pragma once
#include"Staff.h"
 
 
class List {
private:
 
    class Node {
        friend class List;
        StaffFactory* newstaff;
        Node* next;
 
    public:
 
        Node()
        {
            this->newstaff = NULL;
            this->next = NULL;
        };
        Node(StaffFactory* c, Node* n)
        {
            this->newstaff = c;
            this->next = n;
 
        }
 
    };
 
 
    List::Node* head;
    List::Node* curr;
 
 
public: //this is where the functions go
 
    List();
    void AddNode(StaffFactory* c);
    void PrintList();
 
 
};


StaffList.cpp

#include<iostream>
#include<string>
#include"StaffList.h"
#include"Staff.h"
 
using namespace std;
 
 
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->newstaff = 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;
    }
}
 
 
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->newstaff->staffDetails();
        curr = curr->next;
 
    }
}


StaffMain.cpp

#include <iostream>
#include<string>
#include"Staff.h"
#include"StaffList.h"
 
 
using namespace std;
 
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* newStaff;
 
    switch (choice)
    {
 
    case 1:
        newStaff = new JuniorStaffFactory;
        break;
    case 2:
        newStaff = new MgtStaffFactory;
        break;
    default:
        cout << "Invalid selection!!!" << endl;
        newStaff = NULL;
        break;
    }
 
    if (newStaff != NULL)
    {
        Staff* c = newStaff->staffDetails();
        c->input();
        c->display();
        stafflist.AddNode(newStaff);
 
        Staff* d = newStaff->staffDetails();
        d->input();
 
        stafflist.AddNode(newStaff);
 
        stafflist.PrintList();
    }
 
}

AnswerRe: Abstract Factory Design/Linked List Problem Pin
Richard MacCutchan28-Dec-19 6:51
communityengineerRichard MacCutchan28-Dec-19 6:51 
GeneralRe: Abstract Factory Design/Linked List Problem Pin
EjireK28-Dec-19 7:14
MemberEjireK28-Dec-19 7:14 
GeneralRe: Abstract Factory Design/Linked List Problem Pin
Richard MacCutchan28-Dec-19 7:29
communityengineerRichard MacCutchan28-Dec-19 7:29 
GeneralRe: Abstract Factory Design/Linked List Problem Pin
EjireK28-Dec-19 13:18
MemberEjireK28-Dec-19 13:18 
GeneralRe: Abstract Factory Design/Linked List Problem Pin
leon de boer28-Dec-19 15:53
Memberleon de boer28-Dec-19 15:53 
GeneralRe: Abstract Factory Design/Linked List Problem Pin
Richard MacCutchan28-Dec-19 22:49
communityengineerRichard MacCutchan28-Dec-19 22:49 
AnswerRe: Abstract Factory Design/Linked List Problem Pin
Stefan_Lang6-Jan-20 1:22
mveStefan_Lang6-Jan-20 1:22 
Questionqueues to find minimum time to serve all. Pin
Jiopik27-Dec-19 20:01
MemberJiopik27-Dec-19 20:01 
AnswerRe: queues to find minimum time to serve all. Pin
Richard MacCutchan27-Dec-19 23:01
communityengineerRichard MacCutchan27-Dec-19 23:01 
AnswerRe: queues to find minimum time to serve all. Pin
leon de boer28-Dec-19 15:40
Memberleon de boer28-Dec-19 15:40 
QuestionHow to check if USB filter driver be installed by MFC? Pin
yufengchien26-Dec-19 23:19
Memberyufengchien26-Dec-19 23:19 
AnswerRe: How to check if USB filter driver be installed by MFC? Pin
Richard MacCutchan27-Dec-19 0:44
communityengineerRichard MacCutchan27-Dec-19 0:44 
QuestionKnight move, right direction, dynamic programming to get the maximum cost path from top left to right bottom. Pin
Jiopik25-Dec-19 1:25
MemberJiopik25-Dec-19 1:25 
AnswerRe: Knight move, right direction, dynamic programming to get the maximum cost path from top left to right bottom. Pin
Richard MacCutchan26-Dec-19 5:25
communityengineerRichard MacCutchan26-Dec-19 5:25 
QuestionIRichEditOle::InsertObject shifts text one byte Pin
ForNow21-Dec-19 19:18
MemberForNow21-Dec-19 19:18 
GeneralRe: IRichEditOle::InsertObject shifts text one byte Pin
Richard MacCutchan21-Dec-19 23:07
communityengineerRichard MacCutchan21-Dec-19 23:07 
GeneralRe: IRichEditOle::InsertObject shifts text one byte Pin
ForNow22-Dec-19 6:09
MemberForNow22-Dec-19 6:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.