I have written another program in C++. Excluding the point that the class definition should be in a separate header file, is there anything that needs to be improved?
/*
Date:5th January 2011
Programmer:Fahad
*/
#include <iostream>
#include <string>
using namespace std;
class Persons //A class that will store the name,addresses and id numbers of the users
{
private:
string name_;
string address_;
int id_number_;
public:
static int count; //This is the count of the objects created
Persons();
void getData(int n);
void displayData(int n);
~Persons();
};
int Persons count;//initializing the static member
int main()
{
cout << "Enter Number Of Persons:";
int n; //This is the number of objects that the user wants to make.
cin >> n;
Persons *ptr; //A pointer that will be used for the dynamic memory allocation.
/*Exception Handling*/
////////////////////////////////////////////////////////////////////
try
{
//ptr=new [sizeof(Persons) * n];
ptr=new Persons[n];
}
catch(bad_alloc xa)
{
cout<<"Sorry,Program Can Not Continue";
cin.get();
exit(1);
}
/////////////////////////////////////////////////////////////////////
for(int i = 0; i< n; i++)
{
ptr[i].getData(n);
}
for(int j = 0; j< n; j++)
{
ptr[j].displayData( n );
}
cin.get();
delete[] ptr;
return 0;
}
/*Function Definitions*/
Persons::Persons()
{
name_="";
address_="";
id_number_=0;
count++;
}
void Persons::getData(int n)
{
cout<<"Enter Name (Press '$' To Exit):";
getline(cin,name_,'$');
cout<<endl<<"Enter Address (Press '$' To Exit):";
getline(cin,address_,'$');
cout<<endl<<"Enter Identitiy Card Number:";
cin>>id_number_;
}
void Persons::displayData(int n)
{
cout<<"Name:"<<name_;
cout<<endl<<"Address:"<<address_;
cout<<endl<<"Identitiy Card Number:"<<id_number_;
}