How to spawn a simple cube in C++?

This is very old code, and has been deprecated since v4.8.
To spawn a cube (v4.11):

UCLASS()
class ABox : public AActor {
    GENERATED_BODY()
public:
    ABox(const FObjectInitializer& ObjectInitializer) {
        auto box = ObjectInitializer.CreateDefaultSubobject<UBoxComponent>(this, FName("My Box"));
        box->bHiddenInGame = false;
        box->Mobility = EComponentMobility::Movable;
        RootComponent = box;
    }
}

…and then:

auto loc = GetActorLocation();
loc.Z += 300.0f;
GetWorld()->SpawnActor<ABox>(loc, GetActorRotation());