C++ * Please be sure that your code is structured into the following files: - Pe
ID: 3750133 • Letter: C
Question
C++
* Please be sure that your code is structured into the following files:
- PersonType.h
- PersonType.cpp
- PersonTypeMain.cpp
- makefile
_________________________________________________________________
*Please include user input
- Add more member functions on the class extPersonType.
The following functions are needed to be added:
– = assign one object to the other
– == check if two objects are equal
– A copy constructor
Write a C++ program to test your design. Moreover, test your design by creating pointers.
CODE:
#include <iostream>
using namespace std;
class addressType
{
protected:
string street, city, state, zip;
public:
addressType(string s, string c, string st, string z)
{
street = s;
city = c;
state = st;
zip = z;
}
void setStreet(string s)
{
street = s;
}
void setCity(string c)
{
city = c;
}
void setState(string s)
{
state = s;
}
void setZip(string z)
{
zip = z;
}
void print()
{
cout << "Street: " << street << endl;
cout << "City: " << city << endl;
cout << "State: " << state << endl;
cout << "ZIP: " << zip << endl;
}
};
class personType
{
protected:
string fname, lname;
public:
personType(string fn, string ln)
{
fname = fn;
lname = ln;
}
void setFirstName(string fn)
{
fname = fn;
}
void setLastName(string ln)
{
lname = ln;
}
void print()
{
cout << "First Name: " << fname << endl;
cout << "Last Name: " << lname << endl;
}
};
class dataType
{
protected:
int day, month, year;
public:
dataType(int d, int m, int y)
{
day = d;
month = m;
year = y;
}
void setDay(int d)
{
day = d;
}
void setMonth(int m)
{
month = m;
}
void setYear(int y)
{
year = y;
}
void print()
{
cout << "Day: " << day << endl;
cout << "Month: " << month << endl;
cout << "Year: " << year << endl;
}
};
class extPersonType : public addressType, public personType, public dataType
{
private:
string person, phoneNumber;
public:
extPersonType(string p, string pn, string s, string c, string st, string z, string fn, string ln, int d, int m, int y) : addressType(s, c, st, z), personType(fn, ln), dataType(d, m, y)
{
person = p;
phoneNumber = pn;
}
void setPerson(string p)
{
person = p;
}
void setPhoneNumber(string pn)
{
phoneNumber = pn;
}
void print()
{
cout << " ";
cout << "The person details are: " << endl << endl;
cout << "First Name: " << fname << endl;
cout << "Last Name: " << lname << endl;
cout << "Street: " << street << endl;
cout << "City: " << city << endl;
cout << "State: " << state << endl;
cout << "ZIP: " << zip << endl;
cout << "Day: " << day << endl;
cout << "Month: " << month << endl;
cout << "Year: " << year << endl;
cout << "Person Type: " << person << endl;
cout << "Phone Number: " << phoneNumber << endl << endl;
}
};
int main()
{
extPersonType person("Friend", "361-825-5700", "6300 Ocean Dr.", "Corpus Christi", "Texas", "78412", "Justin", "Saenz", 4, 9, 2018);
person.print();
}
Explanation / Answer
ScreenShot
Program
PersonType.h
#pragma once
#include <iostream>
#include<string>
using namespace std;
/*Create a class address*/
class addressType
{
//Member variables
protected:
string street, city, state, zip;
//Member functions
public:
//Default constructor
addressType();
//Parameterised constructor
addressType(string s, string c, string st, string z);
//Mutators
void setStreet(string s);
void setCity(string c);
void setState(string s);
void setZip(string z);
//Print
void print();
};
/*Create a class personType*/
class personType
{
//Member variables
protected:
string fname, lname;
//Member functions
public:
//Default constructor
personType();
personType(string fn, string ln);
void setFirstName(string fn);
void setLastName(string ln);
void print();
};
/*Create a class dataType*/
class dataType
{
//member variables
protected:
int day, month, year;
//Member functions
public:
//Default constructor
dataType();
//Parameterised constructor
dataType(int d, int m, int y);
//Mutators
void setDay(int d);
void setMonth(int m);
void setYear(int y);
//Print
void print();
};
/*Create an extension class which inherits the above classes*/
class extPersonType : public addressType, public personType, public dataType
{
//Member variables
private:
string person, phoneNumber;
//Member functions
public:
//Constructor
extPersonType(string p, string pn, string s, string c, string st, string z, string fn, string ln, int d, int m, int y) : addressType(s, c, st, z), personType(fn, ln), dataType(d, m, y) {
person = p;
phoneNumber = pn;
}
//Mutators
void setPerson(string p);
void setPhoneNumber(string pn);
//print
void print();
//Overload assignment operator
void operator = (const extPersonType &ept);
//Overload logical operator
bool operator==(extPersonType ept);
// Copy constructor
extPersonType(const extPersonType &ept);
};
PersonType.cpp
#include "PersonType.h"
/*Class address types function definition*/
addressType::addressType() {
street = "";
city = "";
state = "";
zip="";
}
addressType::addressType(string s, string c, string st, string z){
street = s;
city = c;
state = st;
zip = z;
}
void addressType::setStreet(string s)
{
street = s;
}
void addressType::setCity(string c) {
city = c;
}
void addressType::setState(string s) {
state = s;
}
void addressType::setZip(string z) {
zip = z;
}
void addressType::print() {
cout << "Street: " << street << endl;
cout << "City: " << city << endl;
cout << "State: " << state << endl;
cout << "ZIP: " << zip << endl;
}
/*Person type class definition*/
personType::personType() {
fname = "";
lname = "";
}
personType::personType(string fn, string ln) {
fname =fn;
lname =ln;
}
void personType::setFirstName(string fn) {
fname = fn;
}
void personType::setLastName(string ln) {
lname = ln;
}
void personType::print(){
cout << "First Name: " << fname << endl;
cout << "Last Name: " << lname << endl;
}
/*Dat type class functions definition*/
//Default constructor
dataType::dataType() {
day = 1;
month = 1;
year = 1;
}
//Parameterised constructor
dataType::dataType(int d, int m, int y){
day = d;
month = m;
year = y;
}
//Mutators
void dataType::setDay(int d){
day = d;
}
void dataType::setMonth(int m){
month = m;
}
void dataType::setYear(int y){
year = y;
}
//Print
void dataType::print(){
cout << "Day: " << day << endl;
cout << "Month: " << month << endl;
cout << "Year: " << year << endl;
}
/*Function definition of child class*/
//Mutators
void extPersonType::setPerson(string p){
person = p;
}
void extPersonType::setPhoneNumber(string pn){
phoneNumber = pn;
}
//print
void extPersonType::print()
{
cout << " ";
cout << "The person details are: " << endl << endl;
cout << "First Name: " << fname << endl;
cout << "Last Name: " << lname << endl;
cout << "Street: " << street << endl;
cout << "City: " << city << endl;
cout << "State: " << state << endl;
cout << "ZIP: " << zip << endl;
cout << "Day: " << day << endl;
cout << "Month: " << month << endl;
cout << "Year: " << year << endl;
cout << "Person Type: " << person << endl;
cout << "Phone Number: " << phoneNumber << endl << endl;
}
//Overload assignment operator
void extPersonType::operator = (const extPersonType &ept) {
person = ept.person;
phoneNumber = ept.phoneNumber;
}
//Overload logical operator
bool extPersonType::operator==(extPersonType ept) {
if (this->person == ept.person) {
if (this->phoneNumber == ept.phoneNumber) {
return true;
}
}
else {
return false;
}
}
// Copy constructor
extPersonType::extPersonType(const extPersonType &ept) {
person = ept.person;
phoneNumber = ept.phoneNumber;
street = ept.street;
city = ept.city;
state = ept.state;
zip = ept.zip;
day = ept.day;
month = ept.month;
year = ept.year;
fname = ept.fname;
lname = ept.lname;
}
PersonTypeMain.cpp
#include "stdafx.h"
#include "PersonType.h"
using namespace std;
//Test method
int main()
{
//First Test
cout << "Test1 :" << endl;
extPersonType person("Friend", "361-825-5700", "6300 Ocean Dr.", "Corpus Christi", "Texas", "78412", "Justin", "Saenz", 4, 9, 2018);
person.print();
extPersonType person1 = person;
person1.print();
if (person == person1) {
cout << "Both persons are same" << endl;
}
else {
cout << "Both persons are not same" << endl;
}
extPersonType person2 = person;
person2.print();
//second Test
cout << "Test2 :" << endl;
//User input
string fname, lname, street, city, state, zip, personStat, phone;
int day, month, year;
cout << "Enter the first name:";
cin >> fname;
cout << "Enter the last name:";
cin >> lname;
cin.ignore();
cout << "Enter the street name:";
getline(cin,street);
cout << "Enter the city name:";
getline(cin, city);
cout << "Enter the state name:";
getline(cin, state);
cout << "Enter the zip:";
cin >> zip;
cout << "Enter date(day month year):";
cin >> day >> month >> year;
cout << "Enter person status:";
cin>> personStat;
cout << "Enter phone:";
cin >> phone;
extPersonType p(personStat, phone, street, city, state, zip, fname, lname, day, month, year);
p.print();
}
--------------------------------------------
Output
Test1 :
The person details are:
First Name: Justin
Last Name: Saenz
Street: 6300 Ocean Dr.
City: Corpus Christi
State: Texas
ZIP: 78412
Day: 4
Month: 9
Year: 2018
Person Type: Friend
Phone Number: 361-825-5700
The person details are:
First Name: Justin
Last Name: Saenz
Street: 6300 Ocean Dr.
City: Corpus Christi
State: Texas
ZIP: 78412
Day: 4
Month: 9
Year: 2018
Person Type: Friend
Phone Number: 361-825-5700
Both persons are same
The person details are:
First Name: Justin
Last Name: Saenz
Street: 6300 Ocean Dr.
City: Corpus Christi
State: Texas
ZIP: 78412
Day: 4
Month: 9
Year: 2018
Person Type: Friend
Phone Number: 361-825-5700
Test2 :
Enter the first name:David
Enter the last name:Jane
Enter the street name:1022 Tax gr.
Enter the city name:Varcity
Enter the state name:Texas
Enter the zip:76412
Enter date(day month year):6 2 2017
Enter person status:Colleage
Enter phone:651-542-5800
The person details are:
First Name: David
Last Name: Jane
Street: 1022 Tax gr.
City: Varcity
State: Texas
ZIP: 76412
Day: 6
Month: 2
Year: 2017
Person Type: Colleage
Phone Number: 651-542-5800
Press any key to continue . . .
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.