GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 15-Oct-2004, 05:56
wc3promet wc3promet is offline
New Member
 
Join Date: Oct 2004
Posts: 7
wc3promet is on a distinguished road
Exclamation

How to Create C++ Objects at Runtime?


These are the instructions given to me:

Quote:
Your program should display the following menu to demonstrate the operations on queue objects for test run.

(1) Create queue object
(2) Enqueue
(3) Dequeue
(4) Display
(5) Exit


Here is my code for my menu:
CPP / C++ / C Code:
// PROGRAM MENU DISPLAY  
  	do
	{
    	cout<<"\n\n";
    	cout<<"Please select one of the following choices:\n";
    	cout<<"\n";
    	cout<<"(1) Create queue object\n";
    	cout<<"(2) Enqueue\n";
    	cout<<"(3) Dequeue\n";
    	cout<<"(4) Display\n";
    	cout<<"(5) Exit\n";
    	cout<<"\n";
    	cout<<"Choice:";
    	cin>>choice;
    
  	switch(choice)
  	{     	
  	// CASE 1: CREATE QUEUE OBJECT
  	case 1:
 	break;
		
		// CASE 2: ENQUEUE  
		case 2:
		break;
		
		// CASE 3: DEQUEUE 
		case 3:
		break;
		
		// CASE 4: DISPLAY	 
		case 4:
		break;
		
		// CASE 5: QUIT PROGRAM		
		case 5: 
		cout<<"Quitting.......\n\n\n\n\n";
		cout<<"Program Terminated.\n";
		break;
		
		// DEFAULT CASE: INVALID CHOICES AND TERMINATES PROGRAM
 		default:
		cout<<"Invalid choice.\nProgram Terminating...."<<endl;
		break;
	
      	}
  	}
	while(choice<5 && choice>0);
  	return 0;
}

My lecturer's reply states that he wants me to create the new queue object at runtime.

Quote:
1) With regards to "(1)Create queue object", is it correct if we allowed the queue q1 object to be created before calling Case 1: Create queue object?

Preferably no.

He doesn't allow me to have queue q1; before the menu function. He wants me have it as Case 1 in the menu function. I am confused. My compiler doesn't allow me to put

Code:
case 1: queue q1; break;

Isn't it impossible for C++ to create objects during runtime in this situation
Last edited by LuciWiz : 15-Oct-2004 at 06:02. Reason: Please insert your c++ code between [c++] [/c++] tags
  #2  
Old 15-Oct-2004, 06:49
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 890
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
I suppose queue is a class you defined, right?
Well, I'm not going to get too deep into this, but consider this *different* approach:

CPP / C++ / C Code:

queue * q1;
//....

case 1:
    q1 = new queue();
   break;

Kind regards,
Luci
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
Last edited by LuciWiz : 15-Oct-2004 at 08:05.
  #3  
Old 15-Oct-2004, 07:19
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
Originally Posted by wc3promet
Isn't it impossible for C++ to create objects during runtime in this situation

Not only is it possible, it is (IMHO) a big part of OOP. The example Luci gave you will create your object at runtime. Even if you don't have a constructor in your class, it will allocate sufficient memory for your object.

You probably know this, but referencing members in a pointer to your class is different that referencing members in a static class.

CPP / C++ / C Code:
queue->add("Hello");   //Dynamic (pointer)
queue.add("Hello");   //Static (non-pointer)
  #4  
Old 15-Oct-2004, 08:17
wc3promet wc3promet is offline
New Member
 
Join Date: Oct 2004
Posts: 7
wc3promet is on a distinguished road
A big thanks to our 2 Moderators: LuciWiz and dsmith.

However, other than using queue* q1 in this situation of creating objects during runtime,

What are the situations that it's better to use
CPP / C++ / C Code:
queue q1;
instead of
CPP / C++ / C Code:
queue* q1;
and vice-versa?

I must admit that I'm still an inept user of C++ and OOP.
Last edited by LuciWiz : 15-Oct-2004 at 08:32. Reason: Please use [c] & [/c] tags to enclose your C code - look at those pretty colours :)
  #5  
Old 15-Oct-2004, 09:21
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
I have two comments on that
  1. With OOP you can have constructors in your class. So when you define an object at run-time, you can actually run code at the time that it is defined. This is great for class initilizations, etc.

    In addition, there is a destructor that is called when you delete it. Once again, very handy for clean-up.
  2. In any program, dynamically allocated memory is invaluable (IMHO). This is what allows your program to be flexible. Let's say that your class represents a list of people. At run time you can shrink & grow your database set when using dynamically allocated memory. So instead of setting a static maximum of 100 entries, my program can be efficient at just 2 entries or grow as large as needed (based on resources).

HTH.
 
 

Recent GIDBlogToyota - 2008 August Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing multiple objects in functions crystalattice C++ Forum 3 08-Oct-2004 08:18
Easy way to create tab ctrl dlaygent MS Visual C++ / MFC Forum 1 18-May-2004 20:05
Can't seem to create db Tigress7 MySQL / PHP Forum 3 19-Aug-2003 09:19
How do I create JavaScript Links? JdS Web Design Forum 8 29-Jan-2003 15:02

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:09.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.