Solved

Need help understanding use of static in a class

Posted on 2004-10-02
4
220 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

739 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