C++ is part of a language family called curly bracket languages: C#, Java, Swing, GO, Rust, D, JavaScript, Dart, Kotlin. All these languages use a similar syntax to enclapsulate a block of code in curly brackets {...}. Other languages like Pascal, Ada, Oberon, Julia, PL/SQL, Ruby, Python all use a different block notation based on keywords or spaces.
Be prepared to enter a world of symbols and weird notations that sometimes are related to math but most of the time are pure innovations. If you can memorize these notations you are half way through. The other half is learning the keywords.
This is the most simple to learn. In C++ we have line comments and block comments. You need comments in a program for two purposes: One is to document the code and second is to comment out some code for debugging purposes. Comments are ignored by the compiler.
Block Comments
/*
This comment is a block comment.
It can be on many lines.
*/
Line Comments
// This is a simple line comment
Note: The comments use two symbols that are also mathematical operators: / is division and * is multiplication. However the compiler is smart enough to know that two symbols "//"; represents comments while a single symbol "/"; represents division.
This is not hard is it? But if we create a program only with comments it will nod do anything. So first thing we can do is to print something to the console. Next I will present the syntax for this so be prepared for a surprise.
In Python we just use function print("hello";) but in C++ we have the most weird notation possible: we use keyword "cout" is the "console output" object. Symbol: << is the output operator and ""; is a string. So we feed from from left to right into console. Check this out:
cout << "c++ is complicated";
The only way to learn something is to memorize. You can memorize in two ways: The hard way and the easy way. The hard way is to understand first then memorize. The easy way is to memorize first then understand. Both are good.
I will give you a list of C keywords. Do not try to memorize this, just read and observe. Most of the keywords are English words or abbreviations. Once you have master all these keywords you have finished to learn C. These are also good for C++.
auto | double | int | struct |
break | else | long | switch |
case | enum | register | typedef |
char | extern | return | union |
continue | for | signed | void |
do | if | static | while |
default | goto | sizeof | volatile |
const | float | short | unsigned |
There are not many symbols that we can use in C. These symbols can be used alone or combined in pairs of two. Some symbols are operators, some symbols are used as separators. Together they form language _punctuation_.
, | < | > | . | _ |
( | ) | ; | $ | : |
% | [ | ] | # | ? |
‘ | & | { | } | " |
^ | ! | * | / | | |
– | \ | ~ | + |
Note: For some reason we do not use symbol: @ in C and C++.
Legendary C++ Developer
Now let’s learn some more about a simple C++ program. This program can be saved as file: hello.cpp. This is the code source of the program we are about to analyze next:
#include <iostream>
#include
using namespace std;
// main() is where program execution begins.
int main() {
cout << "Hello World" << "\n"; // prints Hello World
return 0;
}
Suntax Observations:
#include
is a compiler directive. <iostream> is a "header file";. Header files are like a library file. It contains specification of public members. Header file have extension *.h;using namespace
is to allow "std"; members to be used without the "std::"; member notation (prefix) that is otherwise required for "cout"; like this: std::cout
;int main() {...}
declare the main function. Every C++ program must have a main() function that is found by the operating system and executed when we run a program;A statement is like a paragraph in a book. Each statement can start at the left side of the page and can span several lines. Sometimes statements are Indented several spaces from the left side, for improved code readability.
Each statement in C++ code will terminate with semicolumn ";". Sometimes can be on a single line or can span several lines of code. Last line should be terminated with semicolumn ";" unlike a text paragraph that terminate with a dot. A single line of code can have multiple statements separated by ";" and ending with ";".
Multiple statements can be contained in a block of code. The block is enclosed with curly brackets {...} that can be nested. There are block statements that have more than one block of code. Each block represents a different logical path of execution. We call these statements: "control flow" statements.
A program is created using source files. In the source file we have many statements executed from top to bottom. The program file is divided in invisible regions. These regions are the:
For this tutorial we make many examples. Some examples are special design to be run on-line. You can log-in to repl-it website using your GitHub account. Click the link below the image to open, then compile and run your first homework.
Read next: Data Types