Explain it simply
Explain it simply
Think of C++ as C with a much bigger toolbox. C gives you numbers, arrays and functions, and asks you to manage everything else, including memory, by hand. C++ keeps all of that and adds tools that do the bookkeeping for you: strings that resize themselves, containers like std::vector, classes that tie data to the functions that use it, and templates that write type-specific code for you.
The important part is that those tools cost almost nothing at run time. A C++ program is still compiled straight to machine code, so it can be as fast as C while reading closer to the way you think about the problem.
Problem → Why → Solution
The problem: in C, text is a fixed-size array you manage yourself
Suppose you want to build a greeting from a name. In C, text lives in a char array whose size you choose in advance. If the name turns out longer than you planned, strcpy and strcat keep writing past the end of the array into whatever memory sits next to it. g++ -Wall -Wextra compiles the code below without a single warning. On our test machine the program crashed with a segmentation fault; on another machine it might seem to work, which is worse.
#include <cstdio>
#include <cstring>
void greet(const char* name) {
char greeting[16]; // room for 15 characters plus '\0'
std::strcpy(greeting, "Hello, ");
std::strcat(greeting, name); // no size check at all
std::printf("%s\n", greeting);
}
int main() {
greet("Thuta"); // fits: 12 characters
greet("Thuta from the C++ class"); // 31 characters: writes past the end
}Why does this happen?
C has no string type, only arrays of characters ending in a '\0' byte, and an array never grows. Every function that writes text trusts you to have reserved enough room. Bjarne Stroustrup created C++ to keep C's speed while adding types that carry this bookkeeping themselves: a std::string knows its own length, grows when you append to it, and frees its memory when it goes out of scope.
Core concept: C's speed plus abstractions that cost nothing extra
Bjarne Stroustrup began C++ at Bell Labs in 1979 as "C with Classes", bringing the class idea from the Simula language into C. It was renamed C++ in 1983; ++ is C's increment operator, so the name means one step beyond C. Because it started as an extension of C, most C code still compiles as C++, and C++ programs can call C libraries directly. That compatibility is a big reason C++ spread so widely.
Like C, C++ is compiled ahead of time to native machine code, and it is statically typed: the type of every variable is fixed at compile time. There is no interpreter and no garbage collector at run time. Objects are created and destroyed at points you can see in the code, which makes performance predictable, an important property for games that must draw a frame every 16 milliseconds or trading systems that measure microseconds.
Stroustrup's design rule is the zero-overhead principle: what you do not use, you do not pay for, and what you do use, you could not hand-code any better. A std::string holds a pointer to its characters plus their length, and does the same allocating and copying you would write by hand in C, only written once, correctly, inside the library.
C++ is multi-paradigm: you can write plain functions as in C, model things with classes, write generic code with templates, and pass small functions around as lambdas. The language is standardized by an ISO committee. C++98 was the first standard, C++11 changed the language so much that the result is called "modern C++", and since then a new version has arrived every three years: C++14, C++17, C++20 and C++23. This course uses C++20.
- C core: types, arrays, functions
- Abstractions: string, vector, classes
- Compiler turns it all to machine code
You choose how high to work; the compiler removes the cost of the abstraction.
General pattern: working with text through std::string
#include <string>
std::string text = "initial value";
text += " more text"; // append: the string grows by itself
std::string joined = first + second; // + joins two strings
text.size(); // number of characters, no '\0' countedstd::string is the C++ answer to C's char arrays: create one from a literal, extend it with += or +, and ask for its length with size(). There is no buffer size to choose. The Strings lesson goes deeper; here it shows the C++ habit of letting a type manage its own memory.
Example: a greeting that cannot overflow
Here is the same job as in the problem section, done with std::string. There is no array size, no strcpy and no '\0' to worry about. However much text you append, the string takes the memory it needs by itself.
#include <iostream>
#include <string>
int main() {
std::string language = "C++";
std::string greeting = "Hello from " + language + "!";
greeting += " This text grows as needed.";
std::cout << greeting << '\n';
std::cout << "Characters: " << greeting.size() << '\n';
return 0;
}Code breakdown
- Line 2
#include <string>The header that declares std::string. It is not C's <cstring> with strcpy and strcat; <string> gives you a class type.
- Line 6
std::string greeting = "Hello from " + language + "!";+ joins the pieces into a new string that is exactly big enough for the result. You never calculate how many characters will fit.
- Line 7
greeting += " This text grows as needed.";Does the job of strcat, but when space runs out the string allocates a bigger block and moves its text there. There is nothing to overflow.
- Line 10
greeting.size()The string tracks its own length, so size() answers 42 immediately. C's strlen has to count characters until it finds the '\0'.
- Line 11
return 0;When main ends, language and greeting are destroyed and their memory is released automatically; there is no free() to call.
Hello from C++! This text grows as needed.
Characters: 42C and C++ side by side
| Task | C | C++ |
|---|---|---|
| Store text | A char array of fixed size | std::string, which grows |
| A list that grows | malloc and realloc by hand | std::vector |
| Data plus the functions that use it | A struct and separate functions | A class with member functions |
| Cleaning up memory | Call free() yourself | Destructors run automatically (RAII) |
| One algorithm for many types | Macros or void pointers | Templates |
Everything in the C column still compiles as C++, so you will meet it in older code. This course uses the C++ column and gives RAII, classes and templates their own lessons.
C++ next to Python
| Question | C++ | Python |
|---|---|---|
| When is the code translated? | Once, before running, by a compiler | While running, by the interpreter |
| When are type errors found? | At compile time | When that line runs |
| Who frees memory? | Destructors, when an object's scope ends | The runtime, with reference counting and a cycle collector |
| What do you trade? | More rules to learn, for speed and control | Less speed, for quick experiments |
The two languages are often partners rather than rivals. Inside many large Python libraries, the part that does the heavy work is written in C or C++, and Python provides a friendly interface for calling it.
In real projects
Where C++ is chosen today, and why
The V8 engine that runs JavaScript in Chrome and Node.js, the Godot game engine, the MongoDB database server and the core of TensorFlow are written in C++. In robotics, the C++ client library of ROS 2 drives control loops, and much automotive and aerospace software is written in C++ under strict coding standards such as MISRA C++. Trading firms pick it for systems where microseconds matter.
The reasons repeat: predictable performance with no garbage-collector pauses, control over memory layout, direct calls into operating system and hardware APIs, and decades of existing libraries. Much of that code is old, so at work you will read both C-style and modern C++, but teams increasingly require the modern style for new code.
Common mistakes
Common mistakes
1. Writing C inside a .cpp file
A program built only from char arrays, malloc/free and printf compiles with a C++ compiler but gets none of the safety C++ offers. Make std::string the default for text and std::vector for lists that grow, and keep the C style for the places where you talk to a C library.
2. Expecting every C program to compile as C++
C++ is stricter than C. int* p = malloc(sizeof(int)); is fine in C, but g++ rejects it with "invalid conversion from 'void*' to 'int*'", because C++ does not convert from void* implicitly. C code that uses C++ keywords such as new, class or delete as variable names will not compile either.
3. Assuming std::string makes every access safe
std::string prevents overflow when you append, but square-bracket indexing is unchecked: s[10] on a three-character string is still undefined behavior. When you want a check, use s.at(10), which throws a std::out_of_range exception for a bad index.
Mini challenge
Mini challenge: a name badge
Build and print a name badge using only std::string. No char arrays and no size calculations in advance.
- Store a first name, a last name and a role in three std::string variables
- Build one string "Name: <first> <last> (<role>)" using + and +=
- Print the badge and its number of characters
- Print a line of '=' exactly as long as the badge above and below it, using std::string(count, '=')
#include <iostream>
#include <string>
int main() {
// TODO: three std::string variables: first name, last name, role
// TODO: badge = "Name: " + first + " " + last
// TODO: append " (" + role + ")" with +=
// TODO: a line of '=' as long as the badge: std::string line(count, '=');
// TODO: print line, badge, line, then "Badge length: <n>"
return 0;
}Expected: For Thuta Aung (C++ learner): a line of 30 '=' characters, Name: Thuta Aung (C++ learner), another line of '=', then Badge length: 30.
Show hint
"Name: " + first works because one side is a std::string. std::string line(badge.size(), '='); creates a string of badge.size() '=' characters.
Show solution
#include <iostream>
#include <string>
int main() {
std::string first = "Thuta";
std::string last = "Aung";
std::string role = "C++ learner";
std::string badge = "Name: " + first + " " + last;
badge += " (" + role + ")";
std::string line(badge.size(), '=');
std::cout << line << '\n' << badge << '\n' << line << '\n';
std::cout << "Badge length: " << badge.size() << '\n';
return 0;
}Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- Bjarne Stroustrup started C++ as "C with Classes" in 1979; it was renamed C++ in 1983.
- C++ is compiled to native code, statically typed, and has no garbage collector.
- The zero-overhead principle: you do not pay for what you do not use, and what you use is as good as hand-written code.
- Types like std::string do the memory bookkeeping that C makes you do by hand.
- Standards run from C++98 to C++23, and this course uses C++20.
The Current ISO C++ Standard (C++23) — Standard C++ Foundation