File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments