Question: C++ Implementing an overloaded class in an STL List Modify the Person class to be overloaded for >,
C++
Implementing an overloaded class in an STL List
Modify the Person class to be overloaded for >, <, =, ==.
Designate ostream as a friend to Person.
Now use the STL List and use Person as the template argument.
Documentation on STL List can be found at:
http://www.cplusplus.com/reference/list/list/
code with a good overload should produce these results:
Test of Person in the STL List
Harry Potter 934-888-2323 != Hermione Granger 444-678-8899
Entry Sequence Order
Harry Potter 934-888-2323
Hermione Granger 444-678-8899
Ronald Weasley 788-909-4566
Luna Lovegood 230-675-7811
Sorted Order
Hermione Granger 444-678-8899
Luna Lovegood 230-675-7811
Harry Potter 934-888-2323
Ronald Weasley 788-909-4566
Note: Designate ostream as a friend to Person class
Use the Person class for the template of the STL List
Person class is provided below
Please comment code!
main.cpp
#include "stdafx.h"
#include
#include
#include
#include "Person.h"
using namespace std;
void listP(list);
int _tmain(int argc, _TCHAR* argv[])
{
//test of list of Person instances
list contacts;
Person x("Harry", "Potter", "934-888-2323");
contacts.push_back(x);
Person y("Hermione", "Granger", "444-678-8899");
contacts.push_back(y);
//if (x == y) //needs implementation
// cout << x << " == " << y << endl;
//else
// cout << x << " != " << y << endl;
x.Update("Ronald", "Weasley", "788-909-4566");
contacts.push_back(x);
x.Update("Luna", "Lovegood", "230-675-7811");
contacts.push_back(x);
cout <<" Entry Sequence Order ";
listP(contacts);
// contacts.sort(); //needs implementation
cout <<" Sorted Order ";
listP(contacts);
return 0;
}
void listP(list l)
{
for(list ::iterator i=l.begin(); i != l.end(); i++)
// cout << *i << " "; //needs implementation
cout << "temp line ";
cout <
return;
}
Person.cpp
//Implementation file for Person class
#include "stdafx.h"
using namespace std;
#include "Person.h"
#include
#include
using namespace std;
/*
default constructor
@pre none
@post Person class instance
*/
Person::Person() {
first = "";
last = "";
phone = "";
}
/*
parameterized constructor
@pre none
@post Person class instance
@param fname, string - first name
@param lname, string - last name
@param phone, string - phone number
*/
Person::Person(string fname, string lname, string iphone)
{
first = fname;
last = lname;
phone = iphone;
}
/*
Display - formatted display of Person instance
@pre Person class instance
@post Formatted display to screen.
@param none
@return void
*/
void Person::Display() {
cout <<"Name is " << first << " " << last <
cout <<"Phone is " << phone << endl;
return;
} // end Display()
/*
Update - update all fields of Person instance
@pre Person class instance
@post Person class instance
@param fname, string - first name
@param lname, string - last name
@param phone, string - phone number
@return void
*/
void Person::Update(string fname, string lname, string iphone) {
first = fname;
last = lname;
phone = iphone;
return;
} // end Update()
// end Person implementation
Person.h
#ifndef PERSON_H
#define PERSON_H
#include "stdafx.h"
using namespace std; // if using includes make sure shared namespace
#include // must be included to use strings.
// specification for Person class
//
//
class Person
{
public:
Person();
Person(string, string, string);
void Update(string, string, string);
void Display();
private :
string first;
string last;
string phone;
};
#endif
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
