@
Corecplusplus
By preprocessor variables in C++ will mean a type alias given to a variable using define for instance, |
I think of
#define
as being like a substitution operation: in the example, every time the pre-processor see's
size
it simply substitutes the literal 10 in it's place. I know you mentioned substitution several times, but It has nothing to do with type alias, the type depends on the context. The same thing happens with
#include
and if one used
#define
to define a macro.
Note that neither macro definitions, and "variables values" with
#define
are recommended. With the values, it's better to use a const variable. With macro's, they are
often discouraged, perhaps better to have a function or lambda.
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines.html#Res-macros
I had a read of your site. I can see you have already put quite a bit of effort. My main comment though: one has to be very careful about doing this. There are many experts out there, and one becomes open to lots of criticism if there is anything wrong. Realise that you are attempting to produce something that is at least as good as what is already there, and avoid providing something sub-standard. The ones that already provide quality resources usually have tremendous knowledge and experience.
I wonder about your motivation for writing this tutorial. It seems you have read one book and liked it, have seen another which you didn't enjoy so much. Perhaps you feel you can write something that explains things better? But there are many other very good books out there:
http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list I have heard (from
JLBorges )that this book is very good: C++ Primer * (Stanley Lippman, Josée Lajoie, and Barbara E. Moo) , a review:
http://accu.org/index.php?module=bookreviews&func=search&rid=1848
IMO, your tutorial seems to have a bias towards C. Realise that C++ is quite a different paradigm to C, and requires a different way of thinking. To me, this would have an impact on what topics were mentioned in a tutorial, and when. For example I wouldn't even mention pointers until the topic of polymorphism was discussed.
There are some parts which are misleading if read in isolation. For example you talk about float having 6 significant figures, but the code demonstrates the default of 6 sf for std::cout, and this is not related and nor is the difference made clear. Also, I would advise not to ever show bad practise in the code, as in using floats in equality conditionals, and initialising floats with more precision than they can handle, and using C style casts. I would have preferred an examples of how float's precision can easily be exceeded, and how to do comparisons properly. It would be better to introduce double early, after all it's the default FP type. I prefer to not use float at all, and only if some library requires it, say.
With C style casts, perhaps you explain the loss of precision in a cast without using a C style cast as an example. C style casts are not recommended.
I could probably mention dozens of things, but here are few examples of what I am talking about.
With the section on references, I found that particularly confusing:
C++ reference-what is?
A reference is nothing but another name for a variable so it does not have an address of it’s own.We can make a reference refer to another variable/object.To make variable a reference we add the symbol ‘&‘ in front of the variable name.
|
.
We can make a reference refer to another variable/object. |
Misleading: A reference can only refer to one variable, it's not possible to reseat them. Although I can see how you meant to create a reference to start with. For me, I never need to create a reference directly, they are always indirect as a function parameter.
To make variable a reference we add the symbol ‘&‘ in front of the variable name. |
Better and clearer to put & after the
type. & before the variable name is easily confused with address of operator.
int i=90 , &ri=i ; ///ri is a reference |
Error prone to declare more than one variable in 1 LOC, consider this:
int * ptr, ptr2;
ptr is a pointer to int, ptr2 is just an int - not what was intended or construed from the variable name.
Also,
ii
is a terrible name for a variable.
With C++, it is better to delay a declaration until one has a sensible value to assign to it. So we have declaration and assignment all in one statement, and this helps with the golden rule of always initialising variables.
*Note the actual address value is represented in hexadecimal format but here it is casted to int type so the address is in integer format. |
Everything is represented in binary format, how it might be displayed or what the default format is, is another matter.
With function arguments and parameters:
In such case the programmer has to pass some values such values which is passed when the function is called is known as parameter.And the parameter when received by the function is known as arguments. |
I think you have that backwards, my friend :+) Consider these:
1 2
|
// A function call
int a = AddInt(10, 20); // 10 and 20 are arguments
|
1 2 3
|
// A function declaration
int AddInt(const int Number1,
const int Number2); // Number1 and Number2 are parameters
|
Pointers:
C++ pointers-what is?
A pointers is a variable that can point to another variable or object.To make variable a pointer we simply add the sign ‘*’ in front of the variable name. |
A pointer is a variable that can hold the address of a variable. Place the * after the
type Placing it before the variable name is easily confused with dereferencing the pointer. I like to put the * right next to the type (no space) to reinforce the association with the type, not the variable:
int* PtrToInt;
There have been lots of arguments about this personal preference, but I find it the most logical.
What is the size of a pointer?.Any pointer whether it is of int type or float type or char type the size is always 4 bytes.
|
That's true for a 32 bit machine, on a 64 bit system (very common now) it's 8 bytes. Btw, sizeof() is very rarely required in C++ (especially with the STL), it is more of a C thing.