Data Structures using C++ Programming Consider the PersonType class: #ifndef PER
ID: 3790401 • Letter: D
Question
Data Structures using C++ Programming
Consider the PersonType class:
#ifndef PERSONTYPE_H
#define PERSONTYPE_H
#include
#include
#include
using namespace std;
class PersonType
{
public:
PersonType(string first = "None", string last = "None",
int a = 1230000)
{ firstName = first; lastName = last; id = a; }
void print()
{ cout << setw(15) << firstName << setw(15)
<< lastName << setw(10) << id << endl; }
bool operator <(const PersonType& right) const
{ return (lastName < right.lastName); }
bool operator >(const PersonType& right) const
{ return (lastName > right.lastName); }
private:
string firstName, lastName;
int id;
};
#endif
Now include a fourth private data member int key to indicate the key member (when key equals 1, firstName is the key; when key equals 2, lastName is the key; when key equals 3, id is the key). Modify PersonType.h correspondingly according to this requirement (set the default value of the parameter corresponding to key in the constructor as int k = 2).
Then show your outputs for the following program when you enter the key as;
(a) 1
(b) 2
(c) 3
#include
#include
#include "SortedList.h"
#include "PersonType.h"
using namespace std;
int main()
{
int key;
cout << "Enter the key (1 or 2 or 3): ";
cin >> key;
SortedList personList;
PersonType person1("Kevin", "White", 1234123, key);
personList.insert(person1);
PersonType person2("Albert", "Smith", 1234456, key);
personList.insert(person2);
PersonType person3("Marlon", "Ortega", 1234789, key);
personList.insert(person3);
PersonType person4("Dan", "Ortega", 1235123, key);
personList.insert(person4);
PersonType person5("Thomas", "Bell", 1235456, key);
personList.insert(person5);
cout << "The input list is: ";
for (int i = 0; i < personList.getLength(); i++)
personList[i].print();
cout << endl;
personList.remove(person4);
cout << "After the delete operation, the list is: ";
for (int i = 0; i < personList.getLength(); i++)
personList[i].print();
return 0;
}
Explanation / Answer
Modified PersonType.h
#ifndef PERSONTYPE_H
#define PERSONTYPE_H
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
class PersonType
{
public:
PersonType(string first = "None", string last = "None",
int a = 1230000,int k=2)
{ firstName = first; lastName = last; id = a;key=k; }
void print()
{ cout << setw(15) << firstName << setw(15)
<< lastName << setw(10) << id << endl; }
bool operator <(const PersonType& right) const
{
if(key==1)
{
return (firstName < right.firstName);
}
else if(key==2)
{
return (lastName < right.lastName);
}
else
{
return (id < right.id);
}
}
bool operator >(const PersonType& right) const
{
if(key==1)
{
return (firstName > right.firstName);
}
else if(key==2)
{
return (lastName > right.lastName);
}
else
{
return (id > right.id);
}
}
private:
string firstName, lastName;
int id;
int key;
};
#endif
Outputs for the given program
(a) key=1
Albert Smith 1234456 Dan Ortega 1235123 Kevin White 1234123 Marlon Ortega 1234789 Thomas Bell 1235456
After the delete operation, the list is:
Albert Smith 1234456 Kevin White 1234123 Marlon Ortega 1234789 Thomas Bell 1235456
(b) key=2
Kevin White 1234123 Thomas Bell 1235456 Dan Ortega 1235123 Marlon Ortega 1234789 Albert Smith 1234456
After the delete operation, the list is:
Kevin White 1234123 Thomas Bell 1235456 Marlon Ortega 1234789 Albert Smith 1234456
(c) key=3
Kevin White 1234123
Albert Smith 1234456
Marlon Ortega 1234789
Dan Ortega 1235123 Thomas Bell 1235456
After the delete operation, the list is:
Kevin White 1234123
Albert Smith 1234456
Marlon Ortega 1234789 Thomas Bell 1235456
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.