-
Notifications
You must be signed in to change notification settings - Fork 7
/
staticMemberVariableAndMemberFunction.cpp
64 lines (56 loc) · 2.27 KB
/
staticMemberVariableAndMemberFunction.cpp
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include<bits/stdc++.h>
using namespace std;
class Account {
int balance;
static float rateOfInterest; // Static member variable or Class variable
public:
void setBalance(int balance) {
this->balance = balance;
}
int getBalance() {
return balance;
}
static float getRateOfInterest() {
return rateOfInterest;
}
/*
We cannot use this pointer (->) with static static member functions
Example:
this->rateOfInterest=rateOfInterest is not correct and causes CE
*/
static void setRateOfInterest(float r) { // Static member function or Class member function
rateOfInterest = r;
}
};
/*
The line below is extremely important for static member variables.
Without this line an error will occur and we cannot access the value present in rateOfInterest
Error if the line below is not present: undefined reference to `Account::rateOfInterest'
We can also set some default value for the static variable. It is 0 by default
*/
float Account::rateOfInterest = 6.5f;
int main() {
/*
Static Member Variables:
--> Declared inside the class body
--> Also known as class member variables
--> Should be defined outside the class (MANDATORY)
--> They do not belong to any object but belong to the entire class
--> Only one copy of static member variable for the whole class throughout the runtime of the program
--> They can also be used with classname
--> Memory for static member variable only gets alloted after member definition and not after declaration
Since static member variable is class variable, it can be accessed without an object
If it is a public member it can directly be accessed using className::staticaVariableName
But If it is private/ protected we need to have some functions to get and set the values
But if the function is a member function, then it can only be accessed using objects.
But we should be able to access static member variables without objects also.
Thus, static member functions come into picture
Static Member Functions:
--> Can only access static member variables
--> Can also be invoked without an object
*/
Account a1;
a1.setBalance(200);
cout << a1.getBalance() << endl;
cout << Account::getRateOfInterest() << endl;
}