CodeBlanks Logo Icon CodeBlanks

Summary - C++ basics

Combine main, cout and cin to write your first C++ program.

Summary - C++ basics

In this section you learned the building blocks of C++:

  • including a header with #include
  • the main function as the entry point
  • printing text with std::cout
  • reading input with std::cin
  • adding comments with // and /* */

Now you will combine these concepts by writing a small program.

Practice Write a welcome message

Add a std::cout line inside the empty main. The output must be Hello C++. End the line with std::endl.

Starter code

main.cpp
#include <iostream>

int main() {
    return 0;
}

Sample solution

main.cpp
#include <iostream>

int main() {
    std::cout << "Hello C++" << std::endl;
    return 0;
}

Expected output

Hello C++

Practice Commented welcome

Add a // comment to the program and produce the output Let us start with std::cout. End the line with std::endl.

Starter code

main.cpp
#include <iostream>

int main() {
    return 0;
}

Sample solution

main.cpp
#include <iostream>

int main() {
    // Welcome message
    std::cout << "Let us start" << std::endl;
    return 0;
}

Expected output

Let us start

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Program skeleton

Question
#include <iostream>

int ____() {
    return 0;
}
Answer
#include <iostream>

int main() {
    return 0;
}

Explanation: The main function is the entry point of a C++ program.

2 Match the section concepts

Term Meaning
#include <iostream> I/O library
std::cout Writes to screen
std::cin Reads from keyboard
// Single-line comment

All lessons in Beginner