Introduction to C++

1. Preliminary notes

1.1. Code autograder. These notes are interactive, containing exercises with a code autograder. It is recommended that you utilize this feature to enhance your learning experience.

You will encounter problems that require you to write entire programs, which will then be run by the autograder, providing input values and evaluating whether the output is correct.

Some exercises will be relatively simple, requiring you to write only a few commands, with the autograder completing the rest of the code. The format will look like this:

[Your short code] + [Autograder's long code] = [Entire program]

Upon execution, the website will then assess the program and notify you of its success.

1.2. Your computer. It is not recommended to learn programming on a tablet, smart phone, or smart watch. It is best to use a computer while studying these notes. Ideally, you should execute the programs on your computer and create a separate folder dedicated to your C++ education. As you progress through the pages, fill the folder with C++ programs. It is advisable not to restrict all your practice to the auto-grader provided by this website.

To get started, you will need a C++ compiler and some basic understanding of what a C++ program is. The next four sections, along with a video, will teach you how to run programs on your computer.

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

2. Source, binary, and compiler

Before writing our first program, it is crucial to have a basic understanding of programming languages.

Computers can only understand machine language, which consists of lengthy and complex instructions that are difficult for humans to comprehend. Therefore, programmers use high-level programming languages, such as C++.

When we write a program in C++, we create a document called the source. However, computers cannot execute C++ code. Hence, we need a translator, known as a compiler. The compiler converts the source code into machine language code, which is stored in a separate file called the binary. The binary file is significantly larger than the source code and is completely unreadable to humans.

The page Video introduction to C++ contains a video. The video demonstrates how to create 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 of our program, #include<iostream>, pre-loads necessary code to allow for user input from the keyboard and output to the screen.

In C++, every program must contain a main function. For now, you can think of the main function as a required structure where you will input your program's code. You have to write 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. As a beginner, you will work primarily with simple input devices like the keyboard. However, in advanced programming, input devices may become more complex, such as brain-computer interfaces that can read thoughts directly.

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 or instructions and preprocessor directives. The majority of the commands are regular statements. Statements are also called instructions. 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 (or instructions). Each of the instructions 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 includes a code autograder that can be used to test your solutions for some practice problems. To access the autograder, click the button located beneath the problem statement. You will then be taken to a page where you can input your code and see how it performs on the available test cases.

Please note that if you are reading a printed or PDF version of these notes, you may not have access to the online autograder. The electronic notes with the autograder 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;
}