forked from keshavsingh4522/hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fifo.cpp
97 lines (86 loc) · 1.57 KB
/
fifo.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
Author: Misbahud Din
Topic: First In First Out (FIFO)
Description: Pushing Values to an Empty Array and then poping out those values
*/
#include <iostream>
using namespace std;
class FIFO // Parent Class
{
protected:
int st[5] = {}; // An Empty Array
int top;
int tail;
public:
FIFO(): top(-1), tail(-1)
{
// No arg constructor
}
void push(int var) // Pushing values
{
cout << "Push = " << var << endl;
st[++top] = var;
}
int pop() // Take number off Queue
{
return st[++tail]; // Returns the deleted integer
}
};
class FIFO2 : public FIFO // Child Class
{
public:
FIFO2() : FIFO()
{
// No Arg Constructor of Parent Class
}
void push (int var)
{
if (top == 4) // If stack gets full top = size - 1
{
cout << "Fifo is Full" << endl;
}
else
{
// Calling Parent's function
FIFO::push(var);
}
}
int pop ()
{
if ( top == tail) // When top and tail gets equal it means the stack is empty
{
cout << "Fifo is Empty" << endl;
top = -1; // Setting both values to -1 to use later
tail = -1;
return -1;
}
else
{
// Calling Parent's Function
FIFO::pop();
}
}
};
int main()
{
FIFO2 f;
// Pushing 6 values
f.push(11);
f.push(22);
f.push(33);
f.push(44);
f.push(55);
f.push(66); // Full Condition
// Poping values
cout << f.pop() << endl;
cout << f.pop() << endl;
cout << f.pop() << endl;
cout << f.pop() << endl;
cout << f.pop() << endl;
cout << f.pop() << endl; // Empty array
f.push(88);
f.push(99);
cout << f.pop() << endl;
cout << f.pop() << endl;
return 0;
}