#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Posts
    11
    Rep Power
    0

    Question my class constructor not constructing


    #include "accounts.h"

    class Bank{
    sav_acct *Ps;
    chk_acct *Pc;
    time_acct *Pt;
    char name[20];
    char addy[50];
    public:
    Bank(int x, int y, int z){
    Ps = new sav_acct[x];
    Pc = new chk_acct[y];
    Pt = new time_acct[z];
    }

    ~Bank(){
    delete []Ps;
    delete []Pc;
    delete []Pt;
    }

    void set_name(char *p);
    char *get_name(){ return name;}
    void withdrawl(char type, int index, double amt);
    void deposit(char type, int index, double amt);
    double cash_chk(int index, double amt);


    };

    okay the main problem is, when i call the constructor
    for example:
    Bank b1( 200, 400, 500);

    it does not make 200, 400, or 500 of those instances, i have a feeling it is because of the way i have the dynamic memory created...
    any ideas?
    **the sav, check, and time accounts are in accounts.h**
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Location
    Southern California
    Posts
    73
    Rep Power
    14

    Re: my class constructor not constructing


    Originally posted by maskzilla
    it does not make 200, 400, or 500 of those instances, i have a feeling it is because of the way i have the dynamic memory created...
    The number of elements of an array, the array bound, must be a constant expression. If you need variable bounds, use a vector. For example:

    Code:
    void func(int i)
    {
        int v[i];   // error: not a constant
        vector<int> v2(i);  // ok
    }
  4. #3
  5. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,593
    Rep Power
    4207
    >>The number of elements of an array, the array bound, must be a constant expression.
    He's using the new keyword to allocate the arrays dynamically and assigning them to his pointers. The C++ new keyword allows you to pass a variable in as argument. The following is perfectly legal C++
    Code:
      int i;  
      cin >> i;
      int *array = new int[i];
      // Do whatever with array
      delete [] array;
    maskzilla, your code looks ok at first sight. The only reason I can think of as to why it might fail, is if there isn't enough RAM. How are you sure that you're not getting 200 elements of sav_acct?

    As a side-note, the GNU C and C++ compilers have extensions to the standard that allow you to do this:
    Code:
      int i;  
      cin >> i;
      int array[i];
      // use array for whatever
    Mind you, this is a non-standard thing though and will not work with other compilers. See http://web.mit.edu/6.033/labdoc/gcc_7.html#IDX275 for the documentation on this and other extensions added to the GNU compilers for C and C++.
    Last edited by Scorpions4ever; February 11th, 2003 at 07:44 PM.

IMN logo majestic logo threadwatch logo seochat tools logo