Solved

Need help understanding use of static in a class

Posted on 2004-10-02
4
218 Views
Last Modified: 2010-04-02
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?
0
Comment
Question by:coririzzo
  • 2
4 Comments
 
LVL 11

Expert Comment

by:bcladd
ID: 12210079
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
 

Author Comment

by:coririzzo
ID: 12210106
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
 
LVL 9

Assisted Solution

by:jhshukla
jhshukla earned 100 total points
ID: 12210331
>> 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
 
LVL 11

Accepted Solution

by:
bcladd earned 400 total points
ID: 12211171
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

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question