Your first C++ program

Introduction

C++ powers games, browsers, trading systems and much of the software that has to be fast. It was built on top of C, but it gives you far better tools, such as real strings and lists that manage their own memory. This course starts from nothing, and you do not need to know C first.

How it works

A C++ program is a list of statements, and it starts running in the function called main. Lines starting with #include bring in parts of the standard library. <iostream> provides input and output.

Printing. cout is the output stream. The << operator sends a value to it, and you can chain as many as you like. endl ends the line, and "\n" does the same.

#include <iostream>
using namespace std;

int main() {
    cout << "Hello, C++!" << endl;
    cout << "2 + 3 = " << 2 + 3 << endl;
    return 0;
}
Open in compiler

Calculations happen first. In the second line, 2 + 3 is worked out and then printed.

Reading. cin is the input stream, and >> reads the next value into a variable. Nothing has to say what type to expect, because C++ looks at the variable itself. This is a big improvement over C's %d codes, which have to be matched by hand.

Namespaces. Everything in the standard library is in a namespace called std, so that its names do not clash with yours. The full name of cout is std::cout. The line using namespace std; lets you write the short form. It is fine for small programs, but large projects usually write std:: in full.

Syntax

#include <iostream>
using namespace std;

int main() {
    int n;
    cin >> n;
    cout << "n is " << n << endl;
    return 0;
}

Example

#include <iostream>
#include <string>
using namespace std;

int main() {
    string name;
    int age;
    cin >> name >> age;
    cout << "Hello, " << name << "!" << endl;
    cout << "Next year you will be " << age + 1 << endl;
    return 0;
}

Input used: Mitu 20

Open in compiler

Line by line

  1. #include <string> makes the string type available.
  2. string name; and int age; create two variables.
  3. cin >> name >> age; reads a word and a whole number.
  4. The first cout line joins three pieces of output.
  5. age + 1 is worked out before it is printed.

Expected result

With this input:

Mitu 20

it prints:

Hello, Mitu!
Next year you will be 21

In the real world

Game engines such as Unreal, browsers such as Chrome and most database systems are written in C++. Its great strength is that it gives very high speed while still letting you write in a high-level style.

Common mistakes

  • Forgetting the semicolon at the end of a statement.
  • Mixing up << and >>. The arrows point in the direction the data flows.
  • Forgetting #include <string> when you use string.
  • Expecting cin >> name to read a whole line. It stops at the first space.

Best practices

  • Compile and run after every small change.
  • Fix the first error the compiler reports before looking at the others.
  • Give variables names that say what they hold.

Summary

A C++ program starts in main. cout with << prints, and cin with >> reads. The type of each variable decides how its value is handled, and standard library names live in the namespace std.

Quick check

1. Where does a C++ program start running?
2. Which operator sends a value to cout?
3. What does cin >> name read when the input is Mitu Akter?
4. What is the full name of cout?

Key takeaways

  • Every C++ program starts in main.
  • cout prints with << and cin reads with >>.
  • The type of a variable decides how it is read and printed.

Practice problems for this lesson

My notes

Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.