Skip to content

Commit 555a3fb

Browse files
authored
Merge pull request cherryWood55#20 from Amasha007/Encapsulation
added Encapsulation
2 parents dfef8ee + 2e1173b commit 555a3fb

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

Encapsulation.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include<iostream>
2+
using namespace std;
3+
class ExampleEncap{
4+
private:
5+
/* Since we have marked these data members private,
6+
* any entity outside this class cannot access these
7+
* data members directly, they have to use getter and
8+
* setter functions.
9+
*/
10+
int num;
11+
char ch;
12+
public:
13+
/* Getter functions to get the value of data members.
14+
* Since these functions are public, they can be accessed
15+
* outside the class, thus provide the access to data members
16+
* through them
17+
*/
18+
int getNum() const {
19+
return num;
20+
}
21+
char getCh() const {
22+
return ch;
23+
}
24+
/* Setter functions, they are called for assigning the values
25+
* to the private data members.
26+
*/
27+
void setNum(int num) {
28+
this->num = num;
29+
}
30+
void setCh(char ch) {
31+
this->ch = ch;
32+
}
33+
};
34+
int main(){
35+
ExampleEncap obj;
36+
obj.setNum(100);
37+
obj.setCh('A');
38+
cout<<obj.getNum()<<endl;
39+
cout<<obj.getCh()<<endl;
40+
return 0;
41+
}

0 commit comments

Comments
 (0)