Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Source: https://github.com/Seneca-244200/OOP-Workshops/tree/main/WS09 PART 1 (100%) The Person class and Utils module (fully implemented) The Person class encapsulates a person with first, middle and last name

Source: https://github.com/Seneca-244200/OOP-Workshops/tree/main/WS09

PART 1 (100%)

The Person class and Utils module (fully implemented)

The Person class encapsulates a person with first, middle and last name and is instantiated in an empty state.

All the name fields are stored dynamically and are read from istream through comma-separated data entry.

The C-string manipulation and dynamic memory allocation logic are provided by the Utils module.

A Person is valid if it has valid first and last names. The middle name is optional, therefore the data entry for a Person can be done in the following two ways:

1- A Person with a middle name Data entry:

Homer,Jay,Simpson 

Displayed as follows:

Homer Jay Simpson 

2- A Person without a middle name Data entry:

Lisa,,Simpson 

Displayed as follows

Lisa Simpson 

Walk through the Person class using the nameTester.cpp program; study and understand it.

The Contact class

Create a Class Module called Contact, derived from the Person class. A contact is a person with an address.

The Contact class stores the address in four attributes:

  1. Address (Dynamic, unknown length)
  2. City (Dynamic, unknown length)
  3. Province: stored by two characters (for the province code, like ON, AL, BC, etc...)
  4. Postal code: stored in six characters.

Like a Person, Contact is instantiated in an empty state and later is populated from istream using comma-separated values. All the attributes defined in Contact are mandatory and can not be empty or not provided.

A Contact is read from istream as follows:

 Homer,Jay,Simpson 70 the pond road,North York,ON,M3J3M6 OR Homer,,Simpson 70 the pond road,North York,ON,M3J3M6 

And it is displayed as follows:

 Homer Jay Simpson 70 the pond road North York ON M3J 3M6 OR Homer Simpson 70 the pond road North York ON M3J 3M6 

If any data other than the middle name is missing or exceeds the field length, the Contact should be put in an invalid state.

Displaying an invalid Contact object will be quietly ignored and no action will be taken.

the Contact class implementation

  • Initialize all the attributes to their default empty states in the class declaration. (as a result your defualt constructor should be empty)
  • Use the Utils functions for your C-string work and dynamic memory allocation. (do not include )
  • Invoke the base class's constructors and Methods in copy construction and copy assignment to make sure the base class's resources are managed properly.
  • Override all the virtual functions and virtual operators of the Person class and implement the rule of three.
  • Apart from the overridden methods and operators, you can create any additional method to help you implement this class.

Tester program

/*********************************************************************** // OOP244 Workshop 9: // File w9_tester.cpp // Version 1.0 // Date 2021/11/19 // Author Fardad Soleimanloo // Description // // Revision History // ----------------------------------------------------------- // Name Date Reason ///////////////////////////////////////////////////////////////// ***********************************************************************/ #include #include #include "Contact.h" using namespace std; using namespace sdds; Contact readContact(ifstream& ifstr) { Contact fC; ifstr >> fC; return fC; } int main() { Contact C; ifstream file("contacts.txt"); cout << "Empty Contact: >" << C << "<" << endl; cout << "Enter the following:" << endl << "Homer,Jay,Simpson" << endl << "70 the pond road,North York,ON,M3J3M6" << endl << endl; cout << "Name and address" << endl << "> "; cin >> C; if (cin) cout << "OK!" << endl; else { cout << "Date entry implemented incorrectly" << endl; cin.clear(); cin.ignore(1000, ' '); } cout << "Contact:" << endl << C << endl << endl; //--------------------------------------- cout << "Enter the following:" << endl << "Homer,Jay,Simpson" << endl << "70 the pond road,North York,ONT,M3J3M6" << endl << endl; cout << "Name and address" << endl << "> "; cin >> C; if (cin) cout << "Date entry implemented incorrectly" << endl; else { cin.clear(); cin.ignore(1000, ' '); } cout << "Empty Contact: >" << C << "<" << endl << endl; //--------------------------------------- cout << "Enter the following:" << endl << "Homer,Jay,Simpson" << endl << "70 the pond road,North York,ON,M3J 3M6" << endl << endl; cout << "Name and address" << endl << "> "; cin >> C; if (!cin){ cin.clear(); cin.ignore(1000, ' '); } cout << "Empty Contact: >" << C << "<" << endl << endl; //--------------------------------------- cout << "Enter the following:" << endl << "Homer,Jay,Simpson" << endl << "70 the pond road,,ON,M3J3M6" << endl << endl; cout << "Name and address" << endl << "> "; cin >> C; if (!cin){ cin.clear(); cin.ignore(1000, ' '); } cout << "Empty Contact: >" << C << "<" << endl << endl; //--------------------------------------- cout << "Enter the following:" << endl << "Homer,Jay,Simpson" << endl << ",North York,ON,M3J3M6" << endl << endl; cout << "Name and address" << endl << "> "; cin >> C; if (!cin){ cin.clear(); cin.ignore(1000, ' '); } cout << "Empty Contact: >" << C << "<" << endl << endl; cout << "Name and addresses in file: " << endl; while (file) { C = readContact(file); if (file) cout << C << endl; } return 0; }

output

Empty Contact: >< Enter the following: Homer,Jay,Simpson 70 the pond road,North York,ON,M3J3M6 Name and address > Homer,Jay,Simpson 70 the pond road,North York,ON,M3J3M6 OK! Contact: Homer Jay Simpson 70 the pond road North York ON M3J 3M6 Enter the following: Homer,Jay,Simpson 70 the pond road,North York,ONT,M3J3M6 Name and address > Homer,Jay,Simpson 70 the pond road,North York,ONT,M3J3M6 Empty Contact: >< Enter the following: Homer,Jay,Simpson 70 the pond road,North York,ON,M3J 3M6 Name and address > Homer,Jay,Simpson 70 the pond road,North York,ON,M3J 3M6 Empty Contact: >< Enter the following: Homer,Jay,Simpson 70 the pond road,,ON,M3J3M6 Name and address > Homer,Jay,Simpson 70 the pond road,,ON,M3J3M6 Empty Contact: >< Enter the following: Homer,Jay,Simpson ,North York,ON,M3J3M6 Name and address > Homer,Jay,Simpson ,North York,ON,M3J3M6 Empty Contact: >< Name and addresses in file: Homer Jay Simpson 70 the pond road North York ON M3J 3M6 Fred Soley 1 York Gate Blvd North York ON M3N 3A1 John Al Doe 1750 Finch Ave E North York ON M2J 2X5 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Dental Assisting

Authors: Doni Bird, Debbie Robinson

13th Edition

978-0323624855, 0323624855

Students also viewed these Programming questions