Sage-Code Laboratory
index<--

C++ Syntax

C++ is a multi-paradigm, strongly typed system programming language. C++ is an increment version of C language. It is inspired from Simula and C. It was created as a pre-processor for C to improve productivity of developers. A similar language Objective-C was created in parallel and used by Apple for its operating system.

History of C++

  1. During 1970 Dennis Ritchie created C Programming language.
  2. Bjarne Stroustrup identified that this OOP features can be included in the software development.
  3. C++ Development started in 1979
  4. Versions: Visual C++, Turbo C++, ANSI C++

C Family

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.

Syntax Rules

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.

Comments

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.

Console Output

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";
Observe: This symbol << is also bit shift left operator. What it does is not important. It was just a poor choice to use the same symbol for two things that have nothing to do with each other. When you learn C++ you will find this can be confusing, but I will point out the mistakes you have to endure to be able to learn the language properly.

Keywords

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

Symbols

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++.

Congratulation! for chosing C++ language. I promice there is an adventure waiting for you. At the end of your journey you will be another person. A grown up software developer. A hero with shield and sword. We are your shield, C++ is your spear. Keep reading read with confidence!
Responsive Image

Legendary C++ Developer


C++ Structure

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:

Program Statement

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.

Program Regions

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:

Running C++ Homeworks

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.

Responsive Image

repl.it demo


Read next: Data Types