C++ Basic Concepts
C++ is a general-purpose coding language based on C language. C++ runs line by line, from top to bottom.
// A comment
#include <iostream>
int main() {
std::cout << "Hello World!\n" ;
retutn 0;
}#include <iostream> is known as a pre-processor directive. It instructs the compiler to locate the file that contains code for a library known as iostream. This library contains code that allows for input and output, such as displaying data in the terminal window or reading input from your keyboard.
Every C++ program must have a function called main(). A function is basically a sequence of instructions for the computer to execute. This main() function houses all of our instructions for our program.
<< is an operator that comes right after it.
\n is a special character that indicates a new line.
; is a punctuation that tells the computer that you are at the end of a statement. It is similar to a period in a sentence.
The return statement is used to end a function. If the program reaches this statement, returning a value of 0 is an indication to the operating system that the code executed successfully. This line of code is optional.
A multi-line comment will comment out multiple lines and is denoted with /* to begin the comment, and */ to end the comment:
/* this is a multi
line comment */The code that we wrtie is called source code. C++ source code files are given the suffix:
.cpp (ex: hello.cpp)
.h (ex: std_lib_facilities.h).

A compiler translates the C++ program into machine language code which it stores on the disk as a file with the extension .o (e.g. hello.o). A linker then links the object code with standard library routines that the program may use and creates an executable image which is also saved on disk, usually as a file with the file name without any extension (e.g. hello).
Error Types in C++
In C++, there are many different ways of classifying errors, but they can be boiled down to four categories.
Compile-time errors
Errors found by the compiler. There are two types of compile-time errors.
Syntax errors
Errors that occur when we violate the rules of C++ syntax. Some common syntax errors are:
Missing semicolon
;Missing closing parenthesis
), square bracket], or curly brace}
Type errors
Errors that occur when there are mismatch between the types we declared. Some common type errors are:
Forgetting to declare a variable
Storing a value into the wrong type
Link-time errors
Errors found by the linker when it is trying to combine object files into an executable program. Sometimes the code compiles fine, but there is still a message because the program needs some function or library that it can’t find. This is known as a link-time error. As our program gets bigger, it is good practice to divide the program into separate files. After compiling them, the linker takes those separate object files and combines them into a single executable file. Link-time errors are found by the linker when it is trying to combine object files into an executable file.
Some common link-time errors:
Using a function that was never defined (more on this later)
Writing
Main()instead ofmain()
Run-time errors
Errors found by checks in a running program. Errors which happen during program execution (run-time) after successful compilation are called run-time errors. Run-time errors occur when a program with no compile-time errors and link-time errors asks the computer to do something that the computer is unable to reliably do. Some common run-time errors:
Division by zero also known as division error. These types of error are hard to find as the compiler doesn’t point to the line at which the error occurs.
Trying to open a file that doesn’t exist
Logic errors
These types of errors which provide incorrect output, but appears to be error-free, are called logical errors. These are one of the most common errors that happen to beginners and also usually the most difficult to find and eliminate. Logic errors don’t have error messages. Sometimes, programmers use a process called test-driven development (TDD), a way to give logic errors error messages. Some common logic errors:
Program logic is flawed
Some “silly” mistake in an
ifstatement or afor/whileloop
Vectors
Arrays
Like vectors, the array is a data structure used in C++ to store a sequential collection of elements. Unlike vectors, its size cannot be changed.
Last updated