0:05

At this juncture of our course, we have already encountered many a type.

We have seen the basic types int and double. Then we have seen more advanced types

such as dynamic arrays which we can even compose :

dynamic arrays of dynamic arrays and so on.

Complex types sometimes make the writing a little tedious.

For this reason, among others we will develop soon,

it can be interesting to define type aliases.

Namely giving an other name to a previously defined type.

This is done as follows :

You start your line with the reserved keyword "typedef",

then you write the name of the type you wish to name anew.

And finally, the new name you wish to give to this type,

finishing with a semicolon.

For example, let us suppose you wish to call "Vecteur" (TN: "Vecteur" means "Vector"),

something that is a dynamic array of double.

How should we proceed? We will start our line

with "typedef". Then comes the type we wish to give a new name to : vector<double>.

And finally, the new name we have chosen for this vector type.

Starting with this line, you will be able to use

either the old name vector<double> or, if you want, your new name:

"Vecteur" with a capital V here is now strictly equivalent to vector<double>,

it is an alias for vector<double>.

For example, you can now declare a variable v of type "Vecteur",

which is a dynamic array of doubles.

Of course, you can keep the old syntax,

you had previously. For exemple, you can initialize a vector v2,

with a size of 3 and a certain value, for example.

Now, the type "Vecteur", as has been specified here,

is strictly equivalent to vector <double>.

You can even reuse this type itself in another type.

For example, you can now define the type "Matrice" (TN:"matrix") as a

dynamic array of "Vecteur",

which has been previously defined.

This is done with this line : "typedef vector<Vecteur> Matrice"

Vecteur being the type which has already been redefined. Now, the type "Matrice",

is a dynamic array of dynamic array of doubles.

Instead of writing, for example : "vector <vector <double> > m;" ,

thanks to this line, you can now simply write "Matrice m;"

which is quite a lot simpler.

You can also use the initialization.

Matrice here is the newly defined type.

"rotation" is the variable name,

and the initialization is here, between parentheses.

We are initializing a dynamic array of dynamic array,

specifying that the variable "rotation" is initialized to 3.

3 what ? 3 dynamic arrays of double.

But since we have already defined "Vecteur" why not use it?

We can use three "Vecteur". Each Vecteur is here of size 3 and has all its values initialized
with the value 1.

Therefore, we will create a "Matrice" named "rotation"

which will be a dynamic array of 3 dynamic arrays of 3 doubles,

each one with the value 1.

These type aliases, these "typedef", are useful for three reasons :

First of all, the declaration of arrays, which we have just seen.

This will improve the reading,

writing, and manipulating of vectors and matrices.

It will also make it easier to identify concepts : the concept of vectors

and the concept of matrices.

The "typedef"s let us clearly define and identify the concepts;

this is probably the most important use of these "typedef"s.

So, we can easily identify the objects we are working with.

Let us take an example. Let us suppose that we have a program

in which we need to manipulate distances. We have decided these distances

will be integers. So, we have a distance d,

declared as an integer. For example, we have here a distance d2,

declared as an integer.

Let us suppose that we also have numbers.

So, we have a number "nb" of the type integer.

We may also have temperatures and so on.

In our programs, we thus have a lot of integers,

sharing the same type but representing completely different concepts :

distances, temperatures, numbers.

Now, let us suppose that we decide to modify our program

so that the distances will now be of the type double.

The question is : among all these ints,

scattered all around our program,

which ones will have to be changed to double, which ones will stay ints ?

If you had defined a "typedef",

the solution would have been much easier.

If you have a "typedef" like here, "typedef int Distance",

this means that the "Distance" has now become

a new type, representing an integer. We can now freely

use this new type, this type alias

in order to represent lenghts. The variable "maLongueur" (TN:"myLenght).

At this point of the previous example, the variables d and d2 would have been distances.

Therefore, if we need to modify distances so that

they are no longer integers but doubles,

we only need to modify one single line -the "typedef" line.

We simply replace the "int" by "double" like this.

Through this one change of the "typedef" line,

all our distances will instantly become doubles;

we needed to change neither the numbers nor the temperatures.

The use of "typedef" as we have just seen

permits a better identification of the concepts; this is essential

in order to make your programs all the clearer

and more readable.

In the same time, it will also facilitate subsequent

changes such as the modification of int to double,

here for the distances.

Therefore, modifications will become much easier.

The usefulness of concepts identification through "typedef"s

is also illustrated by the writing of function parameters.

The writing of the functions will indeed become

much clearer and more compact.

Let us take an example of a function which would be the dot product.

The dot product of two vectors. If we write it like this,

we can clearly see that these are two vectors returning a vector.

This is made possible by this line here,

placed at the beginning of the program :

"typedef vector<double> Vecteur;" .

Thanks to this line, it is much clearer to read

that the dot product returns a vector and takes two vectors

as arguments. Without it, we would have to write

"vector<double> produit_vectoriel (vector<double>, vector<double>);

Obviously this is much less readable and understandable.

For all these reasons, and fundamentally the first one

-identifying the concepts-

we warmly advise you to use them whenever possible.

Every time you have the chance to

give a clear type to a variable, please do so : this will enhance de quality of your programs.