2 Replies - 855 Views - Last Post: 04 December 2006 - 03:46 PM Rate Topic: -----

#1 allican57  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 02-October 06

creating objects in class

Posted 01 December 2006 - 03:33 PM

I need to:Create two objects of type Person using the new operator.
Delete those objects with the delete operator before the program reaches the end. If someone could show me the proper place in the below code to insert the new and delete[size=5]. I can handle the syntax. Thanks



//////////////////////////
// The include section
///////////////////////////

#include <iostream>
#include <iomanip>
#include <conio.h>
#include <string>

////////////////////////////
// The namespace section
////////////////////////////

using namespace std;

////////////////////////
// The class section
///////////////////////

class Person   
 {
public:
		Person();
		void Add();
		void Update();
		void Delete();
		void Print();
		void Quit();
		void Sort();
		void Search();
		void Menu();
private:
		char FirstName[32];
		char MiddleName[32];
		char LastName[32];
		 int static Key;/
	   
};//end of class

	int Key = 0;
//////////////////////////
// The function prototype section
////////////////////////

void StartUp(void);
void WrapUp(void);

///////////////////////
// The global variable declaration section
///////////////////////

char ProgramName[] = "Program III.cpp";



/////////////////////////
// main
//			  This is the entry point for this program.  It controls
//			  the flow of execution.
///////////////////////

int main()
{
		StartUp();

		Person p;
		p.Menu();

		return 0;
}

/////////////////////
// The implementation section
//////////////////////

Person::Person()
{
	  
		strcpy_s(FirstName, "none");
		strcpy_s(MiddleName, "none");
		strcpy_s(LastName, "none");
}


// *****************
// StartUp
//	  Currently this is an empty module or stub. It will be used to
//	  perform any needed initialization.
// ****************

void StartUp(void){

}	   // End of function StartUp


//// ****************
//// Menu function
////			Here the user is prompted for his/her choice.
//// **************

void Person::Menu()
{
		void (Person::*f[7])() = {&Person::Add, &Person::Update, &Person::Delete, &Person::Sort, &Person::Print, &Person::Search, &Person::Quit};

		int Option;

		do
		{
						system("cls");

						cout << "\t\t Menu"
								<< "\n\t1. Add"
								<< "\n\t2. Update"
								<< "\n\t3. Delete"
								<< "\n\t4. Sort"
								<< "\n\t5. Print"
								<< "\n\t6. Search"
								<< "\n\t7. Quit" 
								<< "\n\n\tEnter an Option: ";

						cin >> Option;

						if(Option < 1 || Option > 7)
						{
								cout << "\nPlease re-enter your selection." << endl;
						}
						else
						{
								(this->*f[Option - 1])();
						}

		} while(Option != 7);

}
//// *****************
//// The Add function
////			This is where new data is added.
//// ***************
void Person::Add()
{
		if (strcmp(FirstName,"none") == 0)
		{
				cout << "Enter the first name: ";
				cin >> FirstName;
				cout << "Enter the middle name: ";
				cin >> MiddleName;
				cout << "Enter the last name: ";
				cin >> LastName;
		}
		else
		{
				cout << "A Name already exists. Try Update."
						<< endl << "Press Enter to continue." << endl;
				_getch();
		}
}
//// *****************
//// The Update function
////			This is where the user is able to alter existing data
////			in the class.
//// ***************
void Person::Update()
{
		if (strcmp(FirstName,"none") != 0)
		{
				cout << "Enter the first name: ";
				cin >> FirstName;
				cout << "Enter the middle name: ";
				cin >> MiddleName;
				cout << "Enter the last name: ";
				cin >> LastName;
		}
		else
		{
				cout << "A Name does not exist. Try Add."
						<< endl << "Press Enter to continue." << endl;
				_getch();
		}
}
//// ******************
//// The Delete function
////			Here the user is able to remove data.
//// *****************
void Person::Delete()
{
		char choice = 'z';

		if(strcmp(FirstName,"none") != 0)
		{
				cout << "\nAre you sure? (y or n)";
				cin >> choice;

				if(choice == 'Y' || choice == 'y')
				{
						strcpy_s(FirstName, "none");
						strcpy_s(MiddleName, "none");
						strcpy_s(LastName, "none");
				}
				else if(choice == 'N' || choice == 'n')
				{
						cout << "No deletion! Press enter.";
						_getch();
						return;
				}
				else
				{
						cout << "Invalid selection! No deletion! Press enter.";
						_getch();
						return;
				}

		}
		else
		{
				cout << "A Name does not exist. There is nothing to Delete."
						<< endl << "Press Enter to continue." << endl;
				_getch();
		}
}
//// ****************
//// The Sort function
////			This is a stub function.
//// **************
void Person::Sort()
{
		cout << "Sort was here." << endl << "Press Enter to continue." << endl;
		_getch();
}
//// ****************
//// The Print function
////			This is where the data from the class is displayed
////			to the monitor.
//// ***************
void Person::Print()
{
		cout << "Key: " << Key << endl
				<< "FirstName: " << FirstName << endl
				<< "MiddleName: " << MiddleName << endl
				<< "LastName: " << LastName << endl
				<< endl << "Press Enter to continue." << endl;
		_getch();
}
//// *****************
////  Search
////			This is a stub function.
//// ************
void Person::Search()
{
		cout << "Search was here." << endl << "Press Enter to continue." << endl;
		_getch();
}
//// ***************
//// The Quit function
////			This function is used when the user chooses to end the program.
//// ****************

