Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Example 12-9 defined a class personType to store the name of a person. The membe

ID: 3677498 • Letter: E

Question

Example 12-9 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 existingclass 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.

(Malik 719)

FIGURE 12-11: UML class diagram of the class personType

We now give the definitions of the member functions of the class personType.

void personType::print() const

{

    cout << firstName << "" < < lastName;

}

void personType::setName(string first, string last)

{

    firstName = first;

    lastName = last;

}

string personType::getFirstName() const

{

    return firstName;

}

string personType::getLastName() const

{

    return lastName;

}

    //constructor

personType::personType(string first, string last)

{

    firstName = first;

    lastName = last;

}

(Malik 690-691)

Malik, D.S.. C++ Programming: From Problem Analysis to Program Design. Cengage Learning, 03/2014. VitalBook file.

personType -firstName: string -lastName: string +print (): voic +setName (string, string): void +get First Name() const: string +getLastName () const: string +personType (string="", string="")

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;
}

sample output

Enter first name: Ram                                                                                                                                       
Enter middle name: chand                                                                                                                                    
Enter last name: mohan                                                                                                                                      
                                                                                                                                                            
Hello, Ram chand mohan                                                                                                                                      
                                                                                                                                                            
Enter first name to compare to Ram: mohan                                                                                                                   
The first names are different.                                                                                                                              
Enter last name to compare to mohan: ram                                                                                                                    
The last names are different.                                                                                                                               

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote