Fairly simple classes help please
Hi all, how's it going?
I'm a C++ novice and have this bad habit of writing code in one big 'main' file and then trying to split it up into classes and functions afterwards. The following program works fully but I need to have 'Town' as a class that stores the name, poulation and area (a town.h file). I know only the very basics about classes and so don't really know where to start (constructors/destructors etc - not sure how to implement for my code).
I'd appreciate any help
thanks
Sam
Code:
#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <string>
//#include "town.h"
using namespace std;
int main()
{
int c,g;
int n = 0;
cout << "How many towns would you like to enter? ";
cin >> c;
string *narray = new string[c];
double *parray = new double[c];
double *aarray = new double[c];
while (n <= (c-1))
{
cout << "\nPlease enter the name of town " << (n+1) << ": ";
cin >> narray[n];
cout << "Please enter the population: ";
cin >> parray[n];
cout << "Please enter the area (km): ";
cin >> aarray[n];
cout << narray[n] << " has a population of " << parray[n]
<< " and an area of " << aarray[n] << "km" << endl;
n++;
}
n = 0;
g = 0;
while (g < c)
{
g++;
if (parray[n] < parray[g])
{
n = g;
}
}
cout << "\n" << narray[n] << " has the largest population" << endl;
n = 0;
g = 0;
while (g < c)
{
g++;
if (aarray[n] < aarray[g])
{
n = g;
}
}
cout << narray[n] << " has the largest area" << endl;
n = 0;
g = 0;
while (g < c)
{
g++;
if ((aarray[n]/parray[n]) < (aarray[g]/parray[g]))
{
n = g;
}
}
cout << narray[n] << " has the largest population density" << endl;
delete[] narray;
delete[] parray;
delete[] aarray;
getch();
return 0;
}