How would you rewrite this code?

How would you rewrite the following code with getters and setters?

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

class Person
{

public :

//Person (string, int);
string name;
int age;
};

int main()
{
const int MAX_PERSONS = 100;
const string STOP = "-1";
Person human[MAX_PERSONS]; // An array of Person class
Person insert; // Sort temporary variable
int moveitem; // Sort move item
string name; // Person's name
int age; // Person's age
int peopleinarray = 0; // # of people stored
int i=0; // Loop variable

cout << "Enter name (-1 to stop): ";
cin >> name;
human[i].name = name;

// Continue as long as we have not exceeded the maximum # of people and
// the user has not finished their input
while (i < MAX_PERSONS && name != STOP)
{
cout << "Enter age of " << name << ": ";
cin >> age;
human[i].age = age;
i++; // Increment loop variable
cout << "Enter name (-1 to stop): ";
cin >> name;
human[i].name = name;
}

// Store the number of people added to the array
peopleinarray = i;

// Sort the array from youngest to oldest age with insertion sort
for (i =1; i < peopleinarray; ++i)
{
insert = human[i]; // Store value of the current array element
moveitem = i; // Track location of element
while ((moveitem > 0) && (human[moveitem - 1].age > insert.age))
{
human[moveitem]=human[moveitem - 1];
moveitem--;
}
human[moveitem] = insert;
}

// Display out the sorted array
cout << endl;
for (i = 0; i < peopleinarray; ++i)
{
cout << "Name: " << human[i].name << ", age: " << human[i].age;
cout << endl;
}
system ("pause");
}
Add getters and setters for the members, then use them instead of accessing the variables directly.
Topic archived. No new replies allowed.