Question: The Person Class Write a Person class in C++, with separate person.h and person.cpp files. Your Person class should have the following private data members:

The Person Class

Write a Person class in C++, with separate person.h and person.cpp files. Your Person class should have the following private data members: first name (string) last name (string) gender (character) age (integer) height (double) likes monster movies (bool) likes cantaloupe (bool) In addition, your Person class should have the following member functions: A default constructor. You can decide what to initialize the private data members to. You do not need to add getters and setters for every data member. Instead, only add accessor functions that you actually use in your main(). An input function that reads in the data members, in the order listed above. The function should allow the user to send in either the standard input stream or a file input stream, depending on the user's choice. An output function that writes the data members, in the order listed above. Provide appropriate text so reading the output makes sense. The function should allow the user to send in either the standard output stream or a file output stream, depending on the user's choice. A validate function that tests whether a person's data meets your criteria for a potential coding partner. You should choose the gender (male or female), with an age between 18 and 40 (inclusive), the height under 7.5 feet (else the 'person' is likely a monster), and someone who likes to eat cantaloupe while watching monster movies. NOTE: you can assume the file you are reading in is correct, e.g., only 'M' or 'F' is given for gender. You may want to consider NotePad or WordPad on your computer to write your code for your Person class. Once you feel good about what you've written, copy the code into Visual Studio. Doing this will help you see where you might make mistakes on upcoming exams.

Your main() Requirements:

Before we allocate space for our persons, we need to find out how many we need. First, we'll read through the file, counting how many valid persons there are. Then, we'll iterate through the file again, this time reading the valid persons into a dynamically allocated array.

As usual, we recommend you use the "Incremental Build" software development model. To encourage this use, we break down your main() requirements into steps. We suggest you throughly test each step before moving to the next step. Step 1: How many valid people are in the file?

Create an integer called validCount, and a pointer to this integer. To receive full credit for this assignment, you must ONLY access this variable for counting through the pointer to your counting variable. (We know, kind of silly ... but the point is to give you more pointer practice!) Create a Person object. Open the file "PersonFile.dat". Until the end of file, read each person within the file (using your Person's input() function), and test whether the Person details read in are valid or not (using your Person's validate() function). If valid, add one to validCount (via your pointer to validCount). Take a moment to print the number of valid People in the file. It should be an odd number, between 12 and 14. Step 2: Add valid people to your new people array.

Use dynamic memory to allocate an array of people on the heap that is large enough to hold the number of valid people determined in Step 1. Do not over-allocate the array to hold all the people in the data file. We now need to rewind the reading location of PersonFile.dat to the beginning of the file. To do this, you could close() the file, and then re-open it; but doesn't that seem a bit foolish? Thus, instead, use clear() and seekg(0,ios::beg) functions on your input file stream. These two functions will clear the 'end of file reached' and set the next position to read from equal to the top of the file. Declare a new integer variable with initial value zero; this variable will represent the current element offset inside your dynamic array. In a loop (until the end of file): Read each person within the file (using your Person's input() function). Test whether the Person details read in are valid or not (using your Person's validate() function). If valid, assign that Person to the array (and increment your new integer variable). If not valid, print that Person's details to the screen (using your Person's output() function). There are exactly 19 invalid people in the data file. At this point you will know by the program output whether you have found them all. Step 3: Sorting your people array.

We now want to sort your valid people array. Lecture 25 discussed the SelectionSort() algorithm (a simple sorting algorithm), and provided brief pseudocode for implementing it. A few more details are provided here. Put the function prototypes above your main() and function implementations below. Create these prototypes and function headers so that they operate on your People array, with the end goal of sorting your valid People array by each person's height. For full credit, you should pass a pointer to the array to your SelectionSort() function, and use this pointer within the function to access array elements. However, we suggest you first implement the sorting feature using the [ ] notation that you are familiar with, and then (if desired) modify the function to use pointers (not [ ] ). NOTE: getting the function to work with [ ] is worth one point; getting the function to work with * is worth two points. Step 4: Print a few valid people.

Using your Person's output() function, print the following three valid people: the person with the smallest height (first element in your array) the person with the median height (the middle element in your array; we've already confirmed that the number of elements is odd) the person with the largest height (last element in your array) Step 5: Good practice.

There are two things you should do to end the program. First, close your file input stream. Second, free the memory allocated by your program to hold the valid people. Be sure to use the delete[] operation, not delete without [] .

PersonFile.txt

Tracy Isme F 32 5.5956 1 1 Iam Notreal M 37 6.5 1 1 Sparkle Freak F 40 3.45678912 1 1 Sunset Thecat F 27 4.4 1 1 Iam Funny F 1 7.77785 1 0 Cantaloupe Hater M 28 3.2 1 1 Happy Face M 18 5.5 1 1 John Theboy F 19 5.34 1 1 Jenny Thegirl M 21 6.888889 1 1 Taylor Twist F 25 5.7 0 1 La Evilness M 19 2.1 1 1 Johnny Depth M 39 6.1 1 0 Emma Isthe-Best F 18 4.94 1 1 Vampire Googles F 19 10.9 1 1 Hyphen Tastic-Man M 19 4.6 1 1 Velor Mcginnus F 18 6.67 1 1 Jane Tryon F 25 4.1 1 1 Max Camps-Inthewoods M 36 5.7 1 1 Xavior Henrytar M 67000 10.555697 1 0 Iam Fromthemovieyouarescaredof F 21 5.67 1 1 Lord Awesomeness F 19 4.4945 1 1 Glen Overthehouse M 40 6.5 1 1 Kyle Not M 22 4.6 1 1 You Dont-Know-Me M 180900 5.5 0 0 Selena Gumballs F 30 3.3 1 1 Themonster Underthebed F 19 2.8 1 1 Thebunny Outside M 18 1.5 1 1 Haveyou Seenmeyet M 28 6.9874321 1 1 Summer Falls F 26 2.6 1 1 Iam A-MALE M 29 6.6236428 1 1 Kathy Jack F 26 5.09 1 1 Xanthous Periwinkle M 19 7.3 1 1 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!