Thread: Static & Data Member

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    1

    Unhappy Static & Data Member

    Hi all, this is my second classes involving c and c++ and right now i'm pretty stuck. The assignment is as follows:

    Create a SavingAccount class.

    Use a static data member annualInterestRate to store the annual interest rate for each of the savers.

    Each member of the class contains a private data member savingsBalance indicating the amount the saver currently has on deposit.

    Provide member function calculateMontlyInterest that calculates the monthly interest by multiplying the balance by annualInterestRate divided by 12; this interest should be added to savingsBalance.

    Provide a static member function member function modifyInterestRate that sets the static annualInterestRate to a new value.

    Write a driver program to test class SavingsAccount.

    Instantiate two different objects of class SavingsAccount, saver1 & saver2, with balances of $2000.00 and $3000.00, respectively.

    set the annualInterestRate to 3%.

    Then calculate the monthly interest and print the new balances for each of the savers.

    Then set annualInterestRate to 4%, calculate the next month's interest and print the new balances for each of the savers.

    I have done as much as you see the following.
    I think I know something about statics and data member; but, not perfectly. I can't eliminate the errors anyhow. Please help me and guide me how I should fix this program.

    Code:
      
    
    #include<iostream.h>
    #include<iomanip.h>
    
    class savingsAccount
    {
         private:
         double savingsBalance;      //data member
         static double annualInterestRate;  //data member	
       public:
    	savingsAccount() {savingsBalance = 0.0; };
    	savingsAccount(double balance) {savingsBalance = balance; };
    
    	static void modifyInterestRate(double rates);
    
    	void get_balance(double balance){this -> savingsBalance = balance;};
    	double calculateMonthlyInterest();  // add interest every month
    	void printBalance();  // Display the saving account
    };
    double savingsAccount::annualInterestRate = 0.030;
    
    double savingsAccount::calculateMonthlyInterest(){
    
    	 double interest = savingsBalance * annualInterestRate/12;
    	 savingsBalance += interest;
    	 return savingsBalance;  // add to savingsBalance
    	}
    	
    	void savingsAccount::modifyInterestRate(double i){
         if(i < 0.0 || i >= 1.0) throw out_of_range("invalid rate"); 
         annualInterestRate = i; 
    }
    
    	void savingsAccount::printBalance(){
    		cout << setiosflags(ios::fixed | ios::showpoint)
    			<< "$" << setprecision(2) << savingsBalance
    			<< resetiosflags (ios::fixed | ios::showpoint) << endl;
    	}
    
    int main()
    {
    /* The Scenario
    Each saver has $3000 and $4000 deposit in each account.
    Print the amount of money of their saving account for one year,
    so this shows how much amount of money is rising by 3% interest.
    Then, rise the interest upto 4%, and print how much money both of saver
    has in month of 13th. 
    */
    savingsAccount saver1(2000);
    savingsAccount saver2(3000);
    
    for (int n = 0; n < 12; n++){
    	saver1.calculateMonthlyRate();
    	cout << "1:" << endl;
    	saver1.printBalance();
    	saver2.calculateMonthlyRate();
    	cout << "2:" << endl;
         saver2.printBalance();
    }
    
    saver1.modifyInterestRate(0.040);
    saver1.calculateMonthlyRate();
    cout << "1:" << endl;
    saver1.printBalance();
    saver2.calculateMonthlyRate();
    cout << "2:" << endl;
    saver2.printBalance();
    
    return 0;
    }
    
    
    
    /*
         ---------------The following is the error that I get------------------
    
         if(i < 0.0 || i >= 1.0) throw std::out_of_range("invalid rate"); 
                                     ^
    prg10.8ontheway.cpp(29) : Error: Compile all files with -Ae to support exception handling
         annualInterestRate = i; 
                          ^
    prg10.8ontheway.cpp(30) : Error: ';' expected following declaration of struct member
    			<< "$" << setprecision(2) << savingsBalance
    			 ^
    prg10.8ontheway.cpp(35) : Error: reference must refer to same type or be const
    Had: smanip_long
    and: smanip_long&
    			<< resetiosflags (ios::fixed | ios::showpoint) << endl;
    			                                                ^
    prg10.8ontheway.cpp(36) : Error: reference must refer to same type or be const
    Had: smanip_long
    and: smanip_long&
    	saver1.calculateMonthlyRate();
    	                           ^
    prg10.8ontheway.cpp(52) : Error: 'calculateMonthlyRate' is not a member of 'savingsAccount'
    Fatal error: too many errors
    --- errorlevel 1
    
    */

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by ecoliteracy View Post
    Code:
    #include<iostream.h>
    #include<iomanip.h>
    Whoa! Ancient (read: pretty much dead) compiler warning! Try to find a standard C++ compiler.

    Otherwise, just read the compiler warnings/errors. Here's an alternate view:
    H:/Programs/CodeBlocks/bin/../lib/gcc/mingw32/3.4.4/../../../../include/c++/3.4.4/backward/backward_warning.h:32:2:
    warning: #warning This file includes at least one deprecated or antiquated
    header. Please consider using one of the 32 headers found in section
    17.4.1.2 of the C++ standard. Examples include substituting the <X> header
    for the <X.h> header for C++ includes, or <iostream> instead of the
    deprecated header <iostream.h>. To disable this warning use -Wno-
    deprecated.
    main.cpp: In static member function
    `static void savingsAccount::modifyInterestRate(double)':
    main.cpp:29: error: `out_of_range' undeclared (first use this function)
    main.cpp: In function `int main()':
    main.cpp:52: error: 'class savingsAccount' has no member named
    'calculateMonthlyRate'
    main.cpp:55: error: 'class savingsAccount' has no member named
    'calculateMonthlyRate'
    main.cpp:61: error: 'class savingsAccount' has no member named
    'calculateMonthlyRate'
    main.cpp:64: error: 'class savingsAccount' has no member named
    'calculateMonthlyRate'
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. uploading file to http server via multipart form data
    By Dynamo in forum C++ Programming
    Replies: 1
    Last Post: 09-03-2008, 04:36 AM
  3. compilation problems with static data member
    By viral612 in forum C++ Programming
    Replies: 1
    Last Post: 08-13-2008, 06:55 AM
  4. Using private class members in static functions
    By sethjackson in forum C++ Programming
    Replies: 2
    Last Post: 09-23-2005, 09:54 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM