Click here to Skip to main content
15,891,253 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

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

C++
<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

C++
#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

C++
#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

C++
#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

C++
#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 5:51
mveRichard MacCutchan28-Dec-19 5:51 
GeneralRe: Abstract Factory Design/Linked List Problem Pin
EjireK28-Dec-19 6:14
EjireK28-Dec-19 6:14 
GeneralRe: Abstract Factory Design/Linked List Problem Pin
Richard MacCutchan28-Dec-19 6:29
mveRichard MacCutchan28-Dec-19 6:29 
GeneralRe: Abstract Factory Design/Linked List Problem Pin
EjireK28-Dec-19 12:18
EjireK28-Dec-19 12:18 
GeneralRe: Abstract Factory Design/Linked List Problem Pin
leon de boer28-Dec-19 14:53
leon de boer28-Dec-19 14:53 
GeneralRe: Abstract Factory Design/Linked List Problem Pin
Richard MacCutchan28-Dec-19 21:49
mveRichard MacCutchan28-Dec-19 21:49 
AnswerRe: Abstract Factory Design/Linked List Problem Pin
Stefan_Lang6-Jan-20 0:22
Stefan_Lang6-Jan-20 0:22 
Questionqueues to find minimum time to serve all. Pin
Jiopik27-Dec-19 19:01
Jiopik27-Dec-19 19:01 
AnswerRe: queues to find minimum time to serve all. Pin
Richard MacCutchan27-Dec-19 22:01
mveRichard MacCutchan27-Dec-19 22:01 
AnswerRe: queues to find minimum time to serve all. Pin
leon de boer28-Dec-19 14:40
leon de boer28-Dec-19 14:40 
QuestionHow to check if USB filter driver be installed by MFC? Pin
yufengchien26-Dec-19 22:19
yufengchien26-Dec-19 22:19 
AnswerRe: How to check if USB filter driver be installed by MFC? Pin
Richard MacCutchan26-Dec-19 23:44
mveRichard MacCutchan26-Dec-19 23:44 
QuestionKnight move, right direction, dynamic programming to get the maximum cost path from top left to right bottom. Pin
Jiopik25-Dec-19 0:25
Jiopik25-Dec-19 0: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 4:25
mveRichard MacCutchan26-Dec-19 4:25 
QuestionIRichEditOle::InsertObject shifts text one byte Pin
ForNow21-Dec-19 18:18
ForNow21-Dec-19 18:18 
GeneralRe: IRichEditOle::InsertObject shifts text one byte Pin
Richard MacCutchan21-Dec-19 22:07
mveRichard MacCutchan21-Dec-19 22:07 
GeneralRe: IRichEditOle::InsertObject shifts text one byte Pin
ForNow22-Dec-19 5:09
ForNow22-Dec-19 5: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.