cplusplus.com
C++ : Forum : Beginners : dynamic array
 
cplusplus.com
Information
Documentation
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs


solved dynamic array

rishamessi (46)
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
#include <iostream>
using namespace std ;

class NumList {
	  		  private :
			  		   static int counter ;
			  		   int* num[5] ;
 					   int size ;
			  public  :
 	   		  		   NumList( int , int [] );
 	   		  		   void display() ;
 	   		  		   NumList () ;
 	   		  		  ~NumList () ;  
	  		  };
NumList::NumList()
{
 counter ++ ;
 NumList ( NumList &num ) ;
}

NumList::NumList(int s ,int arr[] )
{
  size=s;
  *num = new int [ size ] ;
  for ( int i=0 ; i < size ; i++ )
  {
   	  *num[i] = arr[i] ;
  }
}

NumList::~NumList()
{
 counter-- ;
 delete num ;
}

void NumList::display()
{
  for ( int i=0 ; i < size ; i++ )
  {
   	  cout << num[i] << endl ;
  }
}

void fun ( NumList& numList )
{
 	 numList.display () ;
}

int NumList:: counter = 0 ;

int main ()
{
 	int arr[3] = {1111, 2222, 3333};
	NumList numList(3, arr);

	numList.display();
	cout << " -------------------------------- " << endl  ;
	fun( numList );
	cout << " -------------------------------- " << endl  ;
	numList.display();
	cout << " -------------------------------- " << endl  ;

	system("pause") ;
	return 0 ;
}


what is the wrong in this code ?????????????
Bazzy (6258)
Line 34: you should call delete[] num
What problems does it give you?
rishamessi (46)
there is an error in line 18
and if i remove this line the prog will give me run time error
plz i want to u to handle up the problem coz i really want to know what is the problem
firedraco (4744)
What are you trying to do on line 18 anyway? I don't get that line either...
wasabi (207)
As well, in case this is a simplified code, your non-parametrized constructor is invalid. You're telling it to add +1 to counter, which hasn't been given an initial value. And I don't see the use of counter-- in the destructor, given how you lose that value anyways and one shouldn't call the destructor explicitly.
rishamessi (46)

this code runs well but i want to use a dynamic array but i cant help me
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
# include <iostream>
using namespace std ;

class NumList {
	  		  private :
			  		   static int counter ;
			  		   int num[5] ;
					   int size ;
			  public  :
 	   		  		   NumList( int , int[5] );
 	   		  		   void display() ;
 	   		  		   NumList () ;
 	   		  		  ~NumList () ;  
	  		  };
NumList::NumList()
{
 counter ++ ;
}

NumList::~NumList()
{
 counter-- ;
}

NumList::NumList(int s ,int arr[] )
{
  size=s;
  for ( int i=0 ; i < size ; i++ )
  {
   	  num[i] = arr[i] ;
  }
}

void NumList::display()
{
  for ( int i=0 ; i < size ; i++ )
  {
   	  cout << num[i] << endl ;
  }
}

void fun ( NumList numList )
{
 	 numList.display () ;
}

int NumList:: counter = 0 ;

int main ()
{
 	int arr[3] = {1111, 2222, 3333};
	NumList numList(3, arr);

	numList.display();
	cout << " -------------------------------- " << endl  ;
	fun( numList );
	cout << " -------------------------------- " << endl  ;
	numList.display();
	cout << " -------------------------------- " << endl  ;

	system("pause") ;
	return 0 ;
}


i want to use a dynamic array and make the prog run plz help me to learn
Last edited on
Bazzy (6258)
In the dynamic one, you are calling an unexisting constructor on line 18
And line 7 should be int* num;
Topic archived. No new replies allowed.