Change the Date for all objects of a class

This is a discussion on Change the Date for all objects of a class within the C++ Programming forums, part of the General Programming Boards category; Hello, I'm hoping I can get an idea for how to impliment this. I have two subclasses (SavingsAccount & CheckingAccount) ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    6

    Change the Date for all objects of a class

    Hello, I'm hoping I can get an idea for how to impliment this.

    I have two subclasses (SavingsAccount & CheckingAccount) that derive from a BankAccount class. What I need to do is call a function that changes the date of all objects created by either two subclasses. For example:

    Code:
    BankAccount.changeDate(1,1,2000);
    savingsAccountA.print();
    savingsAccountB.print();
    checkingAccountA.print();
    checkingAccountB.print();
    
    BankAccount.changeDate(2,14,2001);
    savingsAccountA.print();
    savingsAccountB.print();
    checkingAccountA.print();
    checkingAccountB.print();
    Output:
    Savings Account A: Jan 1, 2000
    Savings Account B: Jan 1, 2000
    Checking Account A: Jan 1, 2000
    Checking Account B: Jan 1, 2000

    Savings Account A: Feb, 14, 2001
    Savings Account B: Feb, 14, 2001
    Checking Account A: Feb, 14, 2001
    Checking Account B: Feb, 14, 2001
    I'm sure there is a simple way to do this, but for the life of me I can't figure it out. The ultimate goal is to make a date mutatable so that when a new month is set, interest will be added to each account as well as some other monthly occurances.

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,622
    The easiest way I can think of is to make the actual date member(s) and the changeDate() method static members of the base class.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    6
    I think I understand what you mean, but wouldn't I have to changeDate() for each and every instance of an object? or is that the purpose of making the members static?

    [edit] Nevermind, that was a dumb question...So I could, in a sense, create a date object of the BankAccount to handle all date changes in theory? I'll give it a shot. Thanks for your help!
    Last edited by ShadowDragon; 12-07-2011 at 03:11 AM.

  4. #4
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by ShadowDragon View Post
    [edit] Nevermind, that was a dumb question...So I could, in a sense, create a date object of the BankAccount to handle all date changes in theory? I'll give it a shot. Thanks for your help!
    Static date object ...preferably in the protected region.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    6
    It works beautifully! The only thing I am having a problem with is when I try to impliment the actions needed when a month changes. I have it setup where:

    Code:
    void
    Code:
     
    
    int BankAccount::transactionsAMonthCounter = 1;
    BankAccount::helpIncrement() //used in an =+ operator overloader
    {    
    
    if(!endOfMonth(day)) //finds out if the incremented date is at the end of the month
    day++;
    else
    if (month < 12) {
    month++; day = 1; transactionsAMonthCounter = 1;
    }
    else {
    year++; month = 1; day = 1; } }
    So what I am trying to do is reset the number of transactions that are limited by the Savings Account per month. Currently the savings account has it set up with a loop that increases the transactionsAMonthCounter by 1 each time a successful transaction is made. However, because the date manipulation needs to effect both the checkingAccount and the SavingsAccount classes, I need to somehow set the transactionsAMonthCount back to 1 from the BankAccount class. I tried to make it static, but when I debug it keeps placing the static value for transactionsAMonthCounter to -85899...etc(the limit of the int value).

    Any suggestions?
    Last edited by ShadowDragon; 12-07-2011 at 04:39 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 07-15-2010, 07:53 PM
  2. Two date objects showing same date?
    By dxfoo in forum C++ Programming
    Replies: 7
    Last Post: 06-17-2010, 06:06 PM
  3. Replies: 1
    Last Post: 08-22-2005, 09:21 PM
  4. Replies: 4
    Last Post: 10-16-2003, 11:26 AM
  5. base class pointer to derived class objects
    By curlious in forum C++ Programming
    Replies: 4
    Last Post: 09-28-2003, 08:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21