follow style guidelines Rule 1: use const where appropriate Rule 2: every member
ID: 668389 • Letter: F
Question
follow style guidelines
Rule 1: use const where appropriate
Rule 2: every member function must include a description in the header file including precondition and postcondition.
Rule 3: every member variable must include a description of what the variable holds.
Rule 4: Classes should be capitalized. Bag, not bag. Person, not person. (I know this is different than how the book does it)
Rule 5: member variables of classes should be preceded by “m_”. If it’s not a member variable DON’T precede with m_
Rule 6: static const variables should be all capital letters. e.g: static const std::size_t CAPACITY = 30;
Rule 7: declare public:, protected:, then private:
Rule 8: non-member variable names should be lowercase.
Rule 9: Every class you define should be in its own .cpp and .h.
Do not make ANY changes to the Person class. Your only code should be in main.cpp.
Write a main function that allows the user to specify how many Persons to be entered, any number should be acceptable.
Use the number the user supplies to dynamically allocate an array of Person objects of this size.
Using a loop, ask the user for each Person’s name and age, and add this Person to the dynamic array. You can use C++ string for name.
After the loop is complete, print each person in the array using cout
Don’t forget to deallocate your memory!
You can assume the user will enter valid input.
Your program must look just like this example:
How many people would you like to enter?
2
Enter Person 1's name
Alice
Enter Person 1's age
34
Enter Person 2's name
Bob
Enter Person 2's age
44
People Entered:
Name: Alice Age: 34
Name: Bob Age: 44
////// Person.h /////////////////////
#ifndef PERSON_H_
#define PERSON_H_
#include <ostream>
#include <string>
class Person{
public:
// Initializes the Person with an empty string name and age of zero
// Postcondition: Person is initialized
Person ();
// Initializes the Person with the supplied name and age
// Postcondition: Person is initialized
Person (std::string name, int age);
// Returns the name of the Person
std::string getName () const;
// Sets the name of the person to the supplied name
// Postcondition: m_Name is equal to name
void setName (const std::string& name);
// Returns the age of the Person
int getAge () const;
// Sets the age of the person to the supplied age
// Postcondition: m_Age is equal to age
void setAge (const int age);
// overloaded operator<<
friend std::ostream& operator<< (std::ostream& os, const Person& person);
protected:
std::string m_Name;
int m_Age;
};
#endif // PERSON_H_
/////// Person.cpp //////////////////
#include "Person.h"
using namespace std;
Person::Person() : m_Name(""), m_Age(0)
{
}
Person::Person(string name, int age) : m_Name(name), m_Age(age)
{
}
std::string Person::getName () const
{
return m_Name;
}
void Person::setName(const string& name)
{
m_Name = name;
}
int Person::getAge () const
{
return m_Age;
}
void Person::setAge (const int age)
{
m_Age = age;
}
std::ostream &operator<<(std::ostream &os, const Person &person)
{
os << "Name: " << person.m_Name << " Age: " << person.m_Age << endl;
return os;
}
Explanation / Answer
Person.h
----------------------------------------------------
#ifndef PERSON_H_
#define PERSON_H_
#include <ostream>
#include <string>
class Person{
public:
// Initializes the Person with an empty string name and age of zero
// Postcondition: Person is initialized
Person ();
// Initializes the Person with the supplied name and age
// Postcondition: Person is initialized
Person (std::string name, int age);
// Returns the name of the Person
std::string getName () const;
// Sets the name of the person to the supplied name
// Postcondition: m_Name is equal to name
void setName (const std::string& name);
// Returns the age of the Person
int getAge () const;
// Sets the age of the person to the supplied age
// Postcondition: m_Age is equal to age
void setAge (const int age);
// overloaded operator<<
friend std::ostream& operator<< (std::ostream& os, const Person& person);
protected:
std::string m_Name;
int m_Age;
};
#endif // PERSON_H_
---------------------------------------------------------------------------------------------------------------------
Person.h
#include "Person.h"
using namespace std;
Person::Person() : m_Name(""), m_Age(0)
{
}
Person::Person(string name, int age) : m_Name(name), m_Age(age)
{
}
std::string Person::getName () const
{
return m_Name;
}
void Person::setName(const string& name)
{
m_Name = name;
}
int Person::getAge () const
{
return m_Age;
}
void Person::setAge (const int age)
{
m_Age = age;
}
std::ostream &operator<<(std::ostream &os, const Person &person)
{
os << "Name: " << person.m_Name << " Age: " << person.m_Age << endl;
return os;
}
--------------------------------------------------------------------------------------------------------------------------------
main.cpp
#include <iostream>
using namespace std;
int main()
{
//creating variables
int noOfPersons,age;
string name;
//reading data from user
cout << "How many persons you want to enter: ";
cin>>noOfPersons;
//creating array
Person persons[noOfPersons];
//reading data and creating objects
for(int i=0;i<noOfPersons;i++){
cout<<" Enter name: ";
cin>>name;
cout<<" Enter Age: ";
cin>>age;
Person obj = new Person(name,age);
persons[i] = obj;
}
cout<<" Entered details are: ";
//printing details
for(int i=0;i<noOfPersons;i++){
cout<<persons[i];
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.