Example 10-10 defined a class personType to store the name of a person. The memb
ID: 3857990 • Letter: E
Question
Example 10-10 defined a class personType to store the name of a person.
The member functions that we included merely print the name and set the
name of a person. Redefine the class personType so that, in addition to
what the existing class does, you can:
a. Set the first name only.
b. Set the last name only.
c. Store and set the middle name.
d. Check whether a given first name is the same as the first name of this person.
e. Check whether a given last name is the same as the last name of this person.
Write the definitions of the member functions to implement the operations
for this class. Also, write a program to test various operations on this class
Functions in exercise 9 d. and e. must return a boolean indicating whether the specified names are identical or not.
Class member variables must be implemented as private.
Class declarations must be in separate .h header files (one .h header file per class) and their implementations (the executable code) in separate .cpp files (one .cpp implementation file per class)
Explanation / Answer
main.cpp
#include <iostream>
#include "PersonType.h"
int main(int argc, const char * argv[]) {
//declares personType variables
PersonType firstPerson;
//declares string variables
string fName;
string mName;
string lName;
string compareFName;
string compareLName;
//gets the first name of firstPerson
cout << "Enter first name: ";
cin >> fName;
firstPerson.setFirstName(fName);
//gets the middle name of firstPerson
cout << "Enter middle name: ";
cin >> mName;
firstPerson.setMiddleName(mName);
//gets the last name of firstPerson
cout << "Enter last name: ";
cin >> lName;
firstPerson.setLastName(lName);
//prints out the first and last name of firstPerson
cout << ' ';
cout << "Hello, ";
firstPerson.print();
cout << ' ';
//gets the first name to compare to firstPerson's first name
cout << ' ';
cout << "Enter first name to compare to " << firstPerson.getFirstName() << ": ";
cin >> compareFName;
//if statement to print whether they are the same or different
if(firstPerson.compareFirstName(compareFName))
{
cout << "The first names are the same. ";
}
else{
cout << "The first names are different. ";
}
//gets the last name to compare to the firstPerson's last name
cout << "Enter last name to compare to " << firstPerson.getLastName() << ": ";
cin >> compareLName;
//if statement to print whether they are the same or different
if(firstPerson.compareLastName(compareLName))
{
cout << "The last names are the same. ";
}
else{
cout << "The last names are different. ";
}
//end program
return 0;
}
PersonType.h
#ifndef __cit233_hw3_tony_hendrick_q2__PersonType__
#define __cit233_hw3_tony_hendrick_q2__PersonType__
#include <stdio.h>
#include <string>
using namespace std;
class PersonType {
public:
void print() const;
//Function to output the first name and last name
//in the form "firstName lastName".
void setName(string, string);
//Function to set firstName and lastName according
//to the parameters.
void setFirstName(string);
//Function to set firstName according to the parameter.
void setLastName(string);
//Function to set lastName according to the parameter.
void setMiddleName(string);
//Function to set middleName according to the parameter.
bool compareFirstName(string);
//Function to compare the entered first name according to the
//parameter to the personType object's firstName.
//Post-condition: True or false is returned depending on whether the
//first names are the same.
bool compareLastName(string);
//Function to compare the entered last name according to the
//parameter to the personType object's lastName.
//Post-condition: True or false is returned depending on whether the
//last names are the same.
string getFirstName() const;
//Function to return the first name.
//Postcondition: The value of firstName is returned.
string getLastName() const;
//Function to return the last name.
//Postcondition: The value of lastName is returned.
PersonType(string first = "", string last = "");
//Constructor
//Sets firstName and lastName according to the parameters.
//The default values of the parameters are null strings.
//Postcondition: firstName = first; lastName = last;
private:
string firstName; //variable to store the first name
string lastName; //variable to store the last name
string middleName; //variable to store the middle name
};
#endif /* defined(__cit233_hw3_tony_hendrick_q2__PersonType__) */
PersonType.cpp
#include "PersonType.h"
#include <string>
#include <iostream>
using namespace std;
void PersonType::print() const
{
cout << firstName << " " << middleName << " " << lastName;
}
void PersonType::setName(string first, string last)
{
firstName = first;
lastName = last;
}
void PersonType::setFirstName(string f)
{
firstName = f;
}
void PersonType::setLastName(string l)
{
lastName = l;
}
void PersonType::setMiddleName(string m)
{
middleName = m;
}
string PersonType::getFirstName() const
{
return firstName;
}
string PersonType::getLastName() const
{
return lastName;
}
bool PersonType::compareFirstName(string fn)
{
return fn == firstName;
}
bool PersonType::compareLastName(string ln)
{
return ln == lastName;
}
//constructor
PersonType::PersonType(string first, string last)
{
firstName = first;
lastName = last;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.