Need help understanding use of static in a class

I am working through an exam review, and I need help understanding this problem.  There are no examples in my textbook that explain this.
Given the following class:

class account {
private:
      double balance;
      static double rate;
public:
      account();
      static void setrate(double);
      void setbalance(double);
      double getrate()
      double getbalance();
      void print() const; // prints the balance and the rate
};

and the following c++ statements:

account::rate = 0;

account myaccount(), youraccount();
myaccount.setbalance(1000);
myaccount.setrate(.06);
youraccount.setbalance(2000);
youraccount.setrate(.04);
account::setrate(.05);
myaccount.print();
youraccount.print();

What is the output?
coririzzoAsked:
Who is Participating?
 
bcladdConnect With a Mentor Commented:
Okay:

static double rate;

Inside the class this declares that there will be a single, class-wide variable called rate. Note that variables (data members) declared inside a class do not have any space set aside for them when they are declared. With regular data members, the space is defined when a variable of the class type is defined. The question is when (and in what compilation unit) is the space for rate (or any other static variable) set aside.

The answer is the declaration of the static variable in one of the .cpp files:

double account::rate;

This should appear at the global scope (not inside of main) and it will set aside the space for the variable that will be used by all accounts. You may, at thie moment of declaration, initialize the variable to 0.

double account::rate = 0;

The line you have in main:

  account::rate = 0;

should not compile because rate is a private variable. You should be able to use account::setrate(0) if you want to zero the code; I am guessing that the line should be outside of main (and is the declaration).

Because the function setrate is declared to be static, it can be called without any account object. That is, it is just like any global function and you don't need to use the dot or arrow notation for calling it "on" an object. If you use its full name, you can call it from anywhere:

  account::setrate(0.05); // full name of function is account::setrate

   myaccount.setrate(0.07); // function is still static _but_ the compiler figures its full name based on the type of myaccount


As mentioned above, static information, the class variables/functions, can be accessed from instances of the class but instance variables and functions (those declared without the static keyword) cannot be accessed from static functions because there is no "this" object inside a static function.

HTH, -bcl

0
 
bcladdCommented:
The point is that the static variable is shared. A static variable is shared across all instances of the class.

Output is

2000 - 0.05
1000 - 0.05

Or something like that (depending on how it prints).

HTH, -bcl
0
 
coririzzoAuthor Commented:
I wrote out the function implementations and tried to compile the code like this, but I get a compiler error C2248: 'account::rate' : cannot access private member declared in class 'account'.  Nothing in the text explains using "account::rate=0;" or "account::setrate(.05);" as statements.  I would also like to understand how this works.  Thanks!

#include <iostream>
#include <iomanip>
using namespace std;

class account {
private:
      double balance;
      static double rate;
public:
      account();
      static void setrate(double);
      void setbalance(double);
      double getrate();
      double getbalance();
      void print() const; // prints the balance and the rate
};

int main()
{
account::rate = 0;
account myaccount, youraccount;
myaccount.setbalance(1000);
myaccount.setrate(.06);
youraccount.setbalance(2000);
youraccount.setrate(.04);
account::setrate(.05);
myaccount.print();
youraccount.print();
}



account::account()
{
      setbalance(0);
      setrate(0);
}

void account::setrate(double r)
{
      rate = r;
}

void account::setbalance(double b)
{
      balance = b;
}

double account::getrate()
{
      return rate;
}

double account::getbalance()
{
      return balance;
}

void account::print() const
{
      cout<< "The balance is: " << balance << " and the rate is: " << rate << endl;
}
0
 
jhshuklaConnect With a Mentor Commented:
>> but I get a compiler error C2248: 'account::rate' : cannot access private member declared in class 'account'.
you cannot access private members of a struct or a class directly. you have to go through the functions defined with public access. so you could either do account::setrate(r) or set the value of rate in a separate cpp file like this.
===========================================
#include "account.h"

account::rate = 0;

account::account()
{
     setbalance(0);
     setrate(0);    //remove this statement because each time an instance of account is created, rate is set to 0.
}

//this function is static because you might want to set the value without actually instantiating an object.
void account::setrate(double r)
{
     rate = r;
}
a "normal" function can access a static variable but a static function cannot access a non-static variable. for example accessing balance in setrate() would give you an error.

...

double account::getrate() //you might want to make this static too.
{
     return rate;
}
...
===========================================
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.