What does making this static accomplish?

What is the difference between these two pieces of code and what exactly does the "static" label accomplish? What does it do and why's it added on? What kind of difference does it make?

Thank you for any and all help, hope you're all having a nice day!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 #ifndef BUDGET_H
 #define BUDGET_H

 class Budget
 {
 private:
 static double corpBudget;
 double divBudget;
 public:
 Budget() { divBudget = 0; }
 void addBudget(double b)
 { divBudget += b; corpBudget += divBudget; }
 double getDivBudget() { return divBudget; }
 static double getCorpBudget() { return corpBudget; }
 static void mainOffice(double);
 };
 #endif 



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 #ifndef BUDGET_H
 #define BUDGET_H

 class Budget
 {
 private:
 static double corpBudget;
 double divBudget;
 public:
 Budget() { divBudget = 0; }
 void addBudget(double b)
 { divBudget += b; corpBudget += divBudget; }
 double getDivBudget() { return divBudget; }
 double getCorpBudget() { return corpBudget; }
 };
 #endif 
To explain it in a simple manner:

" Static member variables of a class are a kind of shared memory (shared member variables) between instance of that class "

" Static member variables retain there value and should be initialized "

For Example if I make your class much simpler like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include<iostream>
using namespace std;

class Employee{

    private:
        static int number_of_employees;

    public:
        Employee(){

            number_of_employees++;
            cout << number_of_employees << endl;

        }

};

//Declaring and Initializing
int Employee::number_of_employees = 0;

int main()
{
    //Declaring
    Employee Mr_A, Mr_B, Mr_C;

    cout << endl;
    cin.ignore();
    return 0;
}


Output will be:

1
2
3
1
2
3


Note: "static variables should be declared outside of the class because a class is a logical abstract, it doesn't hold any allocated memory unless an instance (Object) of that class is declared. On the other hand static member variables should hold a memory location before any instance is created because it has to retain its value for all objects of a same type i.e. All objects of class A{ ... };"
You were not asking about the static member variable, were you?
1
2
3
4
5
6
7
class Foo
{
  int x;
public:
         void bar();
  static void gaz();
};

The usage:
1
2
3
4
Foo::gaz();

Foo f;
f.bar();

Static member variable is like a global variable with the exception that you can limit access to it by making it non-public.

Static member function is like a standalone function, except it can access members of the class (and you can limit who can call it). However, the gaz() above cannot access f.x, because it doesn't have reference to object f.

Static members are a way to group data and methods together with a class. For example, see http://qt-project.org/doc/qt-4.8/qstring.html
The important takeaway is this:
Static Variables allow you to store the number of instances that exist in a certain class - ex: suppose you have a enemy class and you want to know how many enemy objects have been instantiated from that class, you could use a static data member to get this number. (increment it in the constructor of course...)

Static Functions also exists for the entire class and they are often written to work with static data members; static functions cannot access non-static data members.
Thank you everybody for your help!! I really truly appreciate it :D
Registered users can post here. Sign in or register to post.