Bonjour ,
Pour un devoir, je suis ramener a creer une classe pour representer un tableau 2D à partir dèun tableau 1D (c'est vraiment inutile mais bon ...).
lèennoncé de l'exercice prcise qu il faut creer un tableau de valeur et un autre de pointeur sur chaque tableau 1D.
Jèai reussi à le faire sans probleme je peux creer mon objet tab2D lèafficher faire un set et get sans probleme.
Je suis maintgenant bloqué sur comment creer un tableau d'objet et instacier mes objets un par un.
voila mon code
Header file
et cpp
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 #pragma once class Tab2D { public: Tab2D(int = 1,int = 1); ~Tab2D(void); Tab2D(const Tab2D&); const Tab2D &operator=(const Tab2D&); void affiche(); void set(int,int,int); void get(int, int); private : int mLines; int mCol; int **mTabLines; int *mElements; };
et finalement le main()
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131 #include "Tab2D.h" #include <iostream> using namespace std; Tab2D::Tab2D(int iLine, int iCol ):mLines(iLine),mCol(iCol) { /*mLines = iLine; mCol = iCol;*/ mTabLines = new int*[mLines]; mElements = new int[mLines*mCol]; int i = 0; while(i < (mLines*mCol)) { *(mElements + i ) = 0; ++i; } int j = 0; int k = 0; while(j < mLines) { mTabLines[j] = (mElements + k); ++j; k+=mCol; } } Tab2D::~Tab2D(void) { if(mTabLines) { delete [] mTabLines; } if(mElements) { delete [] mElements; } } Tab2D::Tab2D(const Tab2D &tab) { mTabLines = new int*[mLines]; mElements = new int[mLines*mCol]; int i = 0; while(i < (mLines*mCol)) { *(mElements + i ) = 0; ++i; } int j = 0; int k = 0; while(j < mLines) { mTabLines[j] = (mElements + k); ++j; k+=mCol; } } const Tab2D& Tab2D::operator =(const Tab2D &tab) { mCol = tab.mCol; mLines = tab.mLines; mTabLines = new int*[mLines]; mElements = new int[mLines*mCol]; int i = 0; while(i < mCol*mLines) { *(mElements + i ) = *(tab.mElements + i); i++; } int j = 0; int k = 0; while(j < mLines) { mTabLines[j] = &tab.mElements[k]; //cout << *mTabLines[j]<<endl; k+=mCol; j++; } return *this; } void Tab2D::affiche() { for( int i =0 ; i < mLines; ++i) { for(int j = 0 ; j < mCol;++j) { cout << mTabLines[i][j] << ' ' ; // or use this one //cout << *(*(mTabLines + i) + j) << ' ' ; } cout << endl; } } void Tab2D::set(int iRow, int iCol, int iValue) { mTabLines[iRow][iCol] = iValue; } void Tab2D::get(int iRow, int iCol) { cout << " Element se trouvant dans " << iRow << ':' << iCol << " est " << mTabLines[iRow][iCol] << endl; }
J'avoue qu ca allait etre trivial avec un vector mais bon c est un devoir :S.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 void main() { /*Tab2D x1(4,6); cout << "----------------Matrice Initiale--------------------------" <<endl; x1.affiche(); x1.set(3,5,10); cout << "----------------Recuperation de valeur--------------------" <<endl; x1.get(3,5); cout << "----------------Matrice apres un set(3,5,10) -------------" << endl; x1.affiche(); cout << "----------------Fin de la premiere partie-----------------" <<endl; */ Tab2D *mTab2D = new Tab2D[5]; for(int i= 0; i < 5 ; ++i) { mTab2D[i] = Tab2D(3,5); mTab2D[i].affiche(); } cout << "Press ENTER to continue..."; cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' ); }
c est cette section qui me fatigue
Merci pour vos reponse et suggestion
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7 Tab2D *mTab2D = new Tab2D[5]; for(int i= 0; i < 5 ; ++i) { mTab2D[i] = Tab2D(3,5); mTab2D[i].affiche(); }
Partager