Introduction to C++

1. Preliminary notes

1.1. Code autograder. These notes are interactive. There are exercises with code autograder. You should use this feature. The auto-grader will help you learn through practice.

You will see problems that ask you to write entire programs. The auto-grader will run your programs, feed them with input values, and tell you whether the output was correct.

There will be some easier exercises. You will only need to write a couple of commands. The auto-grader will fill out the rest. It is going to look like this:

[your short code] + [auto-grader's long code] = [entire program]
This website will then execute the obtained program and tell you whether the execution was successful.

1.2. Your computer. Do not try to learn programming on a tablet, smart phone, or smart watch. Nobody was able to do that. Best not to try.

Please use a computer while going through these notes. Ideally, you should execute the programs on your own computer. You should create a folder specifically for the purposes of your C++ education. As you go through these pages, make the folder full of C++ programs. Do not restrict all your practice to the auto-grader provided by this website.

You will need a C++ compiler and some basic understanding of what C++ program is. The next four sections (and one video!) will teach you how to run programs on your own computer.

In Section 8, you'll get to use the auto-grader.

2. Source, binary, and compiler

Before we can write our first program, we need some basic understanding of the programming languages.

Computers only understand machine language. Programs in machine language are very long and very difficult for humans to understand. Thus, the programmers must use something more friendly. Programmers use high level programming languages. C++ is one such language.

When we write a program, we do it in C++. We obtain a document that is called source. Computers don't understand C++. Hence, the source cannot be executed. We need a translator. That translator is called the compiler. Compiler takes the source and produces the code in machine language. The code in machine language is in completely separate file from the source code. That file is called binary. Binary happens to be much bigger than the source code. Binary is completely unreadable to humans.

The page Video introduction to C++ contains a video. The video shows how to make a source, call the compiler, obtain the binary, and run the binary file.

3. First program

The first problem that we will solve is the following:

Problem 1. The user input consists of two positive integers a and b. Create a program that calculates \(a\cdot (a+7b)\).

We will create the program by first typing the following code and saving it in the file calculation01.cpp.

#include<iostream>
int main(){
   int a;
   int b;
   std::cout<< "What are your two favorite integers? ";
   std::cin >>a;
   std::cin >> b;
   int c;
   c=a*(a+7*b);
   std::cout << c;
   std::cout << std::endl;
   return 0;
}

The first line #include<iostream> pre-loads necessary code that can read user input from the keyboard and print the output to the screen.

Every C++ program must contain the function main. It is OK for now to think of main as a mandatory ritual in which you put the text

int main(){

at the top and the text

return 0;
}

at the bottom of your code.

The line int a; is the first command of our program. Every command must end with ;. This particular command gives the following request to the computer.

Please give me the memory that can hold one integer. This memory will be called a. When in future I ask to store or read from this memory, I will refer to it as a.

The line int b; has a similar effect.

The line std::cout<<"What are your two favorite integers? "; prints the question on the screen. Our understanding of the C++ for the near future will include the following wisdom "In C++ the computer screen is called std::cout".

The line std::cin>>a; asks the computer to take a number from the user input device. The standard input device is called std::cin. At the moment and during the next few weeks the standard input will be the keyboard. Later on, we will work with more advanced input devices where the wires will be directly plug to the brain and thoughts will be so easy to read.

The line std::cin>>b; does an analogous thing.

We then declare the variable c with the command int c;. The command that comes after is used to calculate \(a\cdot(a+7b)\) and store the result in \(c\).

The command std::cout << c; prints the content of the variable c. The command std::cout<< std::endl; prints an end of line character. You may later make an experiment by removing this particular line and see how ugly the output will turn out to be.

4. Compilation of the source

The file calculation01.cpp is called the source file. It is written in C++, which is a language that the computers do not understand. We need to translate the source code to the binary file. We can choose the name firstBinary for our first binary file. The binary file is created if we type the following command in the terminal.

c++ calculation01.cpp -o firstBinary
The binary file is in machine language. The computer understands the machine language. We can execute the file by typing the following in the terminal

./firstBinary

5. Execution of the binary

As mentioned earlier, we type ./firstBinary in the terminal. The computer then asks us about our two favorite integers. Since they are \(17\) and \(5\) and we must never lie to a computer, these are the two things we type in. The program then outputs the number \(884\), which is obtained as the result of the calculation \(17\cdot(17+7\cdot 5)\).

6. Statements. Preprocessor directives. Semicolons

C++ code consists of statements and preprocessor directives. The majority of the commands are regular statements. The preprocessor directives are in the minority. The preprocessor directives start with # and the directive #include<iostream> is usually the only one that the beginners use in the first week of their C++ study.

The most of the code is made out of statements. Each of the statements must end with semicolon (;). The line separator (enter on most of the keyboards) is not sufficient. Thus, the code

a=7
b=8

is not correct. The proper way to assign the value 7 to a and the value 8 to b is

a=7;
b=8;

Also, the line separator is not necessary. The two commands can be placed in the same line. The following code is an equivalent way to assign the number 7 to the variable a and the number 8 to the variable b.

a=7; b=8;

7. Comments

The code can contain texts that will be ignored by the compiler. Such texts are called comments. Programmers write comments to make their code more readable. There are two ways to tell the compiler which sentences are comments that should be ignored.

7.1. Single-line comments

The symbols // denote the start of the comment. The compiler will ignore everything from the occurrence of // until the end of the line.

This is an example of a code with single line comments.

#include<iostream>
int main(){
   int a; int b;
   // The message will be printed for the user
   std::cout<< "Insert the integer a. ";
   // The integer a will be received from the user
   std::cin >>a;
   b=a*a; // The square of a is assigned to b.
   std::cout<<"The square of a is ";
   std::cout<<b;
   std::cout<<std::endl;
   return 0;
}

7.2. Multi-line comments

An alternative way to make a comment is to use /* for start and */ for the end. This is an example.

#include<iostream>
int main(){
   int a; int b;
   /* The user will be asked to insert an integer a
      The square of a will be calculated and the result
      will be stored in the variable b */
   std::cout<< "Insert the integer a. ";
   std::cin >>a;
   b=a*a; 
   std::cout<<"The square of a is ";
   std::cout<<b;
   std::cout<<std::endl;
   return 0;
}

8. Autograder

The course website has the code autograder. This feature can be used to test your solutions for some of the practice problems. The button below the problem will take you to the page where you can insert your code. You will be able to see how it performs on the available test cases.

If you are reading the printed version or the pdf version of these notes, then you will not have the chance to use this important feature. The electronic notes are located at the following address: C++ Lecture Notes

Problem 2.

Write a code that calculates \(a^2+b^2\) and stores the result in the variable m.

You should only write the code that replaces the text // ??? // in the listing below.

#include<iostream>
int main(){
   long a,b,m;
   std::cin>>a>>b;
   // ??? //
   std::cout<<m<<"\n";
   return 0;
}