Thread: easy Vector contructor question

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    27

    easy Vector contructor question

    I have the following constructor

    Code:
    Vector ( T fill [ ], int size)
    {
      // don't know what goes here
    }
    My private variable are
    int cap;
    int num;
    T * data;

    I am told this contructor uses an array of elements to initialize the Vector.


    I am sure this is really easy but I am confused on how and what to do. Thanks in advance.
    Last edited by noodle24; 04-21-2006 at 03:24 PM.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You mean like this?
    Code:
    Vector(T fill[], int size) : cap(0), num(-1), data(NULL)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    It probably will use new to allocate an array for data, then copy the elements from fill into data.

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    27
    I don't know how to put what you said into code. I'm kind of a newbie.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Something like this:
    Code:
    data = new T[size];
    and
    Code:
    delete [] data;
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Hi,

    The idea with a constructor is to send it things that can be used to fill up your private variables. Here is a simple example:

    Code:
    class Vector
    {
    public:
    	
    	Vector(int c, int n)
    	{
    		cap = c;
    		num = n;
    	}
    
    	void show()
    	{
    		cout<<cap<<" " <<num<<endl;
    	}
    
    private:	
    	int cap;
    	int num;
    };
    
    int main()
    {
    	Vector v(10, 20);
    	v.show();
    	
    	return 0;
    }
    I am told this contructor uses an array of elements to initialize the Vector
    .
    Code:
    Vector ( T fill [ ], int size)
    {
      // don't know what goes here
    }
    You don't have to be told that the constructor uses an array of elements to initialize the vector, you can see it. The first parameter of the constructor is: T fill []. In C++, brackets mean "an array". You declare an array like this:

    int num [] = {10, 20, 30};

    or

    int num [3] = {10, 20, 30};

    When a function has a parameter that is an array, the size of the leftmost dimension does not need to be specified, so for a single dimension array, you can write:
    Code:
    void someFunc(int anArray[], int size)
    {
    	....
    }
    The only thing funny about your Vector constructor is the type of the array. Instead of it being a type like 'int', it has a type that is 'T'. That has to do with templates, and it means the Vector constructor can take any type of an array as an argument. The second argument is the size of the array. Notice that the type of the array in the constructor, i.e. type T, matches the type of the pointer that is your private variable:

    T * data;

    That's a good indication that the array in the constructor is meant to be stored in data. A pointer stores an address, so it looks like data was meant to store the address of the array in the constructor. Or, you can create a new array and assign its address to data and then copy the elements of the constructor array into the new array. Creating a new array and copying the elements into it is probably the way to go.

    The way you create a new array is like this:

    data = new T[size];

    I'll leave the copying of the data from the constructor array to the new array to you.
    Last edited by 7stud; 04-21-2006 at 07:47 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Easy String position question...
    By Striph in forum C Programming
    Replies: 4
    Last Post: 05-11-2009, 08:48 PM
  2. Easy question, (should be) easy answer... ;-)
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 06-12-2002, 09:36 PM
  3. Replies: 20
    Last Post: 05-25-2002, 07:14 PM
  4. EASY GUI development.. like with Qt?
    By rezonax in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2001, 01:18 PM