implement a class Person with two data members

New C++ student here, and so thrilled this forum is available to beginner! Thanks for taking the time!
My assignment is to implement a class Person with two data members: name(string) and age(integer), writing a program that reads in a list of names & ages, storing them in a one-dimensional array of Person objects. The maximum number of names entered is 100. After reading in the list of names and ages, sort the list from youngest to oldest, then print out the name & age for each person in the sorted list.
We are instructed to have 3 files to separate the interface from the implementation. So I have Person.h, Person.cpp and ClientCode.cpp that link together at compile time. Here is the code I have so far. The code compiles and the member functions work properly when it is just in one file. When I have to split it out into the 3 pieces, I don't know how to define the constructor for an array and create the object of instantiation with two arrays. I am so confused with those parts. Any suggestions to set me straight would be appreciated.

//ClientCode.cpp - First file with main function
#include "Person.h" //include header definition of class - Person
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;


//function main begins program execution

int main()
{

Person MyPerson[arrSize];

MyPerson.arrInit(); //initialize age array
MyPerson.arrInput(); //input the data from the keyboard
MyPerson.arrSort(); //sort the data in ascending order
MyPerson.arrOutput(); // output the data to the display


system("pause");
return (0);

}//end main


//Person.h - second HEADER file for class definition
//Created by
//Person class definition

#include <iostream>
#include <string>
#include <iomanip>
#pragma once
using namespace std;

class Person
{
public:
Person:: Person()
{
name = " ";
age = 0;
};
void arrInit();
void arrInput();
void arrSort();
void arrOutput();

private:
string name; //name of the person ??
int age; //age of the person ??
static const int arrSize = 100; //how many names
int arrAge[arrSize]; //age of the person - array
string arrName[arrSize]; //name of the person - array
};


//Person.cpp THIRD file for member functions
#include "Person.h"
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;


//constructor initializes data members
Person::Person()
{
name = " ";
age = 0;
};

void Person::arrInput()
{
for (int s=0; s < arrSize; s++)
{

cout << "Enter name (-1 to stop): ";
cin >> arrName[s];

//if (arrName[s] == -1)
//{
// break;
//}
cout << "Enter age of " << arrName[s] << ": ";
cin >> arrAge[s];
} //end for
}

void Person::arrOutput()
{
for (int j=0; j < arrSize; ++j)
{
cout << "Name: " << setw(15) << arrName[j] << "," << setw(5) << "age: " << arrAge[j] << endl;
}
}

//insertion sort - chapter 7 - swap positions
void Person::arrSort()
{
int insertNum = 0;
string insertName;

for (int m = 0; m < arrSize; ++m)
{
for (int k = 0; k < (arrSize-1); ++k)
{
insertNum = arrAge[k+1];
insertName = arrName[k+1];

if (insertNum < arrAge[k])
{
arrAge[k+1] = arrAge[k];
arrAge[k] = insertNum;
arrName[k+1] = arrName[k];
arrName[k] = insertName;
}//end if
}//end nested inner for
}//end outer for
}//end function

//initialize array of age
void Person::arrInit()
{
for (int i=0; i < arrSize; i++)
{
arrAge[i] = 0;
}
}


Last edited on
The maximum number of names entered is 100.

Best to define a constant for this. Therefore, if it changes you're only change one value, not multiple values in your code.

const int MAX_ARRAY = 100;

I don't know how to define the constructor for an array

I don't think you mean constructor on an array. You'll have an array of person objects, each with their own constructor. You seem to have arrays inside your class, which isn't what you need. You want an array of the class.

Person People[MAX_ARRAY];


Person(string name,int age);
//more Constructor here, but what do I need here?

. . .

//constructor initializes data members
Person::Person(string name, int age)


So you've declared your constructor but haven't defined it. I would probably have it set the values to 0 or NULL. Since that constructor has parameters, it suggests you're going to pass in values where I don't think you really need that. Best to use an initialisation list for these default values.

1
2
3
4
5
6
7
8
9
10
11
// Constructor
Person::Person(): name(" "), age(0)
{
};

// This is essentially the same as this
Person::Person()
{
   name = " ";
   age  = 0;
};


So when you create your array, every element will have the initial values of " " for name and 0 for age. Since your brief says you want a maximum array of 100 but never specifies that the whole 100 indices will be populated, it's best to initialise to some value like this so you don't have junk values in the unused elements.

Hope this helps.

EDIT: One more thing; please use code tags!

Last edited on
If you don't need a default constructor, if it makes no sense to have it (¿what will be the initialization?), then don't create a default constructor.
Person::Person(string name,int age): name(name), age(age){}

Now, as you can't initialize the array (¿with what?) I suggest to use
1
2
3
4
std::vector<Person> persons;
persons.reserve(MAX_SIZE);
//...
persons.push_back( Person(name, age) ); //adding a person 


sort the list from youngest to oldest, then print out the name & age for each person in the sorted list.
You need to provide compare and print functionallity.
Either as part of the interface (as a method, or a friend function) or provide access to the data.
If the brief for the assignment specifies an array then an array should be used, not a vector.

Marks will most likely be awarded for how well instructions are followed, as well as functionality.
Last edited on
Thank you for the suggestions. I edited the code above to reflect your additions. (It still doesn't compile ) If you don't mind a really ignorant question, (jic I post here again) what does a code tag look like and where do you place them? I am really new at this...sorry for the imposition.
When you're writing out your post, there's an icon to the right of the box that will insert code tags that looks like this <>.

Alternatively, you can just write your own tags. Open with [ code ] and close with [ /code ] (both without the spaces either side of the word).

There's still a few things wrong with your code. As you'll find with a lot of people on this board, we tend not to post entire solutions. The goal is to help people learn and understand what they're doing, which is why you're more likely to get suggestions like the ones I've posted above.

It's then up to you to figure out how to implement them in your code. If you're still getting stuck, feel free to repost and we can help out more. I, for one, have no problems helping out people that are willing to try to understand and implement the concepts given, whereas I'll be hugely reluctant to help people who are only interested in a copy/paste fix.
Last edited on
It's me again...after adding the "const int arrSize = 100;" under private data members, the compiler is not happy. Where should this declaration be, so that it is recognized globally?
Thank you so much iHutch. I will use the code tags next time!
Well, you don't want your array size to be a private member of your class.

The size of the array isn't an attribute of the person. Just like with real people.

Your MAX_ARRAY (good practice to define constants as all uppercase characters) should be in the main part of your code, where you declare your array (in your case, ClientCode.cpp). An overview would look something like this:

ClientCode.cpp
1
2
3
4
5
6
7
8
9
// Your includes

int main()
{
   const int MAX_ARRAY = 100;  // constant definition
   Person People[MAX_ARRAY]; // 100 instances of your Person class, called People

   // main processing
}


Person.h
1
2
3
4
5
6
7
8
9
10
11
12
// Includes here
// This should only contain the Person class declarations

class Person
{
   public:
     // functions
     int DoSomething();  // example function, obviously yours will differ
   private:
     string name;
     int age;
}


Person.cpp
1
2
3
4
5
6
7
8
9
10
// Includes here

// This should contain the declarations of the functions

// Again, this function is made up nonsense.  Yours will differ.
int Person::DoSomething()
{
   int some_value;
   return some_value;
}


Note, the only member variables for your Person class are the two specified in the project brief; name and age.

Hope this helps.
Topic archived. No new replies allowed.