Unit 1: Hello, C++
The friendlier cousin.
Unit 1 of 8 in C++ for kids. Its 4 lessons are cout, Real Booleans, Strings At Last and Greeting Machine — below is everything each one explains, and a question or two from it to try.
Every sample on this page was compiled and run with g++ before it shipped, and prints exactly what it says it prints.
This unit is free for ever, because the first two units of every track are. Try it in the app.
💬 cout
No more placeholders
In C you had to match %d to the right value by hand. C++ gives you cout and the << arrows instead — it works out the type for you. endl ends the line.
C++
#include <iostream>
using namespace std;
int main() {
cout << "Hello!" << endl;
return 0;
}
It prints
Hello!
Chaining things together
Every << adds one more thing to the line. Text, numbers, variables — mix them freely, and no placeholder can ever be mismatched.
C++
int cats = 5;
cout << "I have " << cats << " cats" << endl;
It prints
I have 5 cats
Try it yourself
What does C++ use instead of printf?
- cout <<
- console.log
- echo
What does this print?
C++
int n = 3;
cout << "Level " << n << endl;
Answer them in the app
⚖️ Real Booleans
true and false actually exist
Remember C answering questions with 1 and 0? C++ has a proper bool type with the words true and false. It still *prints* as 1 and 0 by default — but you can ask for the words with boolalpha.
C++
bool ready = true;
cout << ready << endl;
cout << boolalpha << ready << endl;
It prints
1 true
Let the compiler figure it out
auto tells C++ to work out the type from whatever you put in the box. Handy when the type is long and obvious.
C++
auto score = 10; // an int
auto price = 2.5; // a double
auto ready = true; // a bool
Try it yourself
What does this print?
C++
cout << (5 > 3) << endl;
Which type holds only true or false?
- int
- bool
- char
- float
Answer them in the app
🔤 Strings At Last
Text that behaves
Text in C was a fiddly array of characters. C++ has a real string that you can glue together with +, measure with .length(), and generally treat like text in Python.
C++
#include <string>
string name = "Ada";
cout << "Hi " + name << endl;
cout << name.length() << endl;
It prints
Hi Ada 3
Try it yourself
What does this print?
C++
string a = "cat";
string b = "fish";
cout << a + b << endl;
What does this print?
C++
string word = "hello";
cout << word.length() << endl;
Answer them in the app
🏆 Greeting Machine
Try it yourself
What does this print?
C++
int a = 6;
int b = 4;
cout << a + b << endl;
Careful — what does this print?
C++
string a = "6";
string b = "4";
cout << a + b << endl;
Answer them in the app