-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplateClass.cpp
More file actions
63 lines (57 loc) · 1.29 KB
/
Copy pathtemplateClass.cpp
File metadata and controls
63 lines (57 loc) · 1.29 KB
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
//template class or generic class
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
//creating template class
template <class X> class ArrayList
{
private:
struct OneBlock
{
int capacity;
X *arrayPointer;
};
//create a structure pointer variable
OneBlock *s;
public:
//constructor
ArrayList(int cap)
{
s = new OneBlock;
s->capacity = cap;
s->arrayPointer = new X[s->capacity];
}
//add element in arraylist
void addElement(int index,X data)
{
if(index >= 0 && index <= s->capacity-1)
s->arrayPointer[index] = data;
else
cout<<"\n Array index out of range!"<<endl;
}
//checking element in array
void viewElement(int index,X &data)
{
if(index >= 0 && index <= s->capacity-1)
data = s->arrayPointer[index];
else
cout<<"\n Array index is not valid!"<<endl;
}
void viewList()
{
int i;
for(i=0;i< s->capacity;i++)
cout<<" "<<s->arrayPointer[i];
}
};
int main(int argc, char const *argv[])
{
system("cls");
ArrayList <float>list(2);
list.addElement(0,1.0);
list.addElement(1,0.8);
list.viewList();
system("pause");
return 0;
}