want to create dynamic arrya in classes

I want to create daynamic array in classes.
then add and delete element from that array i tried it but m still confuse please help... :)

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
  #include<iostream.h>

class set 
{
	private:
		int *arr;
		int size;

	public:
		set()
		{
			arr=NULL;
		}
		set(int *arr)
		{
			arr=new int [size];
		}
		void setsize(int s)
		{
			size=s;
		}
		void insert (int v)
		{
			for(int i=0; i<size; i++)
			{
				arr[i]=v;
			}
		}

		void delValue(int pos)
		{
			
			for(int i=0; i<size; i++)
			{
				if(arr[i]==pos)
				{
					arr[i]=0;
				}
			}
			
		}
		
};


void main ()
{
	set first;
	int size;
	int value;
	int delPos;
	cout<<"Enter Size of set"<<endl;
	cin>>size;
	first.setsize(size);
	cout<<"Enter "<< size<<" Element of Set:";
	for (int i=0; i<size; i++)
	{
		cin>>value;  
		first.insert(value);
	}
	cout<<"enter Position of element you want to delete:";
	cin>>delPos;
	first.delValue(delPos);
	
}
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
#include<iostream> ///
using namespace std;
class set 
{
	private:
		int *arr;
		int size;

	public:

		set(int siz) ///
		{
			arr=new int [siz];
			size= siz;
		}

		void insert () ///
		{
			cout << "Enter " << size << " elements of Set: \n";
			for (int i=0; i<size; i++)			
				cin >> arr[i];
		}

		void modifyValue(int mdfy)
		{
			cout<< "enter Position of element you want to modify\n"; //
			int k;
			cin >> k;
			arr[k] = mdfy;			
		}	

		void display() 
		{
			cout<< "the values in *arr are: \n";
			for (int i=0; i<size; i++)			
				cout << arr[i] << " ";
		}

		~set() {delete[] arr;} //destructor
};


int main () ///
{	
	int size, mm=55;
	cout << "Enter Size of set " << endl;
	cin >> size;	
	set first(size);
	first.insert();
	first.modifyValue(mm);	// 
	first.display();

return 0;	
}
Last edited on
Topic archived. No new replies allowed.