-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path09_CPP_PRIVATE_AREA_AND_ENCAPSULATION.cpp
59 lines (52 loc) · 1.48 KB
/
09_CPP_PRIVATE_AREA_AND_ENCAPSULATION.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
/////////////////////////////////////////
// WELCOME TO C++ PROGRAMMING! - 2019 //
/////////////////////////////////////////
/* This Code is Written By Ugur Uresin For training purposes! */
/*
To compile the program: g++ filename.cpp -o executableName
To execute the program: ./executableName
*/
// -------------------------------------------------- //
// 09 - PRIVATE AREAS IN CLASS & ENCAPSULATION
// -------------------------------------------------- //
///Example
/* In the example below,
the name can't be reached directly!
Thus, we keep it as it is and also protect it.
However, it's still reachable inside class.
For example the method getName() can reach the "name".
*/
class Employee{
private:
string name;
public:
void getName(){
cout << name << endl;
}
};
/* To make attributes in the private area reachable,
define set and get methods as shown below.
The process of making attributes private and defining set and get methods
is called 'ENCAPSULATION'.
See the example below*/
class Employee{
private:
string name; //name and age attributes are private!
int age; //so, it's not reachable directly!
public:
/* Here, get and set methods are defined so that
user can read and change the values of the attributes indirectly.
*/
void getName(){
return name;
}
void setName(string newname){
name = newname;
}
void getAge(){
return age;
}
void setAge(int newage){
age = newage;
}
};