This is a cool talk by Samathy Barrett (@samathy_baratt) on concepts of C++ for “normal people” 🙂
First why: C and C++ are used by over 400.000 projects on GitHub, and sometimes (even on the Microbit) you can only use some features with C! Wow, I did not know that.
Firstly C versus C++
Differences are the time of conception (1972 and 1983) C does not have a runtime and was developed to replace assembly.
C++ is similar also staticly and weakly typed, but added modern things like operator overloading, object orientation(classes and polymorphism) and introduced exceptions.
The syntax is similar, which is nice for this talk.
A few ways in which C/C++ differ from things like Python:
- Don’t care about lines, you can put everything on one line. But there are guidelines and it is a good thing to follow them.
- Braces! You need to put braces around things, like a function, a class or a loop
- And if there are no braces, you propably need a semicolon to close a statement
Programs and files
There are c files and h files, and typically the c files do things, and the h files have variables and constants, and interfaces of classes. h files can include each other, and (as such) be included multiple times, but within one project,
if you include it twice, you will get the variables twice. Therefore add the once keyword after the include.
Namespaces are used to organize logically grouped code together, like std::cin and std::cout. Code of one namespace can but do not have to be in the same header files.
The C preprocessor does things to the code before the compilation start, like executing the include statements, and compile parts of the code conditionally (like only on a Windows system).
Basic Programming Statements
Basically, everything is this:
If then else is like that, and else if are two separate words
} else if (i ==1)
//do something else
//do something really different
There is only == which is an equality test. So the empty string is the empty string. Not 0 or null or crazy js business.
Variables and Datastructures
Variables must have types, and they are prefixes before the variable, like
int i = 0;
This needs to be put before functions as well,
int double(int i)
There is the void, but that is scary! 😀
There are different types of variables, like int, float, double, but also short int, long int and unsigned int, which are not very well specified by the standard,
which only says that short ints are shorter than normal ints and long ints are longer. This is because C was designed to run on many things, and as such does not assume anything
about you machine. There is a sizeof operation which will help you figure it out.
Variable meta data and operations
This is a good question! How do you know what to do with a string, if you cannot do string. and have the autocomplete help you? Have a look at the standard library, syas Samathy.
Arrays are bits of memory, like all things. They are zero based, and very common because you can store anything in them. In C++ you really want to use vectors rather than arrays.
Strings are just arrays! 🙂 Of characters that is, with a zero sentinel element. They can be both static and dynamic in length.
char str = “Hello” // static
Char* str2 = “World” // dynamic
In C++ there are strings, which are better!
Checking strings is hard(er) because they degrade to pointers, and thus the memory addresses are compared. So use strcmp on the strings, which will return 0 if they are equal, and
otherwise results the character on which they are not equal.
Strucs are like classes in modern languages: a labeled group of related data things. In C++ there are classes, with public and private methods. Again, the interface will be in the h file, while
the implementation is in the c file.
There are no named arguments! You have to put the arguments in in the right order, and with the right types, or you will get a compiler error. Also, the return type has to match,
but there might be some secret casting going on so be careful.
That was a really nice and quick intro!! More resources here: