-
November 8th, 2011, 02:08 AM
#1
Array of objects
hi all,
I do know how the array of objects work, as an example here it is:
Code:
Stock *s1=new Stock[2];
char n[29];
int num;
double val;
cout<<"Enter name :";
cin>>n;
cout<<"Enter shares :";
cin>>num;
cout<<"Enter share value :";
cin>>val;
s1[0].setName(n);
s1[0].setShare(num);
s1[0].setShareVal(val);
s1[0].show();
cout<<"Enter name :";
cin>>n;
cout<<"Enter shares :";
cin>>num;
cout<<"Enter share value :";
cin>>val;
s1[1].setName(n);
s1[1].setShare(num);
s1[1].setShareVal(val);
s1[1].show();
delete[] s1;
but i'm still confused about it, how i can do if Stock class has one more class object. like this:
Code:
class Stock
{
public:
Date date;
//some code
};
then how do i create array of objects, including date objects and Stock objects. Please guide me through this.. would be appreciated..
thanks all,
Last edited by Aashi; November 8th, 2011 at 02:23 AM.
-
November 8th, 2011, 02:25 AM
#2
Re: Array of objects
You clearly know how to create an array of char. Creating an array of objects is similiar.
-
November 8th, 2011, 02:34 AM
#3
Re: Array of objects
Code:
cout<<"Enter name :";
cin>>n;
cout<<"Enter shares :";
cin>>num;
cout<<"Enter share value :";
cin>>val;
s1[0].setName(n);
s1[0].setShare(num);
s1[0].setShareVal(val);
s1[0].show();
cout<<"Enter name :";
cin>>n;
cout<<"Enter shares :";
cin>>num;
cout<<"Enter share value :";
cin>>val;
s1[1].setName(n);
s1[1].setShare(num);
s1[1].setShareVal(val);
s1[1].show();
Your code is redundant. You should look up about loops.
Originally Posted by Aashi
how do i create array of objects
look up std::vector. It is usually best not to call new directly and if so, it should be placed inside a smart pointer so you don't have to manage its lifespan directly.
0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000 0000 0000 0000 0000
-
November 8th, 2011, 02:43 AM
#4
Re: Array of objects
Originally Posted by Joeman
Code:
cout<<"Enter name :";
cin>>n;
cout<<"Enter shares :";
cin>>num;
cout<<"Enter share value :";
cin>>val;
s1[0].setName(n);
s1[0].setShare(num);
s1[0].setShareVal(val);
s1[0].show();
cout<<"Enter name :";
cin>>n;
cout<<"Enter shares :";
cin>>num;
cout<<"Enter share value :";
cin>>val;
s1[1].setName(n);
s1[1].setShare(num);
s1[1].setShareVal(val);
s1[1].show();
Your code is redundant. You should look up about loops.
look up std::vector. It is usually best not to call new directly and if so, it should be placed inside a smart pointer so you don't have to manage its lifespan directly.
yep sure it would be like this:
Code:
Stock *s1=new Stock[2];
char n[29];
int num;
double val;
for(int i=0; i<2; i++){
cout<<"Enter name :";
cin>>n;
cout<<"Enter shares :";
cin>>num;
cout<<"Enter share value :";
cin>>val;
s1[i].setName(n);
s1[i].setShare(num);
s1[i].setShareVal(val);
s1[i].show();
}
delete[] s1
@laserlight: can u give an example,
would it be like this?
Code:
Stock stock[0];
Date date;
stock[0].show();
stock[0].date.show();
-
November 8th, 2011, 02:57 AM
#5
Re: Array of objects
Why not use std::string?
Originally Posted by Aashi
yep sure it would be like this:
I think you quoted more than what you intended because you didn't fix the unsafe practice of calling delete by hand which can be fixed with raii design at the least, but that is ok.
Code:
Stock *s1=new Stock[2];
should be
Code:
unique_ptr< Stock [] >s1( new Stock[2] );
which can also be
Code:
vector< Stock >s1( 2 );
or even just
Last edited by Joeman; November 8th, 2011 at 03:28 AM.
0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000 0000 0000 0000 0000
-
November 8th, 2011, 08:11 AM
#6
Re: Array of objects
In the sample code you posted, since Date is a member of Stock, it will be created automatically when you create a new Stock object.
If Date were a pointer, you'd have to create the Date object yourself, most likely in Stock's constructor.
-
November 8th, 2011, 09:20 AM
#7
Re: Array of objects
Originally Posted by Joeman
should be
Code:
unique_ptr< Stock [] >s1( new Stock[2] );
which can also be
Code:
vector< Stock >s1( 2 );
or even just
I don't think you can use unique_ptr with arrays unless you provide a custom deleter.
-
November 8th, 2011, 10:03 AM
#8
Re: Array of objects
Originally Posted by Lindley
I don't think you can use unique_ptr with arrays unless you provide a custom deleter.
you can, as unique_ptr is specialized for arrays; so, Joeman's code is correct
-
November 8th, 2011, 10:15 AM
#9
Re: Array of objects
thanks for replies guys,
can anyone tell me about using array of objects for two classes? i want to operate them as arrays not using vectors for the time now. As i have shown my code where i did create array of object for one class object Stock, so how i would do for two class objects.
please expert programmers tell me about this, it would be appreciated. i'm confused about this.
thanks..
-
November 8th, 2011, 10:18 AM
#10
Re: Array of objects
Originally Posted by Aashi
thanks for replies guys,
can anyone tell me about using array of objects for two classes? i want to operate them as arrays not using vectors for the time now. As i have shown my code where i did create array of object for one class object Stock, so how i would do for two class objects.
please expert programmers tell me about this, it would be appreciated. i'm confused about this.
thanks..
What do you mean? You want an array that will hold two different types of objects or are you still talking about when one class has a different class as a member?
-
November 8th, 2011, 11:10 AM
#11
Re: Array of objects
Originally Posted by GCDEF
What do you mean? You want an array that will hold two different types of objects
yes, an array that will have an object of Stock class, an object of Date class and e.t.c
just one example, because i'm confused about how this works..
-
November 8th, 2011, 11:16 AM
#12
Re: Array of objects
Originally Posted by Aashi
yes, an array that will have an object of Stock class, an object of Date class and e.t.c
just one example, because i'm confused about how this works..
It doesn't work. You'd need an array of pointers to a base class and the other classes derived from that base class.
-
November 8th, 2011, 11:19 AM
#13
Re: Array of objects
Originally Posted by Aashi
yes, an array that will have an object of Stock class, an object of Date class and e.t.c
just one example, because i'm confused about how this works..
Let me ask you:
You have this array -- how are you going to know if the item is a Stock object or Data object when you go through the array in a loop?
Regards,
Paul McKenzie
-
November 8th, 2011, 12:15 PM
#14
Re: Array of objects
Originally Posted by Paul McKenzie
Let me ask you:
You have this array -- how are you going to know if the item is a Stock object or Data object when you go through the array in a loop?
Regards,
Paul McKenzie
i just want to do self practice, want to take all object classes and then to show it. if i have three classes, Date, Time and Show.
then an array of type Show class has a date object and time object.
please one example so i can understand.
thanks..
-
November 8th, 2011, 12:20 PM
#15
Re: Array of objects
I'm not sure I understand you. However, if you mean to say that each Show has an associated Date and Time....
Code:
struct Show
{
Date d;
Time t;
};
int main()
{
Show shows[20];
for (int i = 0; i < 20; ++i)
{
shows[i].d = Date(some input);
shows[i].t = Time(some input);
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|