Aug 19, 2011 at 7:34pm UTC
//classData Array.cpp
//data items as class objects
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Person
{
private:
string lastName;
string firstName;
int age;
//----------------------------------------------------------
public:
Person(string last,string first, int a): //constructor
lastName(last), firstName(first), age(a)
{ }
//----------------------------------------------------------
void displayPerson()
{
cout << " Last name: " << lastName;
cout << " First name:" << firstName;
cout << " Age: " << age << endl;
}
//---------------------------------------------------------
string getLast()
{return lastName;} // get last name
}; // end class person
class ClassDataArray
{
private:
vector<Person*> v;
int nElems;
public:
ClassDataArray(int max) : nElems(0)
{ v.resize(max);}
~ClassDataArray()
{
for(int j=0; j<nElems; j++)
delete v[j];
}
Person* find(string searchName)
{
int j;
for(int j=0; j<nElems; j++)
if(v[j]->getLast()== searchName)
break;
if(j == nElems)
return NULL;
else
return v[j];
};
void insert(string last, string first, int age)
{
v[nElems] = new Person(last, first, age);
nElems++;
}
bool remove(string searchName)
{
int j;
for(j=0; j<nElems; j++)
if( v[j] ->getLast() == searchName)
break;
if (j==nElems)
return false;
else
{
delete v[j];
for(int k=j; k<nElems;k++)
nElems--;
return true;
}
}
void displayA()
{
for(int j=0; j<nElems; j++)
v[j]->displayPerson();
}
}; //end class ClassDataArray
////////////////////////////////////////////////////////////////
int main()
{
int maxSize = 100; //array size
ClassDataArray arr(maxSize); //array
arr.insert("Evans", "Patty", 24); //insert 10 items
arr.insert("Smith", "Lorraine", 37);
arr.insert("Yee", "Tom", 43);
arr.insert("Adams", "Henry", 63);
arr.insert("Hashimoto", "Sato", 21);
arr.insert("Stimson", "Henry", 29);
arr.insert("Velasquez", "Jose", 72);
arr.insert("Lamarque", "Henry", 54);
arr.insert("Vang", "Minh", 22);
arr.insert("Creswell", "Lucinda", 18);
arr.displayA(); //display items
string searchKey = " Stimson "; //search for item
cout << "Searching for Stimson" << endl;
Person* found;
found=arr.find(searchKey);
if(found != NULL)
{
cout << " Found ";
found->displayPerson();
}
else
cout << " Can’t find " << searchKey << endl;
cout << "Deleting Smith, Yee, and Creswell" << endl;
arr.remove("Smith"); //delete 3 items
arr.remove("Yee");
arr.remove("Creswell");
arr.displayA(); //display items again
return 0;
} //end main()
Aug 20, 2011 at 6:41pm UTC
lol silly mistakes always kill me, and i did forget some stuff in the Member Function remove, Thank You very much for your help. :)