U
utab
Dear all,
I have a little problem about static class members and could not find
that though it is very easy. Could you please help me?
#include <iostream>
using namespace std;
class SavingsAccount{
public:
SavingsAccount(double amount){
savingsBalance=amount;
}
void calculateMonthlyInterest(){
double mi; // monthly interest
mi=savingsBalance*annualInterestRate/12;
savingsBalance+=mi;
}
static void modifyInterestRate(double newrate){
annualInterestRate = newrate;
}
double savingsGet() const{
return savingsBalance;
}
static void setannualInterestRate(double rate){
annualInterestRate=rate;
}
private:
static double annualInterestRate;
double savingsBalance;
};
int main()
{
SavingsAccount saver1(2000.0);
SavingsAccount saver2(3000.0);
SavingsAccount::setannualInterestRate(3.0);
saver1.calculateMonthlyInterest();
saver2.calculateMonthlyInterest();
cout << saver1.savingsGet() << endl;
cout << saver2.savingsGet() << endl;
SavingsAccount::modifyInterestRate(4.0);
saver1.calculateMonthlyInterest();
saver2.calculateMonthlyInterest();
cout << saver1.savingsGet() << endl;
cout << saver2.savingsGet() << endl;
return 0;
}
I have a little problem about static class members and could not find
that though it is very easy. Could you please help me?
#include <iostream>
using namespace std;
class SavingsAccount{
public:
SavingsAccount(double amount){
savingsBalance=amount;
}
void calculateMonthlyInterest(){
double mi; // monthly interest
mi=savingsBalance*annualInterestRate/12;
savingsBalance+=mi;
}
static void modifyInterestRate(double newrate){
annualInterestRate = newrate;
}
double savingsGet() const{
return savingsBalance;
}
static void setannualInterestRate(double rate){
annualInterestRate=rate;
}
private:
static double annualInterestRate;
double savingsBalance;
};
int main()
{
SavingsAccount saver1(2000.0);
SavingsAccount saver2(3000.0);
SavingsAccount::setannualInterestRate(3.0);
saver1.calculateMonthlyInterest();
saver2.calculateMonthlyInterest();
cout << saver1.savingsGet() << endl;
cout << saver2.savingsGet() << endl;
SavingsAccount::modifyInterestRate(4.0);
saver1.calculateMonthlyInterest();
saver2.calculateMonthlyInterest();
cout << saver1.savingsGet() << endl;
cout << saver2.savingsGet() << endl;
return 0;
}