Thread: generating someting dynamically

  1. #1
    Unregistered
    Guest

    generating someting dynamically

    Hi, I have this assignment due monday and i'm really stuck with a couple of things:

    I'm supposed to create a program for a bank. I have to have one base class ACCOUNT and two sub-classes, CHECKING, and SAVINGS. These classes will have virtual functions so that I can dynamically create the object without knowing the type. I can't figure out how to first create an account and then changing that to a checking or a savings. I also can't figure out how to use the virtual functions, since the program would never have to go into those functions that are in the ACCOUNT class. I don't wan't the code to the whole program, but just an exemple of how I can make these objects. If any more info is needed please reply.

  2. #2
    Ethereal Raccoon Procyon's Avatar
    Join Date
    Aug 2001
    Posts
    189
    Just use pointers:

    Code:
    account * theaccount;
    theaccount = new checking();
    Even though theaccount is an account pointer it can still access functions in the derived class, as long as they also exist in the base class (even abstractly.)

  3. #3
    Unregistered
    Guest
    Thanks, but I need a way to continue to create these objects in a loop, The program should not know a head of time the number of accounts, but if I create a pointer and then I assign it to a class, if the next account is diffrent then won't the pointer delete the previous pointer and replace it? I need a way to work with one pointer and keep changing it's type some how.

  4. #4
    Ethereal Raccoon Procyon's Avatar
    Join Date
    Aug 2001
    Posts
    189
    If you need only one account at a time, just use the same pointer, deleting the account when you're through with it.

    If you need several accounts at once, make a dynamic array of pointers with new[], then allocate each account individually to whatever you need it to be.

    Code:
    account **accountarray;
    accountarray = new *accountarray[naccounts]
    
    //alternate checking and savings accounts
    for (n = 0; n < naccounts; n++)
    {
      if (n%2==0) accountarray[n] = new checking(args);
      else accountarray[n] = new savings(args);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Generating random 2D mountains/terrain
    By Flaug in forum C++ Programming
    Replies: 7
    Last Post: 04-12-2009, 02:49 PM
  2. dynamically allocated strings??
    By CS_Student8337 in forum C Programming
    Replies: 18
    Last Post: 03-19-2009, 05:14 AM
  3. Replies: 60
    Last Post: 01-09-2009, 01:09 PM
  4. Doxygen failing
    By Elysia in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 04-16-2008, 01:24 PM
  5. dynamically generating content from an ASP page:
    By edwardtisdale in forum Windows Programming
    Replies: 0
    Last Post: 11-22-2001, 02:44 PM