Bonsoir,
j'apprends actuellement le C++ sur la chaine "Un prof très spécial" de Jacques-Olivier Lapeyre

J'ai fait exactement comme lui pour le code mais à la différence c'est que j'ai un corps dumped.

je vous donne le code du main, .h .cc

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
 
#include <iostream>
#include <string>
 
#include "intarray.h"
 
int main ()
{
  using namespace std;
  extintarray A=extintarray(15,15);
  for(int i=0;i<A.length();i++)
    {
      A.set(i,i*5);
      cout<<A.get(i)<<" ";
    }
  cout<<endl;
 
  return 0;
}

Le .h :
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
#ifndef INTARRAY_H
#define INTARRAY_H
 
#include <iostream>
#include <string>
 
const int INTARRAY_DEFAULT_ALLOC = 1024;
const int INTHEAP_DEFAULT_ALLOC = 1024;
 
class intarray {
 protected :
  int len;
  int *data;
 public :
  intarray ();
  intarray (int len);
  intarray (const intarray &A);
  ~intarray ();
  int get (int index);
  void set (int index, int n);
  int length ();
  friend std::ostream & operator<< (std::ostream & o, intarray & A);
};
 
class extintarray : public intarray 
{
 private :
  int alloc;
 protected :
  void alloc_more (int newsize);
 public :
  extintarray (); 
  extintarray (int alloc); 
  extintarray (int len, int alloc);
  ~extintarray ();
  void set (int index, int n); 
};
 
#endif
le .cc :
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
 
#include "intarray.h"
 
std::ostream & operator<< (std::ostream & o, intarray & A)
{
  for (int i = 0; i <= A.len - 2; i++)
    o << A.data[i] << ", ";
  o << A.data[A.len - 1];
  return o;
}
 
intarray::intarray (const intarray &A)
{
  this->len = A.len;
  this->data = new int [A.len];
  for (int i = 0; i < A.len; i++)
    this->data[i] = A.data[i];
}
 
int intarray::length ()
{
  return this->len;
}
 
intarray::intarray ()
{
  this->len = INTARRAY_DEFAULT_ALLOC;
  this->data = new int[INTARRAY_DEFAULT_ALLOC];
}
 
intarray::intarray (int len)
{
  this->len = len;
  this->data = new int[len];
}
 
intarray::~intarray ()
{
  delete[] this->data;
}
 
int intarray::get (int index)
{
  return this->data[index];
}
 
void intarray::set (int index, int n)
{
  this->data[index] = n;
}
 
extintarray::extintarray () : intarray ()
{
  this->alloc = INTARRAY_DEFAULT_ALLOC;
}
 
extintarray::extintarray (int alloc)
{
  this->len = 0;
  this->alloc = alloc;
  this->data = new int[alloc];
}
 
extintarray::extintarray (int len, int alloc)
{
  this->len = len;
  this->alloc = alloc;
  this->data = new int[alloc];
}
 
extintarray::~extintarray ()
{
  delete[] this->data;
}
 
void extintarray::set (int index, int n)
{
  if (index >= this->alloc)
    this->alloc_more (index * 2);
 
  if (index >= this->len)
    this->len = index + 1;
 
  this->data[index] = n;
}
 
void extintarray::alloc_more (int newsize)
{
  int* new_data = new int[newsize];
  for (int i = 0; i < this->len; i++)
    new_data[i] = this->data[i];
  delete[] this->data;
  this->data = new_data;
  this->alloc = newsize;
}