-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstaticData.cpp
63 lines (51 loc) · 1.05 KB
/
staticData.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
/*
* Program to understanding static member data/functions
* of a class
*/
#include <iostream>
using namespace std;
class Car {
// by default, members are private
int price;
string model;
string color;
string company;
// counter is a static variable
// used to count how many cars have been produced
public:
// counter should be public to make it accessible outside
static int counter;
void setPrice(int p) {
price = p;
}
void setModel(string model) {
this->model = model;
}
void setColor(string color) {
this->color = color;
}
void setCompany(string company) {
this->company = company;
}
// static function
// can only access static member data
static int getCount() {
return counter;
}
Car() {
price = 100000;
model = "A1";
color = "Black";
company = "Maruti";
// increment the counter
counter ++;
}
};
// initialize the static variable
int Car:: counter = 0;
int main () {
Car c1, c2, c3;
// cout << "No. of cars: " << Car::counter << endl;
cout << "No. of cars: " << Car::getCount() << endl;
return 0;
}