Home >>C++ Tutorial >c++ Syntax

c++ Syntax

Basic Syntax in C++

It is understood that C++ program can be a little bit difficult to understand as it is the collection of objects that generally communicates by invoking each other’s methods. Let us look at the parts of the syntax that are object, methods, and instance variables mean:

  • Class :It is basically a template or blueprint that describes the behavior/states of the object.
  • Methods :Any behavior can be called as the methods in C++. Methods are known as the most important part of the C++ as all the operations like writing logics, manipulating data and all other actions are executed. There can be numerous methods in a class.
  • Object :Objects in C++ consists of behaviors and states, for instance: A tree has a name and color and characteristics. Please note that object is basically an instance of class.
  • Instance Variables :The state of an object is generally created by the values that are assigned to the instance variables as each object has its own unique set of instance variables.

Structure of the C++ Program

Here is a simple code of the C++ that would print the words "Strategic Learning":

// main() is where program execution begins.
int main() 
   cout << "Hello world"; // prints Hello world
   return 0;

Now, let's understand the above mentioned program in brief:

  • The headers that are present in the C++ language contain information that is either necessary or useful to the program. The header named <iostream> is necessary for the above mentioned program.
  • <conio.h> Header file includes the console input output library functions.
  • In the just next line, '// main() is where program execution begins is comment of the single line type that is available in the C ++ programming language. Single-line comments begin with // and end where the line ends.
  • The main function where the program execution begins is int main().
  • The function in the next line that is: cout<<"Hello world" results in displaying the message "Hello world on the screen".
  • In the next line there is return 0 that is used to terminate the main() function and return the 0 value to the process of calling.

Compiling and Executing C++ program

Here are the steps that will make you understand about the process of compiling and executing a program in the C++ language:

  • The first step, add the above code to the text editor.
  • Now, save that file as hello.cpp
  • Now go to the directory where the file is saved by opening the command prompt.
  • Now type, 'g++ hello.cpp' the press enter in order to compile the code. In case of no errors the command prompt would generate a.out executable file.
  • Now you will see that 'Hello world' is printed on the display screen of your computer.

Popular Tutorials