Question

In: Computer Science

You are to create a class called Person. You should create the definition of the class...

You are to create a class called Person. You should create the definition of the class Person in a file person.h and the functions for Person in person.cpp. You will also create a main program to be housed in personmain.cpp.

A Person will have attributes of

  • Height (in inches)
  • Weight (in pounds to 2 decimal places)
  • Name (string)
  • Gender (‘m’ or ‘f’)
  • Ethnicity/Race (could be more than one word)
  • Occupation (also more than a single word)

A Person will have the following methods

  • Accessors for all attributes
  • Mutators for all attributes
  • A default constructor that sets all data elements to 0 or blank text as appropriate
  • A fully specified constructor that allows defining values for all attributes
  • A printinfo method that will display all the info for a person in a logical and well formatted way including labels for each of the attribute fields

personmain.cpp should perform the following actions

  • Dynamically allocate an array of 4 Persons
  • Prompt the user for information about each of the four persons and store that information in one of the array elements
  • Print the information for each Person
  • Clean up all allocated space

Solutions

Expert Solution

// person.h
#ifndef PERSON_H
#define PERSON_H

#include <iostream>
#include <string>

using namespace std;

class Person
{
private:
string name;
int height;
double weight;
char gender;
string race;
string occupation;

public:
Person();
Person(string name, int height, double weight, char gender, string race, string occupation);
void setName(string name);
void setHeight(int height);
void setWeight(double weight);
void setGender(char gender);
void setRace(string race);
void setOccupation(string occupation);
string getName();
int getHeight();
double getWeight();
char getGender();
string getRace();
string getOccupation();
void printInfo();
};

#endif

// end of person.h

// person.cpp

#include "person.h"
#include <iomanip>

// default constructor
Person::Person() : name(""), height(0), weight(0), gender('\0'), race(""), occupation("")
{}

// parameterized constructor
Person::Person(string name, int height, double weight, char gender, string race, string occupation)
: name(name), height(height), weight(weight), gender(gender), race(race), occupation(occupation)
{}

// setters
void Person:: setName(string name)
{
this->name = name;
}

void Person:: setHeight(int height)
{
this->height = height;
}

void Person:: setWeight(double weight)
{
this->weight = weight;
}

void Person:: setGender(char gender)
{
this->gender = gender;
}

void Person:: setRace(string race)
{
this->race = race;
}

void Person:: setOccupation(string occupation)
{
this->occupation = occupation;
}

// getters
string Person:: getName()
{
return name;
}

int Person:: getHeight()
{
return height;
}

double Person:: getWeight()
{
return weight;
}

char Person:: getGender()
{
return gender;
}

string Person:: getRace()
{
return race;
}

string Person:: getOccupation()
{
return occupation;
}

// display details of person
void Person:: printInfo()
{
cout<<"Name: "<<name<<endl;
cout<<"Height: "<<height<<" inches"<<endl;
cout<<"Weight:"<<fixed<<setprecision(2)<<weight<<" pounds"<<endl;
cout<<"Gender: "<<gender<<endl;
cout<<"Race: "<<race<<endl;
cout<<"Occupation: "<<occupation<<endl<<endl;
}

// end of person.cpp

// personmain.cpp

#include "person.h"
#include <iostream>
using namespace std;

int main()
{
// create an array of 4 persons
Person *persons = new Person[4];
string name, race, occupation;
int height;
double weight;
char gender;

// loop to input details of 4 persons
for(int i=0;i<4;i++)
{
cout<<"Enter details for Person-"<<(i+1)<<endl;
cout<<"Name: ";
getline(cin,name);
cout<<"Height (in inches): ";
cin>>height;
cout<<"Weight (in pounds): ";
cin>>weight;
cout<<"Gender (M/F): ";
cin>>gender;
cout<<"Race: ";
cin.ignore(100,'\n');
getline(cin, race);
cout<<"Occupation: ";
getline(cin, occupation);
persons[i].setGender(gender);
persons[i].setHeight(height);
persons[i].setName(name);
persons[i].setOccupation(occupation);
persons[i].setRace(race);
persons[i].setWeight(weight);
}

// display the details of persons
cout<<"Details of all persons:"<<endl;
for(int i=0;i<4;i++)
{
persons[i].printInfo();
}

// delete the memory allocated
delete[] persons;

return 0;
}

