Thread: array of objects?

  1. #1
    *~*~*~*
    Guest

    array of objects?

    just a general question........

    say u have a class and want to create objects for that class, would you be able to use an array of objects for it or would u need to define each object created individually thanks to anyone that can help! the objects are created in main and used to access the member functions. can u define objects in member functions? also can u use a for loop or a do while loop in order to assign values to the array of objects?...thats if u can use an array of objects? im really confused on that thanks if u can help

  2. #2
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    here is an example taht should answer all your questions.

    Code:
    #include <iostream.h>
    #include <conio.h>
    using namespace std;
    
    class test // my simple class 
    {
    public:
    void hello() // simple function
    {   
     cout << "hello"<<endl;
    }
    };
    
    
    int main ()
    {
    int MAX = 50; // max amount of arrays of objects
    int n = 0; // for when i increment my array
    test atest[MAX]; // my array of 50 objects
    for (int i = 0; i <= 50; i++) // output the function hello 50 times
        atest[n++].hello();
    
    getch();  
    return 0;
    }

    From this question i take it you dont have a c++ book.
    here
    http://www.maththinking.com/boat/computerbooks.html
    Last edited by gamer4life687; 05-31-2003 at 12:32 PM.
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  3. #3
    Registered Luser
    Join Date
    Apr 2003
    Posts
    17
    Originally posted by gamer4life687
    here is an example taht should answer all your questions.

    [code]
    #include <iostream.h>
    #include <conio.h>
    using namespace std;
    .
    .
    .

    [ nitpick ]

    You don't have to 'use' namespace std if you are using non-standard C++ headers.

    [ /nitpick ]

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    "say u have a class and want to create objects for that class, would you be able to use an array of objects for it or would u need to define each object created individually"

    When you declare an array of objects, the default constructor will create each object in the array. Try this code and see what happens:
    Code:
    #include <iostream>
    using namespace std;
    
    class aclass
    {
    public:
    	aclass() //Default constructor
    	{
    		cout<<"Default constructor called."<<endl;
    	}
    	aclass(int n) //Constructor
    	{
    		cout<<"Constructor called."<<endl;
    		num = n;
    	}
    		
    private:
    	int num;
    };
    
    int main()
    {
    	//Create single object with default constructor
    	aclass a; 
    	
    	//Create single object with constructor
    	aclass b(25); 
    	
    	//Create an array of objects which calls default
    	//constructor for each object
    	aclass an_array[3];
    	
    	
    	return 0;
    }
    "can u define objects in member functions?"

    Sure, but it probably doesn't work the way you expect. First, the object is destroyed when the member function ends, and second to call the member function you need another object. Take a look at this code:
    Code:
    #include <iostream>
    using namespace std;
    
    class aclass
    {
    public:
    	aclass() //Default constructor
    	{
    		cout<<"Default constructor called."<<endl;
    	}
    	aclass(int n) //Constructor
    	{
    		cout<<"Constructor called."<<endl;
    		num = n;
    	}
    	void display()
    	{
    		cout<<"Data member num = "<<num<<endl;
    	}
    	void create()
    	{
    		aclass d(40);
    		d.display();
    	}
    
    		
    private:
    	int num;
    };
    
    int main()
    {
    	aclass a;
    	a.create();
    
    	//d.display(); Uncomment this line-->error
    	
    	return 0;
    }
    I don't know if you've studied this yet, but you can dynamically allocate memory for an object using the operator new and assign the address to a data member which is a pointer, and then you won't have the problem of the object being destroyed when the member function ends.

    "also can u use a for loop or a do while loop in order to assign values to the array of objects?"

    Sure, the compiler doesn't care what your object name is. Your object can be named my_object or my_array[1] and it doesn't matter. my_array is the name of the array of objects, but each object in the array has a name, and my_array[1] is the name of the second object in the array--my_array[0] is the name of the first.
    Code:
    #include <iostream>
    using namespace std;
    
    class aclass
    {
    public:
    	aclass() //Default constructor
    	{
    		cout<<"Default constructor called."<<endl;
    	}
    	aclass(int n) //Constructor
    	{
    		cout<<"Constructor called."<<endl;
    		num = n;
    	}
    	void display()
    	{
    		cout<<"Data member num = "<<num<<endl;
    	}
    	void set_num(int n)
    	{
    		num = n;
    	}
    	
    private:
    	int num;
    };
    
    int main()
    {
    	aclass my_object;
    	aclass my_array[3];
    	
    	my_object.set_num(25);
    	my_object.display();
    
    
    	my_array[0].set_num(40);
    	my_array[0].display();
    	
    	return 0;
    }
    Last edited by 7stud; 05-31-2003 at 02:26 PM.

  5. #5
    *~*~*~*
    Guest
    thank u

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  3. Replies: 4
    Last Post: 10-16-2003, 11:26 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. Adding objects to an array
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 11-27-2001, 09:24 AM