DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Savings Account Class

  1. #1
    Join Date
    Nov 2006
    Posts
    2

    Savings Account Class

    Hello,
    im new to these forums and thought i would sign up since its the weekend and i cant get help from my instructor for a week. Ive learned quite a few things in C++ such as classes, arrays, pointers, etc. I seem to be having a problem making this class:

    Create a SavingsAccount 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 calculateMonthlyInterest that calculates the monthly interest by multiplying the savingsBalance by annualInterestRate divided by 12 and then adds this interest to savingsBalance. Provide a static member function modifyIntererestRate 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 and 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 the annualInterestRate to 4% and calculate the next month's interest and print the new balances for each of the savers

    As i said, ive made classes before but this one seems to be a little more advanced than the previous one i did. Any help would be appreciated.

  2. #2
    Join Date
    Nov 2003
    Posts
    4,118
    Well, you need to show some effort on your side since we're not doing other people's homework here (read the Etiquette). If you have a specific question, you can ask it here.
    Danny Kalev

  3. #3
    Join Date
    Nov 2006
    Posts
    2
    i wasnt really specific with my question so i apologize about that. As i said, i know how to make a class but what is really confusing me is the way to class Savings Account would work. How would i make saver1 and saver2? And could someone give me an example of how a static member function works?

  4. #4
    Join Date
    Dec 2003
    Posts
    3,366
    classname saver1; //your class name goes in front, its making a variable or "object" or "instance" of the class, much like int x makes an object of type int, named 'x'

    class foo
    {
    static int x;
    }

    int foo::x;

    //I think you do it that way, but maybe the static is backwards, I don't do this a lot but the compiler will tell you if its the wrong way.

  5. #5
    Join Date
    Nov 2006
    Posts
    12
    Here's a working solution. Study it hard and I'm sure you'll figure it out. I've commented most of your problem in the code so you can see what and where i've done everything. Hope it helps byeee
    Code:
    #include <iostream>
    
    using namespace std;
    
    class SavingsAccount
    {
    	public:
    		SavingsAccount(){}
    		SavingsAccount(int value);
    		~SavingsAccount(){}
    		static float annualInterestRate;
    		void calculateMonthlyInterest();
    		static void modifyIntererestRate(float value);
    		float GetBalance() const { return savingsBalance; }
    	private:
    		// Each member of the class contains a private data member 
    		// savingsBalance indicating the amount the saver currently has 
    		// on deposit.
    		float savingsBalance;
    };
    
    // copy constructor to initialize the value at instantiation
    SavingsAccount::SavingsAccount(int value)
    {
    	savingsBalance = value;
    }
    
    // Use a static data member annualInterestRate to store the annual interest
    // rate for each of the savers. 
    float SavingsAccount::annualInterestRate = 0;
    
    // Provide member function calculateMonthlyInterest that calculates the 
    // monthly interest by multiplying the savingsBalance by annualInterestRate
    // divided by 12 and then adds this interest to savingsBalance.
    void SavingsAccount::calculateMonthlyInterest()
    {
    	savingsBalance += ((savingsBalance * annualInterestRate) / 12);
    }
    
    //Provide a static member function modifyIntererestRate that sets the 
    // static annualInterestRate to a new value. 
    void SavingsAccount::modifyIntererestRate(float value)
    {
    	annualInterestRate = value;
    }
    
    int main()
    {
    	// Instantiate two different objects of class SavingsAccount, saver1 
    	// and saver2, with balances of $2000.00 and $3000.00, respectively.
    	SavingsAccount saver1(2000.00);
    	SavingsAccount saver2(3000.00);
    
    	// Set the annualInterestRate to 3%.
    	SavingsAccount::modifyIntererestRate(3);
    
    	// Then calculate the monthly interest and print the new balances for 
    	// each of the savers. 
    	saver1.calculateMonthlyInterest();
    	cout << "Saver 1 Savings Balance: $" << saver1.GetBalance() << endl;
    	saver2.calculateMonthlyInterest();
    	cout << "Saver 2 Savings Balance: $" << saver2.GetBalance() << endl;
    
    	cout << endl;
    
    	// Then set the annualInterestRate to 4%
    	SavingsAccount::modifyIntererestRate(4);
    
    	// and calculate the next month's interest and print the new balances 
    	// for each of the savers
    	saver1.calculateMonthlyInterest();
    	cout << "Saver 1 Savings Balance: $" << saver1.GetBalance() << endl;
    	saver2.calculateMonthlyInterest();
    	cout << "Saver 2 Savings Balance: $" << saver2.GetBalance() << endl;
    
    	cout << endl;
    	return 0;
    }

Similar Threads

  1. Objects disposed incorrectly
    By Osiris43 in forum .NET
    Replies: 1
    Last Post: 08-04-2006, 12:15 PM
  2. JDOM Classpath Help Required
    By kpandya in forum Java
    Replies: 5
    Last Post: 01-15-2006, 08:10 PM
  3. Highschool Java Class Investigation
    By rsixtyone in forum Java
    Replies: 3
    Last Post: 10-12-2005, 06:02 AM
  4. Help with class/applet
    By none_none in forum Java
    Replies: 17
    Last Post: 04-28-2005, 03:00 PM
  5. Replies: 5
    Last Post: 10-17-2002, 01:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center