Thread: class arrays~

  1. #1
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563

    class arrays~

    As known we could initialize an array like this:
    PHP Code:
    int arr[5]={1,2,3,4,5}; 
    and I got it from my book array could also be operated with class, that should be something like this:
    PHP Code:
    class people{...};
    const 
    int amout=5000  //I think we should have almost 5000 guys here, ya~
    people cprogramming[amout]; 
    it works and...... my question is could we initialize the arry cprogramming in the same line declared ? just as the arr on top.

    any ideas ?
    Never end on learning~

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    You mean like this? Sorry for my sloppy programming I just through this together to convey the message.

    Code:
    #include <iostream>
    using namespace std;
    
    class CPoint
    {
    	private:
    		int m_x;
    		int m_y;
    	public:
    		CPoint( ); 
    		CPoint( int x, int y ) : m_x( x ), m_y( y ) { }
    		inline void Display( void )
    		{
    			cout << m_x << endl;
    			cout << m_y << endl;
    		}
    };
    
    int main( void )
    {
    	CPoint PointArray[ ] = { CPoint(1,2), CPoint(2,3) };
    	PointArray[0].Display();
    	PointArray[1].Display();
    	
    
    	return 0;
    }
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    this line below confused me a little, any explaination please ?

    PHP Code:
    CPointint xint y ) : m_x), m_y) { } 
    Never end on learning~

  4. #4
    TK
    Guest
    I think that you might want to try dynamically allocating the array instead of using static allocation. To dynamically allocate an array you use the 'new' operator.

  5. #5
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    Originally posted by TK
    I think that you might want to try dynamically allocating the array instead of using static allocation. To dynamically allocate an array you use the 'new' operator.
    oh, thanx. I know not much about it, where is my book ? I need it now~~~
    Never end on learning~

  6. #6
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    Originally posted by black
    this line below confused me a little, any explaination please ?

    PHP Code:
    CPointint xint y ) : m_x), m_y) { } 

    Its a different means of initialising data members, and the only way to initialise const data members. Its equivalent to the code shown below, but is possibly a little bit quicker and more efficient.

    Code:
    CPoint::CPoint( int x, int y )
    {
         m_x = x;  //bah!!! Hungarian :(
         m_y = y;
    }

  7. #7
    TK
    Guest
    I just think that you are asking the question wrong Black. What you want to know is how to determine the size of the array at runtime. That is what you are wondering about.

  8. #8
    TK
    Guest
    Code:
    #include<iostream>
    #include<algorithm>
    #include<iomanip>
    #include<string>
    #include<vector>
    
    using std::cin;
    using std::cout;
    
    int main()
    {
      int * pint;
      int icount;
    
      cout << "How many numbers do you want to enter? ";
      cin >> icount;
    
      pint = new int[icount];
      int i;
    
      for ( i = 0; i < icount; ++i)
        {
          cout << "Enter number " << i + 1 << ": ";
          cin >> pint[i];
        }
    
      cout << "The numbers you entered are:" << endl;
      for ( i = 0; i < icount; ++i)
        {
          cout << pint[i] << endl;
        }
    
      delete []pint;
    
      return 0;
    }

  9. #9
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    Originally posted by TK
    I just think that you are asking the question wrong Black. What you want to know is how to determine the size of the array at runtime. That is what you are wondering about.
    that should be Dynamic Memory, yes ? I am reading something these days, hope all would be clear soon. thanx, TK~

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 09:10 AM
  2. Specializing class
    By Elysia in forum C++ Programming
    Replies: 6
    Last Post: 09-28-2008, 04:30 AM
  3. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM