Introduction to C++
C++ was created by Bjarne Stroustrup in 1985 as "C with classes". It keeps C's speed and control over memory, and adds tools for building large programs: classes and objects, templates, and a rich Standard Library.
Where C++ is used
| Area | Examples |
|---|---|
| Games | |
| Browsers | |
| Databases and compilers | |
| Embedded and real-time | cars, robots, medical devices, |
A first look
C++
#include <iostream>
using namespace std;
int main() {
cout << "Hello, C++!" << endl;
cout << "2 + 3 = " << 2 + 3 << endl;
return 0;
}cout ("character output") sends text to the screen with the << operator, and endl ends the line. You'll take this apart in the next lesson.
C++ and C
Almost all C code is also valid C++, so everything in the C course applies. C++ adds:
| Feature | What it gives you |
|---|---|
cout / cin | type-safe, easy input and output |
| classes | bundle data and functions into objects |
| references | a safer alternative to many pointer uses |
std::string, std::vector | text and resizable lists without manual memory management |
| templates | code that works for any type |
Compiled to machine code
Like C, C++ is compiled. On your own computer you'd write:
Terminal
g++ hello.cpp -o hello
./helloFinished this lesson?Mark it complete to earn +20 XP and move your progress on.