Welcome to Dream.In.Code
Become a C++ Expert!

Join 150,018 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,477 people online right now. Registration is fast and FREE... Join Now!




creating objects in class

 
Reply to this topicStart new topic

creating objects in class, keyword new

allican57
1 Dec, 2006 - 02:33 PM
Post #1

New D.I.C Head
*

Joined: 2 Oct, 2006
Posts: 37


My Contributions

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



CODE

//////////////////////////
// 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.


User is offlineProfile CardPM
+Quote Post

gregoryH
RE: Creating Objects In Class
1 Dec, 2006 - 03:30 PM
Post #2

D.I.C Regular
Group Icon

Joined: 4 Oct, 2006
Posts: 417


Dream Kudos: 50
My Contributions
QUOTE(allican57 @ 1 Dec, 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


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.
CODE
/////////////////////////
// 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.
User is offlineProfile CardPM
+Quote Post

allican57
RE: Creating Objects In Class
4 Dec, 2006 - 02:46 PM
Post #3

New D.I.C Head
*

Joined: 2 Oct, 2006
Posts: 37


My Contributions
QUOTE(gregoryH @ 1 Dec, 2006 - 04:30 PM) *

QUOTE(allican57 @ 1 Dec, 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


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.
CODE
/////////////////////////
// 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.


User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 09:08PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month