> > #include <string.h> > #include > > class Court > { > private: > //attributes > char *firstName; > char *secondName; > int count; > > //private methods > int GetSlot(); > public: > //constructors & destructors > Court(); > Court(char *first, char *second); > ~Court(); > > // Changing information > int PersonEnters(char *name); > void PersonLeaves(char *name); > > //getting information > void WhosInFirst(char *buffer); > void WhosInSecond(char *buffer); > int CountPeople(); > > }; > > Court::Court() > { > firstName = new char[50]; > secondName = new char[50]; > strcpy(firstName, ""); > strcpy(secondName, ""); > count = 0; > } > > Court::Court(char *first, char *second) > { > firstName = new char[50]; > secondName = new char[50]; > strcpy(firstName, first); > strcpy(secondName, second); > count = 2; > } > > Court::~Court() > { > delete firstName; > delete secondName; > } > > int Court::GetSlot() > { > if (strcmp(firstName,"") == 0) > { > return(1); > } > else if (strcmp(secondName, "") == 0) > { > return(2); > } > else > { > return(0); > } > } > > void Court::PersonLeaves(char *name) > { > if (strcmp(firstName, name) == 0) > { > strcpy(firstName, ""); > count--; > } > else if (strcmp(secondName, name) == 0 ) > { > strcpy(secondName, ""); > count--; > } > } > > int Court::PersonEnters(char *name) > { > int slot; > > slot = GetSlot(); > > if (slot == 1) > { > strcpy(firstName, name); > count++; > return(1); > } > else if (slot == 2) > { > strcpy(secondName, name); > count++; > return(1); > } > else > { > return(0); > } > } > > void Court::WhosInFirst(char *buffer) > { > strcpy(buffer, firstName); > } > > void Court::WhosInSecond(char *buffer) > { > strcpy(buffer, secondName); > } > > int Court::CountPeople() > { > return(count); > } > > void main() > { > > Court *Original; > > //we need a buffer for when we get the names, > char *buffer; > buffer = new char[50]; > > cout << "Initializing Original with parameters" << endl; > Original = new Court; > > > cout << "Adding some names!" << endl; > Original->PersonEnters("Scott"); > cout << "Number of people in Original = "; > cout << Original->CountPeople() << endl; > Original->PersonEnters("IRD"); > cout << "Number of people in Original = "; > cout << Original->CountPeople() << endl; > > cout << endl << "Listing People" << endl; > Original->WhosInFirst(buffer); > cout << "First has " << buffer << endl; > Original->WhosInSecond(buffer); > cout << "Second has " << buffer << endl; > > cout << endl << "Removing some people" << endl; > Original->PersonLeaves("Scott"); > cout << "Number of people = "; > cout << Original->CountPeople() << endl; > cout << endl << "Remove someone not there" << endl; > Original->PersonLeaves("Jhon"); > cout << "Number of people in Original = "; > cout << Original->CountPeople() << endl; > > //clean up > cout << endl << "Clean up" << endl; > delete Original; > > } > >
delete [] firstName; firstName = 0; firstName = new char[strlen(first)];
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionchar firstName[50];