Chapter 1 · Lesson 1 of 5
01. Your first C++ program
What C++ is, the parts every C++ program has (#include, using namespace std and main), and cout to show words on the screen.
C++ grew out of C, and adds ways to build big programs from small pieces. Games, web browsers, banking systems and the apps on your phone are often written in it. Learn C++ and you can build almost anything, fast.
Here's a whole C++ program. It shows one line on the screen.
#include <iostream>using namespace std;
int main() { cout << "Hello, World!" << endl; return 0;}It shows
Hello, World!
#include <iostream> brings in the tools for input and output. using namespace std; lets you write cout instead of the longer std::cout. int main() is where the program starts, and everything it does goes between the curly braces { }.
cout shows things on the screen: what follows << is sent to it. endl ends the line. return 0; says the program finished well, and every instruction ends with a semicolon ;.
Your turn
1Steps
- 1Write a program that prints
I am learning C++!on its own line.
Write it in main.cpp. If it reads input, put the answers in Program input, under the terminal.
Use: cout
Starting code
The editor starts empty. Copy this into it to begin, or write your own from scratch.
#include <iostream>using namespace std;
int main() { // Print your line here
return 0;}2Expected output
It should show
I am learning C++!
3Hints
Stuck? Open one hint at a time: each gives a bit more help.
Hint 1
It's one cout line, inside main, before return 0;.
Hint 2
The words go in double quotes after cout <<, like cout << "…";
Hint 3
Add << endl after the words so the line ends, and don't forget the ; at the end.