Question

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

  • Allocate a vector to hold 4 Persons
  • Prompt the user for information about each of the four persons and store that information in one of the vector elements
  • Print the information for each Person using an iterator
  • Clean up all allocated space

Homework Answers

Answer #1

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;
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Create a class called Student. Include the following instance variables: name, address, phone, gpa Create all...
Create a class called Student. Include the following instance variables: name, address, phone, gpa Create all of the methods required for a standard user defined class: constructors, accessors, mutators, toString, equals Create the client for testing the Student class (5 points extra credit) Create another class called CourseSection (20 points extra credit) Include instance variables for: course name, days and times course meets (String), description of course, student a, student b, student c (all of type Student) Create all of...
Step 1: Create a new Java project in NetBeans called “PartnerLab1” Create a new Java Class...
Step 1: Create a new Java project in NetBeans called “PartnerLab1” Create a new Java Class in that project called "BouncyHouse" Create a new Java Class in that project called “Person” Step 2: Implement a properly encapsulated "BouncyHouse" class that has the following attributes: Weight limit Total current weight of all occupants in the bouncy house (Note: all weights in this assignment can be represented as whole numbers) …and methods to perform the following tasks: Set the weight limit Set...
Write the following problem in Java Create a class Dog, add name, breed, age, color as...
Write the following problem in Java Create a class Dog, add name, breed, age, color as the attributes. You need to choose the right types for those attributes. Create a constructor that requires no arguments. In this constructor, initialize name, breed, age, and color as you wish. Define a getter and a setter for each attribute. Define a method toString to return a String type, the returned string should have this information: “Hi, my name is Lucky. I am a...
Create a C# application You are to create a class object called “Employee” which included eight...
Create a C# application You are to create a class object called “Employee” which included eight private variables: firstN lastN dNum wage: holds how much the person makes per hour weekHrsWkd: holds how many total hours the person worked each week. regHrsAmt: initialize to a fixed amount of 40 using constructor. regPay otPay After going over the regular hours, the employee gets 1.5x the wage for each additional hour worked. Methods:  constructor  properties  CalcPay(): Calculate the regular...
Create a class called Employee that should include four pieces of information as instance variables—a firstName...
Create a class called Employee that should include four pieces of information as instance variables—a firstName (type String), a lastName (type String), a mobileNumber (type String) and a salary (type int). Your class (Employee) should have a full argument constructor that initializes the four instance variables. Provide a set and a get method for each instance variable. The validation for each attribute should be like below: mobileNumber should be started from “05” and the length will be limited to 10...
In c++ create a class to maintain a GradeBook. The class should allow information on up...
In c++ create a class to maintain a GradeBook. The class should allow information on up to 3 students to be stored. The information for each student should be encapsulated in a Student class and should include the student's last name and up to 5 grades for the student. Note that less than 5 grades may sometimes be stored. Your GradeBook class should at least support operations to add a student record to the end of the book (i.e., the...
Create a class hierarchy to be used in a university setting. The classes are as follows:...
Create a class hierarchy to be used in a university setting. The classes are as follows:  The base class is Person. The class should have 3 data members: personID (integer), firstName(string), and lastName(string). The class should also have a static data member called nextID which is used to assign an ID number to each object created (personID). All data members must be private. Create the following member functions: o Accessor functions to allow access to first name and last...
Challenge: Animal Class Description: Create a class in Python 3 named Animal that has specified attributes...
Challenge: Animal Class Description: Create a class in Python 3 named Animal that has specified attributes and methods. Purpose: The purpose of this challenge is to provide experience creating a class and working with OO concepts in Python 3. Requirements: Write a class in Python 3 named Animal that has the following attributes and methods and is saved in the file Animal.py. Attributes __animal_type is a hidden attribute used to indicate the animal’s type. For example: gecko, walrus, tiger, etc....
java Create a program that defines a class called circle. Circle should have a member variable...
java Create a program that defines a class called circle. Circle should have a member variable called radius that is used to store the radius of the circle. Circle should also have a member method called calcArea that calculates the area of the circle using the formula area = pi*r^2. Area should NOT be stored in a member variable of circle to avoid stale data. Use the value 3.14 for PI. For now, make radius public and access it directly...
IntNode class I am providing the IntNode class you are required to use. Place this class...
IntNode class I am providing the IntNode class you are required to use. Place this class definition within the IntList.h file exactly as is. Make sure you place it above the definition of your IntList class. Notice that you will not code an implementation file for the IntNode class. The IntNode constructor has been defined inline (within the class declaration). Do not write any other functions for the IntNode class. Use as is. struct IntNode { int data; IntNode *next;...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT