-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtempclass.cpp
More file actions
44 lines (43 loc) · 1.02 KB
/
tempclass.cpp
File metadata and controls
44 lines (43 loc) · 1.02 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
#include<iostream>
using namespace std ;
template<class X> class ArrayList{
private:
struct ControlBlock{
int capacity ;
X *arr_ptr ;
};
ControlBlock *s ;
public:
ArrayList(int capacity){
s = new ControlBlock ;
s ->capacity = capacity ;
s-> arr_ptr = new X[s->capacity];
}
void addElement(int index,X data){
if(index >= 0 && index <= s->capacity-1)
s->arr_ptr[index]= data ;
else
cout<<"\n Array index is not valid";
}
void viewElement(int index,X &data){
if(index >= 0 && index <= s->capacity-1)
data = s->arr_ptr[index];
else
cout<<"\nArray index not valid";
}
void viewList(){
int i ;
for(i =0 ;i< s->capacity ;i++){
cout<<" "<<s->arr_ptr[i];
}
}
};
int main(){
int data ;
ArrayList <float> list1(4);
list1.addElement(0,3.2);
list1.addElement(1,3.5);
list1.addElement(2,4.4);
// list1.addElement(3,78);
list1.viewList();
}