5.2.1 · D1C++ Programming

Foundations — C++ as superset of C — key additions

2,862 words13 min readBack to topic

Before we can appreciate what C++ adds, we must be fluent in the vocabulary the parent note throws around. This page defines every symbol and word from absolute zero, in build order, so nothing later surprises you.


0. What is a "program" made of?

Figure — C++ as superset of C — key additions

Look at the figure. Memory is one long strip of numbered boxes. Each box holds a value; each box has an address (its number). A program is code that moves values between boxes. Hold this picture — almost every symbol below points at some part of it.


1. Value, variable, type

The picture: in the memory strip, a variable is one (or several) boxes with a sticky note giving it a name and a type. Why the topic needs it: the whole point of C++'s "stronger typing" and "templates" is about how strictly the compiler checks those labels — so you must know what a type is first.


2. Address, pointer, and the symbols & and *

This is the single most important prerequisite, because C++'s references are defined against pointers.

Figure — C++ as superset of C — key additions

The two symbols you must earn:

The picture (see figure): x is a box holding 5. &x is the number on that box. We store that number in a second box p (the pointer). Drawing p as an arrow to x is exactly what *p undoes: *p walks the arrow and lands back on the 5.

Why the topic needs it: the parent's reference example void inc(int& r) is sold as "a pointer that's always valid and auto-dereferenced." You cannot judge that claim without picturing what a pointer is and what */& do.


3. References — a second name for an existing box

Figure — C++ as superset of C — key additions

The picture (see figure): the box x gets a second sticky-note name r. There is no new box and no visible arrow — r and x label the same container.


4. The heap, malloc/free, and what a constructor is

The picture: the heap is a separate warehouse. malloc hands you a box and an address; free returns it. If you forget to free, the box is rented forever (a memory leak).

Why the topic needs it: the parent contrasts new/delete with malloc/free. The headline difference — new also runs the constructor (and delete the destructor) while malloc/free do not — only makes sense once you know both what raw bytes are and what a constructor is. See new and delete vs malloc and free and Classes and Objects in C++.


5. struct — bundling boxes together

The picture: a bigger box with smaller labelled boxes nested inside. Why the topic needs it: C++'s central addition — the class — is literally a struct that can also hold functions (including that constructor) and hide some inner boxes. You must see the struct first to see what a class adds. See Classes and Objects in C++.


6. Function, parameter, return

Figure — C++ as superset of C — key additions

The picture (see figure): a machine with input chutes (parameters) and one output chute (return) — or, for a void function, no output chute at all. Why the topic needs it: overloading, references, templates, and methods are ALL modifications of "what a function looks like." Nail the plain function first.

The figure shows both: a copy travels into the value-machine; an arrow to the original travels into the reference-machine.


7. Compiler, keyword, identifier

Why the topic needs it: the parent's mistake "int new = 5; is fine in C, illegal in C++" is exactly a keyword clash — new is a keyword in C++ but an ordinary identifier in C. And "compile-time vs runtime" (templates resolve at compile time; virtual costs at runtime) needs the compiler concept. See Compilation Model — C vs C++.


8. Cast and implicit conversion

Why the topic needs it: the flagship "C++ is stricter" example — int* p = malloc(4); compiles in C (silent void*int*) but errors in C++ because C++ forbids that implicit conversion, forcing an explicit (int*) or static_cast<int*>. You need both the word "implicit conversion" and the cast syntax to state and fix that rule.


The prerequisite map

alias, no arrow

never null, never reseated

void star raw bytes

run on allocate

add hidden boxes + methods

one def per type

same name diff params

forbidden silently

Memory boxes and values

Type - label on a box

void - the empty type

Address and pointer

null and garbage pointers

References addition

Heap - malloc and free

Constructor and destructor

new and delete addition

struct - bundled boxes

Classes and OOP addition

Function

Templates addition

Overloading addition

Keyword vs identifier

Namespaces addition

Casts and implicit conversion

Stronger typing addition

Read it top-down: the raw C concepts on top feed each C++ addition below them. Every arrow is "take this, add one twist."


Equipment checklist

Recall Self-test: can you answer each before moving on?

What is the difference between a value and a variable? ::: A value is the data itself (e.g. 5); a variable is a named box in memory that holds a value. What does the void type mean in its three roles? ::: void return = returns nothing; empty/void parameter list = takes no parameters; void* = a pointer to raw bytes of unknown type. What does &x mean in an expression? ::: The address of the box x — where x lives in memory. What does *p do? ::: Follows the pointer p and gives the value in the box it points at. What is a null pointer and how do you write it in C++? ::: A pointer that points at nothing; write it nullptr (C uses NULL/0). Dereferencing it is undefined behaviour, so you test p != nullptr first. What is a reference, and how does it differ from a pointer? ::: A reference is a second name (alias) for an existing box; unlike a pointer it can't be null, can't be reseated, must be initialised at declaration, and needs no * to use. Why does int& r = x; need the = x immediately? ::: A reference must bind to a real box when declared and can never be rebound afterward. What raw thing does malloc return, and what doesn't it do? ::: A block of raw uninitialised bytes (a void* address); it does NOT call any constructor. What is a constructor? ::: A special class function that sets up (initialises) a fresh object the moment it's created; the destructor cleans it up when it dies. What is a struct, and what does a class add to it? ::: A struct bundles variables into one compound type; a class also adds member functions (including constructors) and access hiding (private/public). Give the return type and parameters of int add(int a, int b), and of void greet(). ::: add: returns int, two int params. greet: returns nothing (void), no params. Difference between pass-by-value and pass-by-reference? ::: By value the function gets a copy (caller unchanged); by reference it works on the caller's actual box. What is the difference between a keyword and an identifier? ::: A keyword is reserved by the language; an identifier is a name you choose. You can't use a keyword as an identifier. Write an explicit cast of malloc(4) to int* two ways. ::: C-style (int*)malloc(4); C++ style static_cast<int*>(malloc(4)). What is an implicit conversion, and which one does C++ forbid that C allows? ::: A silent type change by the compiler; C++ forbids implicit void*T* (so malloc needs an explicit cast).


Connections

  • 5.2.01 C++ as superset of C — key additions (Hinglish)
  • Pointers vs References
  • new and delete vs malloc and free
  • Classes and Objects in C++
  • Templates and Generic Programming
  • Namespaces and the std namespace
  • Compilation Model — C vs C++