Class with dynamic array how?

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2006
Posts: 5
Reputation: marioxp is an unknown quantity at this point 
Solved Threads: 0
marioxp marioxp is offline Offline
Newbie Poster

Class with dynamic array how?

 
0
  #1
Mar 20th, 2006
I am new in programming and my problem is probably notorious but still is a problem.
I want build class which can produce object which contain two dimensional dynamic array and member function which fill that array.
Question is where I should to create the array, in function or in constructor.
Cod of array is:

int sizeP, stupaca;
char **polje; //dinamicka alokacija polja s pozivacem na pokazivac
polje = new char*[sizeP];
for (int i = 0; i < sizeP; i++)
{
polje[i] = new char[stupaca + 1];
polje[i][0] = stupaca;
for (int j = 1; j <= stupaca; j++)
polje[i][j] = i + 1 + j / 100.;
}


2. question: do I need copy constructor? Probably yes, but I don’t know create it.
3.Can I delete the array in destructor with following cod:

for (int i = 0; i < sizeP; i++)
delete [] polje[i];
delete [] polje;




Thenx
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Class with dynamic array how?

 
0
  #2
Mar 21st, 2006
Well, when someone says dynamic array my knee jerk reaction would be use <vector> from the STL. However, I'm not too sure about 2d vectors although I guess its inherent structure would probably be the same.

If you're intent is to build this from scratch for fun then go for it, although you probably haven't found my reply useful.

hmmm
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 176
Reputation: dubeyprateek is an unknown quantity at this point 
Solved Threads: 22
dubeyprateek's Avatar
dubeyprateek dubeyprateek is offline Offline
Junior Poster

Re: Class with dynamic array how?

 
0
  #3
Mar 21st, 2006
i hope this can help u a little....
  1. #include<iostream>
  2. using namespace std ;
  3.  
  4. class DynamecArray2D ;
  5. class DynamecArray2D {
  6. char **pArray ;
  7. public:
  8. DynamecArray2D() ;
  9. DynamecArray2D(int iRow, int iColumn) ;
  10. DynamecArray2D(DynamecArray2D &) ; //copy constructor
  11. ~DynamecArray2D() ;
  12. };
  13. DynamecArray2D::DynamecArray2D() {
  14. pArray = NULL;
  15. }
  16. DynamecArray2D::DynamecArray2D(int iRow, int iColumn) {
  17. pArray = new char*[iRow] ;
  18. for(int i=0; i<iRow; ++i) {
  19. pArray[i] = new char[iColumn] ;
  20. pArray[i][iColumn-1] = NULL ;
  21. }
  22. }
  23. DynamecArray2D::DynamecArray2D(DynamecArray2D &objDynamecArray2D) { //copy constructor
  24. char **p=NULL;
  25. int i=0,j=0,iColumn;
  26. p=objDynamecArray2D.pArray ;
  27. while(*(p+i)) {
  28. ++i;
  29. }
  30. pArray = new char*[i] ;
  31. iColumn = strlen(objDynamecArray2D.pArray[0]) ;
  32. for(j=0; j<i; ++j) {
  33. pArray[j] = new char[iColumn+1] ;
  34. pArray[j][iColumn-1] = NULL ;
  35. }
  36. i=0;
  37. p=objDynamecArray2D.pArray ;
  38. while(*(p+i)) {
  39. strcpy(*(pArray+i),*(p+i));
  40. ++i;
  41. }
  42. }
  43. DynamecArray2D::~DynamecArray2D() {
  44. int i=0;
  45. while(pArray[i]) {
  46. if(pArray[i]) {
  47. delete pArray[i] ;
  48. }
  49. ++i;
  50. }
  51. if(pArray){
  52. delete []pArray ;
  53. }
  54. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,858
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 755
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Class with dynamic array how?

 
0
  #4
Mar 21st, 2006
>I'm not too sure about 2d vectors although I guess its inherent structure would probably be the same.
  1. std::vector<std::vector<T> > matrix;
std::vector is designed to work the way people expect even in multiple dimensions.
New members chased away this month: 5
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 5
Reputation: marioxp is an unknown quantity at this point 
Solved Threads: 0
marioxp marioxp is offline Offline
Newbie Poster

Re: Class with dynamic array how?

 
0
  #5
Mar 21st, 2006
Thank you I’ll try with that. I think this is it!

I don't use this for vectors I’m trying create from one big string many small strings, and place it in array.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Class with dynamic array how?

 
0
  #6
Mar 21st, 2006
>std::vector<std::vector<T> > matrix;

Thanx _jsw, I was wondering that but I didn't know 4 sure. Do you have an example...

>I don't use this for vectors I’m trying create from one big string many small strings, and place it in array.

Well unless, you just missed the above, you can use a 2D vector to do exactly that. Unless, like I said before ,you're just doing that for fun, practise or just to complicate matters?
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,858
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 755
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Class with dynamic array how?

 
0
  #7
Mar 22nd, 2006
>Do you have an example...
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. int main()
  5. {
  6. typedef std::vector<std::vector<int> > mat_t;
  7.  
  8. mat_t mat ( 10, std::vector<int> ( 10, 0 ) );
  9.  
  10. for ( mat_t::size_type i = 0; i < mat.size(); i++ ) {
  11. for ( mat_t::size_type j = 0; j < mat[i].size(); j++ ) {
  12. if ( j % 2 != 0 )
  13. mat[i][j] = j;
  14. }
  15. }
  16.  
  17. for ( mat_t::size_type i = 0; i < mat.size(); i++ ) {
  18. for ( mat_t::size_type j = 0; j < mat[i].size(); j++ )
  19. std::cout<< mat[i][j] <<' ';
  20. std::cout<<'\n';
  21. }
  22. }
New members chased away this month: 5
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum


Views: 6983 | Replies: 6
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC