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
#include<iostream>
using namespace std;
class Person{
private:
int height;
double weight;
string name;
char gender;
string race;
string occupation;
public:
Person();
Person(int,double,string,char,
string,string);
void setHeight(int );
void setWeight(double);
void setName(string);
void setGender(char);
void setRace(string);
void setOccupation(string);
int getHeight();
double getWeight();
string getName();
char getGender();
string getRace();
string getOccupation();
void printInfo();
};
person.cpp
#include "person.h"
Person::Person(){
height = 0;
weight = 0;
name = "";
gender = ' ';
race = "";
occupation = "";
}
Person::Person(int h,double w,string n,char g, string r,string
o){
height = h;
weight = w;
name = n;
gender = g;
race = r;
occupation = o;
}
void Person::setHeight(int h){
height = h;
}
void Person::setWeight(double w){
weight = w;
}
void Person::setName(string n){
name = n;
}
void Person::setGender(char g){
gender = g;
}
void Person::setRace(string r){
race = r;
}
void Person::setOccupation(string o){
occupation = o;
}
int Person::getHeight(){
return height;
}
double Person::getWeight(){
return weight;
}
string Person::getName(){
return name;
}
char Person::getGender(){
return gender;
}
string Person::getRace(){
return race;
}
string Person::getOccupation(){
return occupation;
}
void Person::printInfo(){
cout<<"Height : "<<height<<"
inches\n";
cout<<"Weight : "<<weight<<"
pounds\n";
cout<<"Name : "<<name<<endl;
cout<<"Gender :
"<<gender<<endl;
cout<<"Race : "<<race<<endl;
cout<<"Occupation :
"<<occupation<<endl;
}
main.cpp
#include "person.h"
#include<vector>
int main(){
vector<Person> vec(4);
int height;
double weight;
string name;
char gender;
string race;
string occupation;
for(int i=0;i<4;i++){
cout<<"Enter height in inches
: ";
cin>>height;
vec[i].setHeight(height);
cout<<"Enter weight in pounds
: ";
cin>>weight;
vec[i].setWeight(weight);
cout<<"Enter gender :
";
cin>>gender;
vec[i].setGender(gender);
cout<<"Enter name : ";
cin.get();
getline(cin , name);
vec[i].setName(name);
cout<<"Enter race : ";
getline(cin , race);
vec[i].setRace(race);
cout<<"Enter occupation :
";
getline(cin , occupation);
vec[i].setRace(occupation);
}
vector<Person>::iterator ptr;
for (ptr = vec.begin(); ptr < vec.end();
ptr++){
ptr->printInfo();
cout << "\n";
}
vec.clear();
return 0;
}
Get Answers For Free
Most questions answered within 1 hours.