// end of personmain.cpp

Output:


Related Solutions

Create a file called grocery.ts. It should have a definition of a class with the obvious...
Create a file called grocery.ts. It should have a definition of a class with the obvious name Grocery. The class should have some basic attributes such as name, quantity, etc. Feel free to add any other attributes you think will be necessary. Add few grocery items to an array of groceries, such as milk, bread, and eggs, along with some quantities (i.e. 3, 6, 11). Display these grocery items as HTML output. The output of this assignment will be grocery.ts...
Write a Java class called Person. The class should have the following fields: A field for...
Write a Java class called Person. The class should have the following fields: A field for the person’s name. A field for the person’s SSN. A field for the person’s taxable income. A (Boolean) field for the person’s marital status. The Person class should have a getter and setter for each field. The Person class should have a constructor that takes no parameters and sets the fields to the following values: The name field should be set to “unknown”. The...
1- Write a class called MedicalStaff that is a Person (the class that you wrote in...
1- Write a class called MedicalStaff that is a Person (the class that you wrote in last lab). A MedicalStaff has specialty (i.e. Orthopedic, cardiology, etc.). 2- Then write two classes: Doctor class has office visit fee. Nurse class has title (i.e. RN, NP, etc.) Both classes inherit from MedicalStaff. Be sure these are all complete classes, including toString method. 3- Write a tester to test these classes and their methods, by creating an array or ArrayList of Person and...
In C++ Define a base class called Person. The class should have two data members to...
In C++ Define a base class called Person. The class should have two data members to hold the first name and last name of a person, both of type string. The Person class will have a default constructor to initialize both data members to empty strings, a constructor to accept two string parameters and use them to initialize the first and last name, and a copy constructor. Also include appropriate accessor and mutator member functions. Overload the operators == and...
C++ Define a base class called Person. The class should have two data members to hold...
C++ Define a base class called Person. The class should have two data members to hold the first name and last name of a person, both of type string. The Person class will have a default constructor to initialize both data members to empty strings, a constructor to accept two string parameters and use them to initialize the first and last name, and a copy constructor. Also include appropriate accessor and mutator member functions. Overload the operators == and !=...
for java Welcome to a classic homework problem! Create a public class called Last8. You should...
for java Welcome to a classic homework problem! Create a public class called Last8. You should exposed two public methods: add: adds a value, does not return a value last: returns an array containing the last 8 values that were added, in any order. You do not need a constructor, but you can add an empty one if you need. Until 8 values have been added you should return 0s in their place. For example, here's how an instance of...
OOPDA in java project. in eclipse Instructions: Create a class called Person with the properties listed:...
OOPDA in java project. in eclipse Instructions: Create a class called Person with the properties listed: int id, String firstName, String middleName, String lastName, String email, String ssn, int age. The Person class should have getters and setters for all properties other than id. (You may use Eclipse to help you auto-generate these.) Person class should also have a getter for id Create a no-arg constructor for Person In addition to these getters and setters, Person should have the following...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the following variables: an instance variable that describes the title - String an instance variable that describes the ISBN - String an instance variable that describes the publisher - String an instance variable that describes the price - double an instance variable that describes the year – integer Provide a toString() method that returns the information stored in the above variables. Create the getter and...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the following variables: an instance variable that describes the title - String an instance variable that describes the ISBN - String an instance variable that describes the publisher - String an instance variable that describes the price - double an instance variable that describes the year – integer Provide a toString() method that returns the information stored in the above variables. Create the getter and...
You are to create a class called ManageDB. The ManageDB class will have a 2 input...
You are to create a class called ManageDB. The ManageDB class will have a 2 input constructor that has the following definition: ManageDB(int number, String fileName) The constructor will create an array of EmployeeDB objects of length "number". The constructor will read the file located at "fileName" and extract the information from that file to populate a database of EmployeeDB objects. The file contains information on the EmployeeDB objects. This information is contained in records. The format of each record...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT