-
Notifications
You must be signed in to change notification settings - Fork 28
/
Information_Hiding.cpp
78 lines (63 loc) · 1.86 KB
/
Information_Hiding.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
67
68
69
70
71
72
73
74
75
76
77
78
/*
What is Information Hiding OR Encapsulation or State Based Design ?
The private part of a class (attributes and methods) forms its "implementation" because the class alone should be concerned with it and have the right to change it.
The public part of a class (attributes and methods) constitues its "interface" which is available to all others for using the class
Customarily (a string design guideline), we put:
attributes -> private (Access specifier)
methods -> public (Access specifier)
Becasue :
- The state of an object can be changed only through one of its methods (with the knowledge of the class)
- The behaviour of an object is accessible to others through the methods.
*/
#include <iostream>
#include <vector>
using namespace std;
//Interface
//File: Stack.h
class Stack {
private:
//(This private members will also go to a separate private section (because it's an interface) later when we learn inheritance. For now, keeping it here
char *_data;
int _top;
public:
Stack();
~Stack();
int Empty();
void push(char x);
void pop();
char top();
};
//Implementation : Which writes the body/definition of the functions available in interface above
//File : Stack.cpp
//The header file above should be included here
Stack::Stack():_data(new char[100]), _top(-1) {}
Stack::~Stack() {
delete []_data;
}
int Stack::Empty() {
return _top==-1;
}
void Stack::push(char x) {
_data[++_top] = x;
}
void Stack::pop() {
--_top;
}
char Stack::top() {
return _data[_top];
}
//Application
//File: Separate application file (any other file)
//It will have to #include "Stack.h" to use it
int main() {
Stack s;
char str[10] = "ABC";
for(int i = 0; i<3; i++) {
s.push(str[i]);
}
while(!s.Empty()) {
cout << s.top();
s.pop();
}
return 0;
}