Question

In: Computer Science

C++ Coding ****** Please read prompt carefully and include screenshots fro verification. Start with a Person...

C++ Coding ****** Please read prompt carefully and include screenshots fro verification.

Start with a Person class, and create a multiset to hold pointers to person objects. Define the multiset with the comparePersons function object, so it will be sorted automatically by names of person. Define a half-dozen persons, put them in the multiset, and display its contents. Several of the persons should have the same name, to verify that multiset stores multiple object with the same key. Also allow the user to search a person object by the last name and the first name.

Solutions

Expert Solution

Code :

#include <iostream>
#include <algorithm>
#include <string>
#include <set>
using namespace std;

class person
{
private:
string lastName;
string firstName;
long phoneNumber;
public:
// default constructor
person() :lastName("blank"), firstName("blank"), phoneNumber(0L)
{ }
// 3-arg constructor
person(string lana, string fina, long pho) :
lastName(lana), firstName(fina), phoneNumber(pho)
{ }
friend bool operator<(const person&, const person&);
friend bool operator==(const person&, const person&);

void display() const // display person's data
{
cout << endl << lastName << ",\t" << firstName
<< "\t\tPhone: " << phoneNumber;
}
long get_phone() const // return phone number
{
return phoneNumber;
}
}; //end class person
//--------------------------------------------------------------
// overloaded < for person class
bool operator<(const person& p1, const person& p2)
{
if (p1.lastName == p2.lastName)
return (p1.firstName < p2.firstName) ? true : false;
return (p1.lastName < p2.lastName) ? true : false;
}
//--------------------------------------------------------------
// overloaded == for person class
bool operator==(const person& p1, const person& p2)
{
return (p1.lastName == p2.lastName &&
p1.firstName == p2.firstName) ? true : false;
}
//--------------------------------------------------------------
// function object to compare persons using pointers
class comparePersons
{
public:
bool operator() (const person* ptrP1, const person* ptrP2) const
{
return *ptrP1 < *ptrP2;
}
};
//--------------------------------------------------------------
//function object to display a person, using a pointer
class displayPerson
{
public:
void operator() (const person* ptrP) const
{
ptrP->display();
}
};

////////////////////////////////////////////////////////////////
int main()
{
//make persons
person* ptrP1 = new person("KuangThu", "Bruce", 4157300);
person* ptrP2 = new person("Deauville", "William", 8435150);
person* ptrP3 = new person("Wellington", "John", 9207404);
person* ptrP4 = new person("Bartoski", "Peter", 6946473);
person* ptrP5 = new person("Fredericks", "Roger", 7049982);
person* ptrP6 = new person("McDonald", "Stacey", 7764987);
person* ptrP7 = new person("KuangThu", "Bruce", 4157300);
person* ptrP8 = new person("Deauville", "William", 8435150);

//Creating multiset
multiset<person*, comparePersons> multiPtrsPers;
multiPtrsPers.insert(ptrP1);
multiPtrsPers.insert(ptrP2);
multiPtrsPers.insert(ptrP3);
multiPtrsPers.insert(ptrP4);
multiPtrsPers.insert(ptrP5);
multiPtrsPers.insert(ptrP6);
multiPtrsPers.insert(ptrP7);
multiPtrsPers.insert(ptrP8);

for_each(multiPtrsPers.begin(),multiPtrsPers.end(), displayPerson());//display person
cout << endl;

/* sort(multiPtrsPers.begin(), multiPtrsPers.end(), comparePersons());//compare persons

for_each(multiPtrsPers.begin(), multiPtrsPers.end(), displayPerson());//display persons
cout << endl;
*/
system("pause");
return 0;
} // end main()


Related Solutions

