Note: Both C++ and OOP in C++ are enormous topics, so the mechanisms/concepts discussed here will be a small, limited subset of the subject. Also, general programming/OOP concepts are not addressed.
General Intricacies of C++
- C++ is wholly compatible with C, which means that you can compile and run C programs as C++ programs, with some changes with the library/header names. The C libraries in C++ are prefixed with c, i.e. the
string.h
iscstring
in C++. - Previously considered “lacking” in conveniences previously, C++ had “recently” (2014) gone through a major standard change called C++11. Features added are..
- Standard Template Library (often abbreviated STL). STL allows for a generic (supporting multiple classes) data structure like vectors (which are arrays (linked-list implementation) with dynamic (changeable) size), queues, stacks, etc.
- STL uses things like:
- Iterators (essentially indexes)
auto
(deduces type, often used for iterators due to their long name), e.g.auto i = numVector.begin();
assigns the iterator type to i.
- Templates are often specified like template
<classname T>.
So, if you see a function specified with such brackets, you can infer that the function is designed to be used with multiple classes.
- STL uses things like:
- There is now
nullptr
to indicate null pointers (NULL
is known to cause issues in error handling)
- Standard Template Library (often abbreviated STL). STL allows for a generic (supporting multiple classes) data structure like vectors (which are arrays (linked-list implementation) with dynamic (changeable) size), queues, stacks, etc.
- Nor do C++ have many of the practical features implemented, so many/most C++ programmers complement the language with a library called Boost, which provides features for Serialization, I/O streams (often used for parsing input/data), and Math.
- C++ print to standard out using
std::cout << "string" << endl;
and standard errorstd::cerr << "string" << endl;
, eschewing commonly used print statement. - Like C, the default in C++ is that values passed into functions are copied, rather than via reference like Java – which can cause performance issues. That is the reason why so many functions have
&
notation next to the parameters, indicating that the parameters are passed via “reference”, or more accurately, via pointers (unfortunately, I can’t go into pointers here). - You can have default arguments/paramters in C++.
- C++ have no garbage collection, which means that you have to destroy everything that has been created dynamically with the
new
operator withdelete
. Use of C’smalloc
andfree
is discouraged.
OOP intricacies with C++
- C++ is more flexible with overriding things in classes in general, allowing the overriding of things operators (+, <<), destructors (things that destroy class constructor).
- C++ have something called namespaces (which is also used in ROS) to organize things variables and functions without the structure of a class.
- Namespaces are used to remediate the problem of global variables. Global variables are evil, as you could unintentionally call global variables from other places of the project without knowing – i.e. if you declare a global variable with a generic name of speed, the variable can now be used/changed everywhere.
- Alternatives to global variables are enums.
- Namespaces are called
name::variable
and declarednamespace name { auto variable = 0; }
- Class functions and variables, too, are called as
class::variable.
- Class functions and variables, too, are called as
- Namespaces are used to remediate the problem of global variables. Global variables are evil, as you could unintentionally call global variables from other places of the project without knowing – i.e. if you declare a global variable with a generic name of speed, the variable can now be used/changed everywhere.
- You indicate public, protected, private parts of class using notations
public:, private:, protected:
- Objects/Instances of the class will be destroyed once the scope of the instance is exited, e.g. with
void init() { Dog d = Dog(); },
Dog d will be destroyed once init function ends. You can avoid this using thenew
operator discussed earlier.
Feel free to inform me if there is any part of C++ code that is confusing – I’ll add onto the list.