Solved

Need help understanding use of static in a class

Posted on 2004-10-02
4
212 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
Comment Utility
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
Comment Utility
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
Comment Utility
>> 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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

762 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now