You can simulate sampling and quantization in C++ just as you can in MATLAB. The difference is that you need loops to operate on arrays in C++, while one command in MATLAB can cause an operation to be performed on each element in an array. (You can also write loops in MATLAB, but it isn't necessary where array operations are available.) For example, the equivalent of the MATLAB lines above is given in C++ below. (We use mostly C syntax but, for simplicity, include some C++ features like dynamic allocation with new and the ability to declare variables anywhere in the program.)

int f = 440;
int r = 44100;
double s = 1;
int b = 8;
double* y = new double[r*s];
for (int t = 0; t < r * s; t++)
	y[t] = sin(2*PI*f*(t/(r*s)));

To quantize the samples in y to a bit depth of b, we do this:

int sample_max = pow(2, b-1) – 1;
short* y_quantized = new short[r*s];
for (int i = 0; i < r * s; i++)
	y_quantized[i] = floor(y * sample_max);

The suggested programming exercise linked with this section uses code such as this to implement Algorithm 5.1 for aliasing and to experiment with quantization error at various bit depths.