-
Notifications
You must be signed in to change notification settings - Fork 0
/
classes.cpp
66 lines (65 loc) · 1.4 KB
/
classes.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
65
66
#include <iostream>
using namespace std;
class Employee
{
private:
int a, b;
static int count; // static variable, initialized with 0 by default
void setData2(int b1);
public:
int c;
void setData1(int a1);
void getData()
{
setData2(10); // This is an example of nesting of member functions
cout << "a = " << a << " (employee no. " << count << ")" << endl;
cout << "b = " << b << " (employee no. " << count << ")" << endl;
cout << "c = " << c << " (employee no. " << count << ")" << endl;
}
static void getCount() // static member function (can only access static variables)
{
cout << "The value of count is " << count << endl;
}
};
int Employee ::count; // must be declared outside the class since it is static
void Employee ::setData1(int a1)
{
a = a1;
count++;
}
/*
ALITER:
void Employee :: setData1(int a)
{
this->a = a;
count++;
}
*/
void Employee ::setData2(int b1)
{
b = b1;
}
/*
ALITER:
void Employee :: setData2(int b)
{
this->b = b;
}
*/
int main()
{
Employee Jack, John, Jill;
Jack.setData1(5); // Jack.a = 5 is not allowed since 'a' is a private member
Jack.c = 15;
Jack.getData();
Employee ::getCount();
John.setData1(5);
John.c = 15;
John.getData();
Employee ::getCount();
Jill.setData1(5);
Jill.c = 15;
Jill.getData();
Employee ::getCount();
return 0;
}