Hello everyone, this is Bisqwit.
The topic of today’s video is C++. Many people who watch my videos have some background in C programming, or at least they know some other programming language that is descended from C.
This video, possibly to become a series, is for people who already know C, but would like to learn what is the big deal with C++. The C language standard I go by is ANSI C.
Nobody, who really still prefers to use the antiquated K&R syntax,
can seriously claim that they can’t read ANSI C. ANSI C has been around since 1989 after all. This is an example C program that implements
a bidirectional linked list, which is a commonly used data structure that
provides fast insertion and fast deletion, but linear-time searching.
Only the most rudimentary operations like insertion, deletion, iteration
and cleanup are provided. The main program demonstrates those basic
operations. This program will provide the framework and
foundation for today’s lesson. Before we switch to C++, let’s save the program,
compile it and run it. C++ was originally called “C with classes”,
that’s how it began. Now a class in C++ is just the same thing
as the struct. class is struct. The big deal with classes is that they may
contain “methods”. A method is a function, declared within the
struct, for dealing with the struct’s data. These functions automatically receive an invisible
parameter, a pointer to the struct instance itself, called “this”.
This is why they can use the struct members without a pointer. Compared to the equivalent C code, this results
in significant simplifications that make the code both easier
to read and write. In C++, structs can also be nested,
for the purposes of limiting the scope in which it can be accessed.
It helps avoid namespace pollution a great deal. Now we can use a simple name like “node” for the list nodes
without fear of collision with existing names in the global namespace.
To access a scope-limited name outside its scope,
you must use the double-colon syntax to explicitly specify which namespace you are accessing. Structs are now proper types. A typedef statement
is no longer needed to turn a struct name into a type name. One of the major points of attention in C++
is initialization and deinitialization. C++ calls them constructing and destructing.
Our init() method becomes a constructor, and our done() method becomes a destructor.
These methods are written in special syntax. The “new” keyword, new in C++, does both memory
allocation and constructor calling. The “delete” keyword,
conversely, does both destructing and freeing at once. However, unless you are implementing data
structures like this program here, it is very rare that you actually use the
“new” or “delete” keywords yourself. A much simpler way to create objects is simply
to declare them. Our main() here does just that. When the object is constructed, the constructor
method is automatically called. We no longer need an explicit call to “init()”.
Similarly, when the object goes out of scope, here denoted by the closing curly brace,
the destructor is automatically called. We no longer need an explicit call to “done()”. There is no memory leak in this program. Now, what if we need a linked list for some
other data type than integers? That’s where templates come in.
In C, this would be difficult. In C++, just a couple of changes is enough. The bracket syntax in main() here is called
“specializing”. We “specialize” the list template for two
datatypes, int and pointer to const char.
These two specializations are distinct types, but they are
implemented by the same source code. The template is a little bit like macro, but
much more sophisticated. Now of course, a linked list is one of the
hundreds of things already found in the C++ standard library.
This is why in C++, you almost never need to deal with
memory allocation and deallocation. Opponents of C++ often bring up the question
of performance. What is the cost of all this abstraction?
Let’s study the assembler listing of the iteration loop of the C version first.
These colors and arrows help you match the C code to the assembler listing.
Now what happened to the code when it was translated to C++? Here we go.
Whoa, it didn’t really change that much. How about when we introduced templates?
Huh. That’s still almost completely identical to before.
Okay. Now let’s see the version that used the standard library!
Hmm… Do you see the change? A couple of instructions changed a bit,
but it’s still basically exactly the same code as the C version was. Conclusion: Rewriting your code for greater
clarity, greater reusability, and greater extensivibility
by translating it into C++ will not hurt the performance. Now this was just a short summary that only
stratched the surface of what C++ is good for. Maybe I’ll make more
in the future, who knows? Anyway, thanks for watching. See you next