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

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




array of objects

 
Reply to this topicStart new topic

array of objects, this is a pgm that just displays the output for 10 items.

dan_ram
24 Nov, 2007 - 08:59 AM
Post #1

D.I.C Head
**

Joined: 15 Aug, 2007
Posts: 88


My Contributions
CODE

#include<iostream.h>
class ITEM
{
   int item_no,qty_hd;
   float unit_price;
   char des[25];        //description
  protected:
   int rol,roq;        //reorder level & reorder quantity
  public:
   void accept()
   {
      cout<<"enter: "<<endl;
      cout<<"item number"<<endl;
      cin>>item_no;
      cout<<"description"<<endl;
      cin.get();
      cin.getline(des,25);
      cout<<"rate"<<endl;
      cin>>unit_price;
      cout<<"quantity on hand"<<endl;
      cin>>qty_hd;
      cout<<"reorder level"<<endl;
      cin>>rol;
      cout<<"reorder quantity"<<endl;
      cin>>roq;
   }
   void display()
   {
      if(qty_hd<=rol)
      {
     cout<<"item no."<<item_no<<endl
         <<"description"<<des<<endl
         <<"reorder quantity"<<roq<<endl;
      }
   }
};
void main()
{
   ITEM s[10];        // to accept 10 values
   s[10].accept();
   s[10].display();
}

[b]this program has no syntax error. it accepts the values properly but an error occurs saying "abnormal program termination" & "null pointer...".
I seriously need help so as to what it means and why it happens.pls dont ask y those data members are used.



User is offlineProfile CardPM
+Quote Post

Kiriran
RE: Array Of Objects
24 Nov, 2007 - 09:21 AM
Post #2

New D.I.C Head
*

Joined: 11 Apr, 2007
Posts: 41


My Contributions
first off, I don't know C++ (but C)
CODE

ITEM s[10];
s[10].accept();
s[10].display();

the first problem I see here is, that you create an array of ITEM that can hold 10 ITEMs. s[10] tries to access the 11th item wich is obviously not there and that's why you get a null pointer. A null pointer means you try to access "nothing". try s[9] because arrays start counting at 0. So first ITEM is at s[0], seccond at s[1] etc...

Now I don't know how C++ initializes Objects in an array, but I guess it will insert null as default value. So s[9] will probably raise a null pointer exception again. You need to create instance of the object first in the array. I think in C++ that's done with new ITEM()
CODE

int i;
for(i = 0; i<10; i++) {
s[i] = new ITEM();
}

this will place 10 ITEM objects into your array. now s[9] should work.

hth
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Array Of Objects
24 Nov, 2007 - 11:12 AM
Post #3

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,868



Thanked: 53 times
Dream Kudos: 550
My Contributions
you don't need to use the new operator here as C++ will create the objects for you in this case.
User is online!Profile CardPM
+Quote Post

Bench
RE: Array Of Objects
24 Nov, 2007 - 02:46 PM
Post #4

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 684



Thanked: 24 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
QUOTE(Kiriran @ 24 Nov, 2007 - 05:21 PM) *

Now I don't know how C++ initializes Objects in an array, but I guess it will insert null as default value.

No, it will construct the objects using their default constructor. Please, if you don't know, don't guess. C doesn't initialise objects in an array to null either, so I'm not sure where you got this idea from (Java perhaps?).


QUOTE(Kiriran @ 24 Nov, 2007 - 05:21 PM) *

So s[9] will probably raise a null pointer exception again. You need to create instance of the object first in the array. I think in C++ that's done with new ITEM()
CODE

int i;
for(i = 0; i<10; i++) {
s[i] = new ITEM();
}

this will place 10 ITEM objects into your array. now s[9] should work.

hth

Again, if you don't know, don't guess. As NickDMax pointed out, the new operator isn't needed here - The code you've pasted won't compile, and changing it to use pointers is unnecessary.


Aside from the array out-of-bounds issue It looks to me as if the OP's code is OK, it seems that the OP's accept function initialises the object's data members with user input
(Of course, the program would likely fall over if the accept function was never called, but I'm sure design issues can come later)
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 10:10PM

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