Jump to content

Convert old code to New Classes.

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
Hawk1

Hawk1

    Learning Programmer

  • Members
  • PipPipPip
  • 70 posts
i have this good ole code that runs
// Header file first

#ifndef _ADDRESSBOOK

#define _ADDRESSBOOK


const int MAXADDRESS = 25;


struct PERSON

{

char fName[25];

char lName[25];

char Address[100];


};


bool addPerson(const PERSON &p);

bool getPerson(PERSON &p);

bool findPerson(char *lastName, PERSON &p);

bool findPerson(char *lastName, char *firstName, PERSON &p);

void printBook();



#endif



// addressBook.cpp


#include 

#include "addressBook.h"


using namespace std;

PERSON people[MAXADDRESS];

int head = 0;

int tail = -1;


bool addPerson(const PERSON &p)

{

if(head < MAXADDRESS)

{

people[head] = p;

head++;

if(tail == -1)

tail++;

return true;

}

return false;

}

bool getPerson(PERSON &p)

{

if(tail >=0)

{

if(tail >= MAXADDRESS)

tail = 0;

p = people[tail];

tail++;

return true;

}

return false;

}

bool findPerson(char *lastName, PERSON &p)

{

for(int i = 0; i < head; i++)

{

if(!stricmp(people[i].lName, lastName))

{

p = people[i];

return true;

}

}

return false;

}

bool findPerson(char *lastName, char *firstName, PERSON &p)

{

for(int i = 0; i < head; i++)

{

if(!stricmp(people[i].lName, lastName) && !stricmp(people[i].fName, firstName))

{

p = people[i];

return true;

}

}

return false;

}

void printBook()

{


for(int i = 0; i < head; i++)

{

cout << people[i].fName << "\t" << people[i].lName << "\t" << people[i].Address << endl;

}


}


// finally main.cpp


#include 

#include 

#include 

#include "addressbook.h"



using namespace std;

int printMenu();

void waitKey();


const int ADDPERSON = 1;

const int GETPERSON = 2;

const int FINDLAST = 3;

const int FINDBOTH = 4;

const int PRINT = 5;

const int EXIT = 0;

int main()

{

int selection;

PERSON p;

bool status;

char lName[50];

char fName[50];



selection = printMenu();

while(selection != EXIT )

{

switch(selection)

{

case ADDPERSON :

cout << "Enter First Name " << endl;

cin >> p.fName;

cout << "Enter last Name " << endl;

cin >> p.lName;

cout << "Enter Address " << endl;

cin >> p.Address;

status = addPerson(p);

if(status == false)

cout << "Sorry There is no more room in the address book " << endl;

else

cout << "Thanks for your Entry " << endl;


waitKey(); 

break;

case GETPERSON :

status = getPerson(p);

if(status)

cout << p.fName << "\t" << p.lName << " " << p.Address << endl;

else

cout << "Sorry The address book is empty " << endl;


waitKey();


break;

case FINDLAST :

cout << "Enter a last name " << endl;

cin >> lName;

status = findPerson(lName,p);

if(status)

cout << p.fName << "\t" << p.lName << " " << p.Address << endl;

else

cout << "Sorry, Name not found " << endl;


waitKey();

break;


case FINDBOTH :

cout << "Enter last name " << endl;

cin >> lName;

cout << "Enter first name " << endl;

cin >> fName;

status = findPerson(lName, fName,p);

if(status)

cout << p.fName << "\t" << p.lName << " " << p.Address << endl;

else

cout << "Sorry, Name not found " << endl;


waitKey();

break;


case PRINT :

printBook();

waitKey();

break;

case EXIT :

cout << "Thanks for using the address book " << endl;

exit(0);

}

selection = printMenu();

}

}


int printMenu()

{

int selection;


system("CLS");

cout << "1. Add A Person" << endl;

cout << "2. Get A Person " << endl;

cout << "3. Find A person By Last Name " << endl;

cout << "4. Find A person By First and Last Name " << endl;

cout << "5. Print the address book" << endl;

cout << "0. Exit this program " << endl;

cin >> selection;


return selection;


}


void waitKey()

{


cout << "Press a key to continue " << endl;

while(!kbhit())

;


getch();

fflush(stdin);


}
it creates an adddresss b00k good for 10 people
now my book has told me to take that code i did before and turn iit into a class.

they APPEAR to be really confusing, now is there an easy way to break it down and take the code step by step and change it? or is it easier to just start with a blank. cpp and go from there??
thanks

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Step 1) change the word "struct" to "class"
Step 2) move the function declarations into the class (where appropriate)
Step 3) fix whatever else needs to be fixed so it compiles/runs :)
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Hawk1

Hawk1

    Learning Programmer

  • Members
  • PipPipPip
  • 70 posts
hmmmmmmm sounds easier then make it out to be then. i'll have to give it a shot