Getting Started

Course 1 · Ch 1
Getting Started
Everything from the C track, still true — plus what changes the moment OOP gets layered on top

c3-6 named this course directly: the next step once OOP is layered on top of everything the C track already covers. Most of what you already know still applies. This chapter is about the specific places it doesn't.

g++ and clang++

The same underlying toolchains as c1-1's gcc/clang, extended for C++. The situation around build tooling hasn't changed either — no official package manager or build system, exactly as before; make/CMake still fill that gap, and c2-5's own Makefile material transfers directly, unchanged.

$ g++ --version g++ (Ubuntu 13.2.0) 13.2.0

What's Source-Compatible With C

A large majority of valid C code compiles as valid C++, unchanged: variables, control flow, functions, arrays, pointers, and plain structs all carry over. Everything the C track covered about manual memory, bounds checking, and pointer discipline still applies at the same level as before — none of that gets replaced, only added to.

What's Not Compatible

A concrete, common example: in C, malloc's void * return implicitly converts to any pointer type. In C++, it does not — an explicit cast is required.

// valid C, invalid C++: int *p = malloc(10 * sizeof(int)); // required in C++: int *p = (int *)malloc(10 * sizeof(int));

This reflects a broader theme worth noting immediately: C++'s type checking is stricter than C's in several places, not looser.

iostream vs. stdio

cout/cin/cerr are C++'s own I/O streams — using << and >>, which are genuinely overloaded operators (Chapter 6 covers writing your own), not format-string parsing the way printf/scanf work. Both remain fully usable in C++ — <cstdio> still works — but iostream is the idiomatic choice.

#include <iostream> std::cout << "Value: " << 42 << std::endl;

The Compile-Then-Link Model, Unchanged

c1-1's compile-then-link model transfers completely unchanged — same two conceptual steps, same idea of object files and linking. The only visible difference is convention: C++ source files typically use a .cpp extension, and g++ replaces gcc.

The Smallest C++ Program

#include <iostream> int main() { std::cout << "Hello, C++!" << std::endl; return 0; }

Compare directly against c1-1's own hello world — the shape is identical; only the I/O mechanism changed.

ConceptCC++
Print a valueprintf("%d\n", x)std::cout << x << std::endl;
void* implicit conversionallowedrequires an explicit cast
File extension convention.c.cpp
Compilergcc / clangg++ / clang++
The C track's discipline carries forward, not something to relearn
Since most valid C is also valid C++, the bounds-checking habits and manual-control mindset built across the entire C track apply directly here — this course adds to that foundation, it doesn't replace it.
A real "compiles in C, doesn't compile in C++" gotcha
The void *-to-typed-pointer implicit conversion difference is a genuine, common trap when porting C code to C++ or mixing the two — code that compiled cleanly as C can fail to compile once treated as C++, specifically because C++'s type checking is stricter here.

Coding Challenges

Challenge 1

Write a hello-world program using iostream that prints two separate lines with two separate std::cout statements, compile it with g++, and run it.

📄 View solution
Challenge 2

Take a small C program using malloc without an explicit cast on its return value, and compile it first as C (gcc), then attempt to compile the identical file as C++ (g++). Report what happens in each case.

📄 View solution
Challenge 3

Explain why the malloc void* conversion difference is evidence that C++ is, in at least this respect, MORE strictly typed than C — not simply "different" — and why that's a genuinely counter-intuitive fact given C++ is usually introduced as C "with more features."

📄 View solution

Chapter 1 Quick Reference

  • g++/clang++ — same underlying toolchains as C's own compilers, extended
  • Most valid C compiles as valid C++ unchanged — the manual-control discipline carries forward
  • void * requires an explicit cast to a typed pointer in C++ — not implicit, unlike C
  • iostream's cout/cin use overloaded <</>>, not format strings — stdio still works too
  • The compile-then-link model and c2-5's Makefile material transfer completely unchanged
  • Next chapter: variables, references, and a genuine bool primitive from day one