# Chapter 55: C++ 3

## Basics

C++ is in some ways a combination of Java and C. It has syntax and a lot of other features borrowed from C, but also supports object-oriented programming similar to Java.

If you’re not familiar with either of these languages, you may want to learn one of those first. C++ should probably not be your first language. It is old, cumbersome, dangerous, and has some odd conventions.

One interesting aspect of C++ programming is that it is (typically, for most use cases and with a few small syntax changes) backwards compatibile with C. You can, for all intents and purposes, write a program in C and use a C++ compiler to compile it.

Perhaps one of the most notorious things about C++ (or at least one of the biggest problems the language currently faces) is building, especially when it comes to using and compiling with libraries.

Just like C, C++ can be compiled using Makefiles. But usually, we’ll use another program to automatically generate those Makefiles, called CMake.

This is a bit odd, because we’ve got quite a chain in order to compile our source code.

First we use CMake to generate a Makefile. Then, we use Make to execute this make file, in turn calling g++ or another actual compiler to compile the code.

If we opt to use and IDE instead of compiling on the commandline, we can still use CMake. That’s one of the reasons its so commonly used for C++ compilation.

• On OS X, CMake can generate XCode project files for us.
• On Windows, CMake can generate Visual Studio project files for us.

CMake can actually generate a ton of different build files, so chances are good that if you are using some other IDE it will still be supported.

However, I am an avid Visual Studio user and I kind of don’t care for using CMake to build my projects. So in addition to CMake configuration, I also include ready-built Visual Studio project files for all base codes. So as long as you are using the latest version of Visual Studio (2017), you don’t need to bother with CMake - you can just use my provided files.

## Structs and Objects

C++ objects are similar to C structs, but they allow you to define both data and behavior, unlike C structs which only support data definitions. The behavior of a C++ object takes the form of member functions, which should be familiar to you as a Java programmer.

## References

void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}

// elsewhere

int x, y;
swap(&x, &y);

void swap(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
}

// elsewhere

int x, y;
swap(x, y);


## Inheritance

Check out GeomObject.h.

Syntax for declaring inheritance:

class GeomObject
{
// ...
};

class Sphere : public GeomObject
{
// ...
};


public implies there are other types of inheritance. It’s true, there is protected and private inheritance. They are rarely used.

## Memory Allocation

If we want to create instances of our classes, there are two ways to do it. We can either create them automatically (limited to current scope, sometimes called on the stack) or allocate them dynamicallly (on the heap).

// Automatic allocation
{
Vector v1;
Vector v2 = v1;
v1.p_x = 3.0;
} // v1 and v2 go "out of scope" here and are deallocated

// Dynamic allocation
{
Sphere *sphere = new Sphere();
Sphere *other = new Sphere();

delete other; // 'other' is deallocated now
} // The pointers 'sphere' and 'other' go out of scope, but what they point to is not deallocated.
// The Sphere instance we created on the first line still exists, and 'scene' can still use it


Note: Virtual functions only work on pointers and references. They don’t have to be dynamically allocated, though.

Here, new and delete replace the C usage of malloc and free. There should be no mallocs or frees anywhere in your code!

Note that to allocate arrays of something (instead of just a single copy), there are special commands:

int * my_int_array = new int[10];
my_int_array[4] = 2;
delete[] my_int_array;