Please Read Carefully Before start answering this question. Please follow the instructions. This Question is from...
Please Read Carefully Before start answering this question. Please follow the instructions. This Question is from 'BSBFIM501 Manage budgets and financial plans' course. There are no parts missing for this Question; guaranteed!. This is the original Screenshot direct from the question. Therefore, there are nothing any further information can be provided. Thanks for your understanding and Cooperation. Please answer the following questions from the topics discussed for Prepare, implement, monitor and modify contingency plans: 1.a. Explain the process of preparing...
Please comments this C++ code and show screenshots of the outputs main.cpp #include<iostream> #include<vector> #include<string> #include"BST.h"...
Please comments this C++ code and show screenshots of the outputs main.cpp #include<iostream> #include<vector> #include<string> #include"BST.h" #include"BST.cpp" using namespace std; std::vector<std::string> tokenize(char line[]) {    std::vector<std::string> tok;        std::string word = "";        for (int i = 0; i < strlen(line); i++)        {            if (i == strlen(line) - 1)            {                word += line[i];                tok.push_back(word);                return tok;       ...
Please read carefully. Your younger sister wants to start a business that sells a product in...
Please read carefully. Your younger sister wants to start a business that sells a product in the UAE. She found a supplier in France who can provide her with one of these products: - Coffee (for 5 dhs/coffee bag) - Mascara (for 5 dhs/mascara) From your own words and understanding of entrepreneurial marketing. There is not right or wrong answer as long as you provide good reasons from your understanding of entrepreneurial marketing. 1. Which product do you advise her...
C++ Please read the question carefully and make sure that the function prototypes given are used...
C++ Please read the question carefully and make sure that the function prototypes given are used correctly for both parts. This is one whole programming assignment so please make sure that it;s answered entirely not just one part. The output example is provided at the end of the question. First , write a program to create an array and fill it up with 20 randomly generated integers between 0 to 10 and output the array. Part 1: Write a function...
C++ Please read the question carefully and match the output example given at the end! Question...
C++ Please read the question carefully and match the output example given at the end! Question In this project you will implement operator overloading for a two dimensional Vector class. Begin with the declaration of the Vector class on the final page. There are three operators that must be overloaded insertion (​<<​), addition (​+​), and subtraction (​-​). insertion (​<<​): The insertion operator is used to send the values of a vector to an ostream object, if a vector has two...
Implement a queue - C programming, please read the instruction carefully and implement queue.c using dynamic...
Implement a queue - C programming, please read the instruction carefully and implement queue.c using dynamic array structure given dynarray.h and dynarray.c below The second ADT you'll implement for this assignment is a queue. For this assignment, the interface for the queue (i.e. the structures and the prototypes of functions a user of the queue interacts with) is already defined for you in the file queue.h. Your first task in this assignment is to implement definitions for the functions that...
c++ Please read the instructions carefully. You must put the proper items in the proper file...
c++ Please read the instructions carefully. You must put the proper items in the proper file (eitehr SSandwich.h or SSandwich.cpp. Do NOT include any main function in your submission. You are encouraged to write your own main function to test what you are submitting. You are submit two files via BlackBoard: A file named SSandwich.h, a header file defining the class SSandwich. No inline methods are permitted. Any enum classes should be defined here also. Your header file should have...
c++. please read this task carefully. First, try given examples of input to your code, if...
c++. please read this task carefully. First, try given examples of input to your code, if it works, then send it here. Create structure Applicants with following fields: struct applicants{ int id; string name; string surname; int subject1,subject2,subject3,selectedSubject; string specialCase; int total; }; SpecialCase (if applicant has "Awardee of Olympiads", then this field is "true", otherwise "false"). Do not forget that all "Awardees of Olympiads" will gain grants automatically. If the total points are same your algorithm have to choose...
Write a Fraction Class in C++ PLEASE READ THE ASSINGMENT CAREFULY BEFORE YOU START, AND PLEASE,...
Write a Fraction Class in C++ PLEASE READ THE ASSINGMENT CAREFULY BEFORE YOU START, AND PLEASE, DON'T ANSWER IT IF YOU'RE NOT SURE WHAT YOU'RE DOING. I APPRECIATE IF YOU WRITE COMMENTS AS WELL. WRONG ANSWER WILL GET A DOWNVOTE Thank in Advance. Must do; The class must have 3 types of constructors; default, overloaded with initializer list, copy constructor You must overload the assignment operator You must declare the overloaded output operator as a friend rather than part of...
projectOne (Please be sure to include screenshots of the test runs for all programs) A. write...
projectOne (Please be sure to include screenshots of the test runs for all programs) A. write a program the computes nx and store the result into y You can use y = Math.pow( Mantissa, exponent) Requirements: Besides main() your program must have one method with two parameters, one double and one int n and x are positive numbers read from the keyboard if the user makes an entry that does not meet this criteria, the user must be given to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT