Menu

[solved]-Today Going Modify Code Use Standard Template Library Stl List Template Contain Instances Q38988616

Today, we are going to modify our code to use the StandardTemplate Library (STL) list<> template to contain all ourinstances of the WITperson class we instantiate. This will allow itto grow without limits (except physical memory!). You will alsoneed to instantiate an iterator for the class so that you cantraverse the list when displaying the list of persons in yourdatabase.Using the appropriate iterator<>, design your testharness to allow you to: 1. insert multiple instances of your classin the list; 2. display them all; 3. search your list by name; 4.remove any instance in the list without any effect on the remaininglist.

Source Code:

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;
#include “WitPerson.h”
int main(void)
{
   int selection;
   int index = 0;
   int i = 0;
   string newName;
   string newAddress;
   string newWnumber;
   string newRole;
   WitPerson* Entry[10];

   ofstream writefile;
   ifstream readfile;
   string filename;
   string readlines;

   cout << “Create a text file to save entries.(~~~.txt): “;
   cin >> filename;
   writefile.open(filename);

   for (int index = 0; index < 10; index++)
   {

   Selection:
       cout << “To display thecurrent list, Enter 2.nTo add a member, Enter 1.nTo exit theprogram, Enter 0.nSelection:”;
       cin >> selection;//Userchooses what they want to do

       if (selection == 1)// BeginFilling Arrary
       {
           Entry[i] = newWitPerson();
           cin >>*Entry[i];
           writefile<< “Entry#” << i + 1 << endl;
           writefile<< *Entry[i] << endl;
           i++;
       }
       Entry[index] = newWitPerson(newName, newAddress, newWnumber, newRole);

       if (selection == 2) //ReadsArray/Database
       {
           cout <<“////////////////////////////////////////////////n”;
           cout <<“nHere is the current database.n”;
          readfile.open(filename);
           for (index = 0;index < i; index++)
           {
              while (!readfile.eof())   //ReadsFile
              {
                  getline(readfile,readlines);
                  cout <<readlines;
                  cout << “n”;
              }
              readfile.close();
           }
           cout <<“////////////////////////////////////////////////nn”;
           gotoSelection;

       }
       if (selection == 0) // ExitProgram
       {

          std::at_quick_exit;
           return 0;
       }
       for (int index = 0; index < 10;index++)
       {
           deleteEntry[index];
       }

   }
   writefile.close();

   return 0;
}

Header CPP Code:

#include <string>
#include <iostream>
using namespace std;
#include “WitPerson.h”

//Default CTOR
WitPerson::WitPerson(void) {
   IDname = “Empty”;
   IDaddress = “Empty”;
   IDWnumber = “Empty”;
   IDrole = “Empty”;

}

//Default DTOR
WitPerson::~WitPerson() {}

//Copy CTOR
WitPerson::WitPerson(WitPerson* Entry)
{
   IDname = Entry->getName();
   IDaddress = Entry->getAddress();
   IDWnumber = Entry->getWnumber();
   IDrole = Entry->getRole();

}

//Added ostream and istream operators
ostream& operator<< (ostream& strm, constWitPerson& obj)
{
   strm << “Name: ” << obj.IDname <<endl;
   strm << “Address: ” << obj.IDaddress<< endl;
   strm << “WNumber: ” << obj.IDWnumber<< endl;
   strm << “Role: ” << obj.IDrole <<endl;
   return strm;
}

istream& operator>> (istream& strm, WitPerson&obj)
{
   cout << “nEnter Name: “;
   getline(strm >> ws, obj.IDname);
   cout << “Enter Address: “;
   getline(strm >> ws, obj.IDaddress);
   cout << “Enter W Number: “;
   getline(strm >> ws, obj.IDWnumber);
   cout << “Are you a Student, Staff, or Faculty?”;
   getline(strm >> ws, obj.IDrole);
   cout << “n”;

   return strm;
}

Header File:

#include <string>
#include<iostream>
#include<fstream>

using namespace std;
class WitPerson
{
   friend istream& operator>> (istream&,WitPerson&);

   friend ostream& operator<< (ostream&,const WitPerson&);
public:
   WitPerson();// Default CTOR

   //CTOR
   WitPerson(string newName, string newAddress, stringnewWnumber, string newRole)
   {
       this->IDname = newName;
       this->IDaddress =newAddress;
       this->IDWnumber =newWnumber;
       this->IDrole = newRole;
   }

   WitPerson(WitPerson* Entry);

   //Copy CTOR
   WitPerson(const WitPerson& person)
   {
       this->IDname =person.IDname;
       this->IDaddress =person.IDaddress;
       this->IDWnumber =person.IDWnumber;
       this->IDrole =person.IDrole;
   }

   string getName(void)const { return IDname; }
   string getAddress(void)const { return IDaddress;}
   string getWnumber(void)const { return IDWnumber;}
   string getRole(void)const { return IDrole; }

   string IDname = “”;
   string IDWnumber = “”;
   string IDaddress = “”;
   string IDrole = “”;

   virtual ~WitPerson(void);// DTOR
protected:

private:
   WitPerson& operator=(const WitPerson&src);
   void getName(const string newName);
   void getAddress(const string newAddress);
   void getWNum(const string newWNum);
   void getRole(const string newRole);

};

Expert Answer


Answer to Today, we are going to modify our code to use the Standard Template Library (STL) list template to contain all our insta… . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *