Lost Password?


Go Back   CodeCall Programming Forum > Software Development > C and C++

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-22-2008, 06:12 PM
gsingh2011 gsingh2011 is offline
Newbie
 
Join Date: Nov 2007
Posts: 3
Rep Power: 0
gsingh2011 is on a distinguished road
Default Creating an instance of an object

I don't really know if this is a dumb question or not but how would you create an instance of an object when the use wants you to. For example, lets say I make an address book that stores various information about contacts. I have a class named contact. Now when the user wants to add a new contact and when he enters the contacts name, then I want the object with the class contact to be created. The objects name should be the name of the contact the user entered. How would I do that? I hope I made the question clear....
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 01-22-2008, 11:25 PM
dargueta dargueta is offline
Guru
 
Join Date: Oct 2007
Age: 18
Posts: 793
Last Blog:
Programs Under the Hoo...
Rep Power: 13
dargueta is a jewel in the roughdargueta is a jewel in the roughdargueta is a jewel in the roughdargueta is a jewel in the rough
Default

Your class named Contact should have a constructor with the information you want it to hold, i.e. name, address, etc. You would maintain a list of contacts in an array, and when the user asks to add a contact, you can reallocate the array to add a new space, then call the constructor for the pointer with the arguments, like so:

C++ Code:
  1. class AddressBook
  2. {
  3. private:
  4.     Contact        **contactList;
  5.     unsigned int  numberOfContacts;
  6. public:
  7.     //this is the constructor for your address book, which maintains a
  8.     //list of your contacts. Constructors and destructors would go here.
  9.     ///////////////////////////////////
  10.     //this is the function that you wanted:
  11.     //the actual format of the arguments depends on how you have
  12.     //your contacts defined, i.e. what your Contact class
  13.     //contains.
  14.     bool addNewContact(char *name,char *address)
  15.     {
  16.         ++numberOfContacts;
  17.         Contact **temp = (Contact **)realloc(contactList,numberOfContacts*sizeof(Contact *));
  18.         //this check is only for dummy-proofing
  19.         if(temp == NULL)
  20.             return false//this means that we're out of memory.
  21.         else
  22.             contactList = temp;
  23.         //now that we've added a new space, we need to copy the info.
  24.         contactList[numberOfContacts - 1] = new Contact(name,address);
  25.         //creation of new contact successful.
  26.         return true;
  27.     }
  28. };

In your program, you need to declare a variable of type AddressBook. When the user wants to create a new contact...

C++ Code:
  1. AddressBook *addrbk = new AddressBook();
  2. //get user input...
  3. //now user wants to create a new contact.
  4. //get the appropriate information, i.e. name, address, etc....
  5. //and now add the new contact.
  6. addrbk->addNewContact(name,address);

The code I provided for AddressBook is the bare minimum to illustrate my example. Feel free to copy it, but make sure you code constructors, destructors, etc. If you want a somewhat more convoluted (but still relatively easy) method of doing this without classes, just let me know.

Last edited by dargueta; 01-22-2008 at 11:30 PM. Reason: Clarified a point
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help needed with a image object VB5 please MagicBytes Visual Basic Programming 3 11-07-2007 01:43 PM
Object IO Stream Data Loss Possible? Any solution, pls? reachpradeep Java Help 0 03-05-2007 03:02 PM
object in session.... "<logic:present>" with JSTL? reachpradeep Java Help 0 03-04-2007 09:46 AM
Creating a Custom Cursor ahsan16 Tutorials 2 01-13-2007 06:03 PM
Creating an analog clock with ActionScript AfTriX Tutorials 2 01-07-2007 03:19 AM


All times are GMT -5. The time now is 11:35 AM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 101%


Complete - Celebrate!

Ads