C++

C++ Syntax

C++ Syntax

This section lays out basic C++ syntax.  The first step to learn any language is to study its rules and regulations which together are called as syntax. Formally, the term “Syntax” means an approved set of pre-defined protocols or rules that we need to follow while working in a programming language. Just like any other programming language, C++ has its own unique syntax.

So let us begin with a simple “Hello World!” program. Open any IDE or text editor and create file HelloWorld.cpp with following content.

// Basic header file for giving Input/output function
# include<iostream>
//Standard namespace
using namespace std;
/* Entry point of the code */
int main()
{
	//Print Hello world on console and exit
	cout<<"Hello world!"<<endl;	
	return 0;
}

Now run this program from command prompt

(base) 10:28:00:~ % g++ hello.cpp  

(base) 10:28:03:~ % ./a.out 

Hello world!

For this code to run, g++ should be in your PATH environment variable and your present working directory should be the one containing file hello.cpp.

For bigger projects containing many files, you can compile the project by writing a makefile. 

Let us understand this program line by line. Set line number in your ide (in vim use set nu option) and paste the code here

 1 // Basic header file for giving Input/output function
  2 # include<iostream>
  3 
  4 //Standard namespace
  5 using namespace std;
  6 /* Entry point of the code */
  7 int main()
  8 {
  9         //Print Hello world on console and exit
 10         cout<<"Hello world!"<<endl;
 11         return 0;
 12 }
 13

Line 1 is a comment for the reader as to that why we are including the iostream header file. Comments are ignored by the compiler. C++ ignores white space. Whitespace denotes blanks, tabs, newline characters and comments. Whitespace gives logical separation and is used by compiler to identify where one element in a statement ends, and the next element begins.

Line 2 tells compiler to include standard header iostream. Iostream is needed for input output functionality to a stream (console in this case). The compiler will look into the standard path for this. A user can include his own header file say XX.h that is located in same directory by simply writing #include “XX.h” 

Line 3 is blank (kept blank for readability)

Line 4 is comment to denote we are using standard namespace

Line 5 tells compiler to use standard namespace in this file. Namespace is collection of names for objects and variables from the standard library. Namespaces allow us to group named entities into small and narrow scope that can be accessed privately for different programmes. This allows organizing the elements of programs into different logical scopes referred to by name. If you do not want to include standard namespace then you have to add std:: prefix before every standard object e.g. std::cout<<”…”;

Line 6 gives comment about main() to tell reader that it is the main entry point of this code. This means it is the first function that is called when the program executes

Line 7 starts definition of main(). Any code for a method that is written in {} gets executed.

Line 8 and 12 are curly brackets that enclose the body of main()

Line 9 gives comment 

Line 10 uses cout function to print Hello world! On to console and then leave a line

Line 11 gives return statement with return code telling compiler to take control back from this program. Return code 0 denotes successful execution.

A typical C++ program has a collection of objects such as class, object, methods, interface, directives, macros, pre-processor statements, variables, global etc. An Object is basic entity in C++ and it can be anything such as circle, point, cylinder. An object has attributes and behaviours. Example: A car has properties - colour, name, brand, price etc.; a car object has behaviours (basically methods) - start/stop, drive, brake, service etc. An object is an instance of a class which is a blueprint describing the behaviours/states that object of its type support. A method is basically a behaviour or action. A class can contain many methods. It is in methods where the logics are written, data is manipulated, and all the actions are executed. Every object has its unique set of instance variables. An object's state is created by the values assigned to these instance variables.

In C++, the semicolon is a statement terminator. That is, each individual statement must be ended with a semicolon. It indicates the end of one logical entity. E.g.

C++ does not recognize the end of the line as a terminator. For this reason, it does not matter where you put a statement in a line. For example block 1 and block 2 are same from compiler point of view.

Block 1

x = 10;

y = 29;

mul(x, y);

Block 2

x = 10; y = 29; mul(x, y);

C++ identifier is a name used to identify a variable, function, class, module, or any other user-defined item. It can start with A to Z or a to z or an underscore (_) followed by zero or more letters, underscores, and digits (0 to 9). However, C++ does not allow special characters such as @, $, and % within identifiers. E.g

employeeName, x, 55rajesh etc.

Further note that C++ is a case-sensitive programming language. Thus, EmployeeRecord, employeeRecord and employeerecordpower are three different identifiers in C++.

There is a set of words called reserved key that cannot be used to name any identifier in user code. E.g . asm, else, new are reserved keywords.