Miscellaneous things about C++
Developed by Bjarne Stroustrup in the mid 1980s, C++ was meant to
be object-oriented, but it also sat on top of C, which was
developed in the early 1970s. That means all C code would compile
using a C++ compiler. This is not entirely true, but most C
programs will compile using a C++ compiler.
Some of C++ is cryptic because of acquiring its basics from C.
C was developed in the 1970s and as you can imagine, priorities
and issues were different then. C was designed to be small and fast.
A basic philosophy of C is to compile correct programs quickly.
And you will find that it will try to compile most any code
that you write and will not give error messages for programmer
errors. For example, if you access an element out of bounds of
an array, you will not get an error. Your program may crash depending
on what the subscript value is, but it will generally not complain.
Thus, pay close attention to warnings as most modern compilers
try to help you out.
In C++, it is common to separate the interface from the
implementation.
There are header files, which use extension .h (the interface)
as well as .cpp files holding code (the implementation).
Typically .h files hold function prototypes or class definitions
and the .cpp file will hold the actual function or the
bodies of class functions or methods.
File names for C++ programs don't have to be the same as class names.
Typically though, if not named the same, most people name then
something similar. For example, an example we will look at
is a class wrapper for int arrays, called Array, so we the programmer can
do error checking. The file names used are array.h and array.cpp.
As with all good coding, use meaningful names.
C++ has both local and global everything. In Java, everything is local
to some unit. You have local declarations in classes and local
declarations in methods. This means you can only use the identifier
in that scope.
In C++ you can define things globally. This means that anything
defined globally can be used anywhere in the entire program.
As you might guess, this can be very dangerous as anything defined
globally can be changed by any unit, in any function defined anywhere.
For this reason, you should almost never define things globally.
A few exceptions include const definitions
and some type definitions. Technically class definitions are global
as they are not found in the scope of another unit.