CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Oct 2010
    Posts
    98

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Array of objects

    You clearly know how to create an array of char. Creating an array of objects is similiar.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Jun 2008
    Posts
    592

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

  4. #4
    Join Date
    Oct 2010
    Posts
    98

    Re: Array of objects

    Quote Originally Posted by Joeman View Post
    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();

  5. #5
    Join Date
    Jun 2008
    Posts
    592

    Re: Array of objects

    Code:
    char n[29];
    Why not use std::string?
    Quote 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
    Code:
    Stock s1[2];
    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

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    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.

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Array of objects

    Quote Originally Posted by Joeman View Post
    should be
    Code:
    unique_ptr< Stock [] >s1( new Stock[2] );
    which can also be
    Code:
    vector< Stock >s1( 2 );
    or even just
    Code:
    Stock s1[2];
    I don't think you can use unique_ptr with arrays unless you provide a custom deleter.

  8. #8
    Join Date
    Oct 2008
    Posts
    1,456

    Re: Array of objects

    Quote Originally Posted by Lindley View Post
    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

  9. #9
    Join Date
    Oct 2010
    Posts
    98

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

  10. #10
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Array of objects

    Quote Originally Posted by Aashi View Post
    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?

  11. #11
    Join Date
    Oct 2010
    Posts
    98

    Re: Array of objects

    Quote Originally Posted by GCDEF View Post
    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..

  12. #12
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Array of objects

    Quote Originally Posted by Aashi View Post
    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.

  13. #13
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Array of objects

    Quote Originally Posted by Aashi View Post
    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

  14. #14
    Join Date
    Oct 2010
    Posts
    98

    Re: Array of objects

    Quote Originally Posted by Paul McKenzie View Post
    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..

  15. #15
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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);
        }
    }

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured