In: Computer Science
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
A Person will have the following methods
personmain.cpp should perform the following actions
// 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: