Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

*** Note this is considered a challenging lab *** You might want to check your d

ID: 3863059 • Letter: #

Question

*** Note this is considered a challenging lab

*** You might want to check your design with your instructor BEFORE coding

1. Define a class named Family in a header file named: family.h

This file acts as a prototype.

//

// Family.h

#pragma once

#ifndef family_h

#define family_h

#endif

struct person

{

    char FN[30];

    char LN[30];

    int age;

};

class Family

{

private:

    char* lname;

    person parent[2];

    person children[10];

public:

    void setName(char* n);

    char * getName();

    void setParent(char FN[], char LN[], int, int);

    char getParent(char FN[], char LN[], int, int);

    void setChildren(char FN[], char LN[], int, int);

    char getChildren(char FN[], char LN[], int, int);

};

2. Create the implementation file family.cpp

#include "Family.h"

#include

#include

#include

using namespace std;

void Family::setName(char LN[])

{

    strcpy(this->lname, LN);

};

char* Family::getName()

{

    return lname;

};

void Family::setParent(char FN[], char LN[], int age, int index)

{

    strcpy(parent[index].FN, FN);

    strcpy(parent[index].LN, FN);

    parent[index].age = age;

};

void Family::setChildren(char FN[], char LN[], int age, int index)

{

    strcpy(children[index].FN, FN);

    strcpy(children[index].LN, FN);

    children[index].age = age;

};

char Family:: getParent(char FN[], char LN[], int age, int index)

{

    return parent[index].FN, parent[index].LN, parent[index].age;

};

char Family:: getChildren(char FN[], char LN[], int age, int index)

{

    return children[index].FN, children[index].LN, children[index].age;

};

********My questions are below.******

a. The class will have the following members

1. Family name

2. Parent (array of 2)

a. First Name

b. Last Name

c. Age

3. Children (array of 10)

a. First Name

b. Last Name

c. Age

b. The class will be able to add parents or children

c. The class will be able to return information on parents and children.

3. Create the program Lab05b.cpp which uses the class Family to Implement the following

a. The program will allow the user to

1. Add families

a. With one or two parents

b. With zero or more children

2. Delete Families search by last name (should use the same search

function from item 3 below.

3. Display Families searching by last name

4. Display a list of all families

4. On exit print the following text to the screen; “So long from the family!” where is

the first family in the list (if the list is empty just print “Bye Bye”).

5. Bonus

a. Be able to store data in a file C: emp amily.dat

when the program exits

b. Be able to read the data from the file C: emp amily.dat

when the program starts.

Sample output:

Explanation / Answer

#include <iostream>
#include <list>
#include <vector>
#include <string.h>
using namespace std;

struct Person{
     char* FN; char* LN; int age;
};

class Family {
    private:
    char* name;
    Person parent[2];
    Person children[10];
    public:
    void setName(char* n);
    char * getName();
    void setParent(char FN[], char LN[], int a, int i );
    void setChildren(char FN[], char LN[], int a, int i);
};

void Family::setName(char n[]){
    name = n;
}

char* Family::getName(){
    return name;
}
void Family::setParent(char FN[], char LN[], int a, int i ){
    parent[i].FN = FN;
    parent[i].LN = LN;
    parent[i].age = a;
}
void Family::setChildren(char FN[], char LN[], int a, int i ){
    children[i].FN = FN;
    children[i].LN = LN;
    children[i].age = a;
}

char printMenu();
void addFamily(std::list<Family*> list);
void printAll(std::list<Family*> list);
void prinFamily(std::list<Family*> list,char * n);
void deleteFamily(std::list<Family*> list,char * n);
int main()
{
    std::list<Family*> mylist;
    std::list<Family*>::iterator it;
    cout << "" << endl;
    char c = '*';
    while(c != 'Q'){
        c = printMenu();
        if(c=='A'||c=='a'){
           addFamily(mylist);
        }
        if(c=='D'||c=='d'){
            char* name;
            cout << "Enter Name to Delete :";
            cin >>name;
            deleteFamily(mylist,name);
        }
        if(c=='P'||c=='p'){
            char* name;
            cout << "Enter Name :";
            cin >>name;
            prinFamily(mylist,name);
        }
        if(c=='L'||c=='l'){
           printAll(mylist);
        }
        if(c=='q'){
            c ='Q';
        }
    }
    return 0;
}

void printAll(std::list<Family*> list){
    std::list<Family*>::iterator it;
    for (it = list.begin(); it!=list.end(); it++) {
        cout << "Name: "<<(*it)->getName()<<endl;
    }
}
void prinFamily(std::list<Family*> list,char * n){
    std::list<Family*>::iterator it;
    for (it = list.begin(); it!=list.end(); it++) {
        char* name = (*it)->getName();
        if(strcmp(name,n)==0){
            cout << "Name: "<<(*it)->getName()<<endl;
            break;
        }
    }
    cout << "Not found"<<endl;
}
void deleteFamily(std::list<Family*> list,char * n){
    std::list<Family*>::iterator it;
    for (it = list.begin(); it!=list.end(); it++) {
        char* name = (*it)->getName();
        if(strcmp(name,n)==0){
            delete *it;
            break;
        }
    }
    cout << "Not found"<<endl;
}
void addFamily(std::list<Family*> list){
  
    Person* p = new Person;
    Family* f = new Family;
    char* name;
    int numOfP;
    cout << "Enter Name :";
    cin >> name;
    f->setName(name);
    cout << "Enter Number of Parent :";
    cin >>numOfP;
    for(int i =0;i<numOfP;i++){
        char* fname;
        char* lname;
        int age;
        cout << "Enter Parent First Name :";
        cin >>fname;
        cout << "Enter Parent Last Name :";
        cin >>lname;
        cout << "Enter Parent Age :";
        cin >>age;
        f->setParent(fname,lname,age,i);
    }
    cout << "Enter Number of Child :";
    cin >>numOfP;
    for(int i =0;i<numOfP;i++){
        char* fname;
        char* lname;
        int age;
        cout << "Enter Child First Name :";
        cin >>fname;
        cout << "Enter Child Last Name :";
        cin >>lname;
        cout << "Enter Child Age :";
        cin >>age;
        f->setChildren(fname,lname,age,i);
    }
    list.push_front(f);
}

char printMenu(){
    char choice;
    cout << " [A] Add Family "<< endl;
    cout << " [D] Delete Family "<< endl;
    cout << " [P] Display By Name "<< endl;
    cout << " [L] Display all "<< endl;
    cout << " [Q] Quit"<< endl;
    cout << "Choose an option :";
    cin >> choice;
    return choice;
}