Solved

Need help understanding use of static in a class

Posted on 2004-10-02
4
222 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

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

635 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