void Person::Quit()

{

		cout << "\n\tYou have chosen to Quit the program."

				 << "\n\tPlease have a great day." << endl;

		WrapUp();

}

//// ************
//// WrapUp
////	It is used to perform any end of program processing needed.
//// *************

void WrapUp(void)

{

cout << "\n\tProgram "

				<< ProgramName

				<< " ended successfully.\n\t";

} // End of function WrapUp.


Is This A Good Question/Topic? 0
  • +

Replies To: creating objects in class

#2 gregoryH  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 60
  • View blog
  • Posts: 656
  • Joined: 04-October 06

Re: creating objects in class

Posted 01 December 2006 - 04:30 PM

View Postallican57, on 1 Dec, 2006 - 03:33 PM, said:

I need to:Create two objects of type Person using the new operator.
Delete those objects with the delete operator before the program reaches the end. If someone could show me the proper place in the below code to insert the new and delete[size=5]. I can handle the syntax. Thanks

Allican

Your code is really well written, making the job of reading your code very easy. Thanks.

Baed on your post, I am guessing that you want to create an array of Person. You may need to include a

I have commented you main program with the changes.
/////////////////////////
// main
//			  This is the entry point for this program.  It controls
//			  the flow of execution.
///////////////////////

int main()
{
		StartUp();

		//Person p;
				Person p = new Person [ 5 ]; // this creates 5 people
		p.Menu();
	 
				delete [] p; // correct clean up.

		return 0;
}


Given the code layout, you might want to overload operator[] to work with your data.

I have also spotted a small typo int static Key;/. I recomend you try compiling your code to find other typo's.
Was This Post Helpful? 0
  • +
  • -

#3 allican57  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 02-October 06

Re: creating objects in class

Posted 04 December 2006 - 03:46 PM

View PostgregoryH, on 1 Dec, 2006 - 04:30 PM, said:

View Postallican57, on 1 Dec, 2006 - 03:33 PM, said:

I need to:Create two objects of type Person using the new operator.
Delete those objects with the delete operator before the program reaches the end. If someone could show me the proper place in the below code to insert the new and delete[size=5]. I can handle the syntax. Thanks

Allican

Your code is really well written, making the job of reading your code very easy. Thanks.

Baed on your post, I am guessing that you want to create an array of Person. You may need to include a

I have commented you main program with the changes.
/////////////////////////
// main
//			  This is the entry point for this program.  It controls
//			  the flow of execution.
///////////////////////

int main()
{
		StartUp();

		//Person p;
				Person p = new Person [ 5 ]; // this creates 5 people
		p.Menu();
	 
				delete [] p; // correct clean up.

		return 0;
}


Given the code layout, you might want to overload operator[] to work with your data.

I have also spotted a small typo int static Key;/. I recomend you try compiling your code to find other typo's.

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1