Tutorial Study Image

C++ Basic Syntax

July 20, 2022, Learn eTutorial

The primary topic of discussion in this module will be C++'s basic syntax. The fundamentals of C++ will be covered in this section, along with information on its syntax, variables, operators, loop types, pointers, references, and other program needs. Many of the terms you have already studied in C will be used frequently. We will also go through the details of a C++ program structure.

When we think of a C++ program, we can define it as a group of objects that interact by calling each other's methods. Now let's take a quick look at the definitions of a class, object, method, and instance variable.

Class: A class is a blueprint or template that outlines the characteristics and states that an object of a particular kind can support.

Object: There are states and behaviors in objects. A dog has states like color, breed, and name, as well as actions like tail-wagging, barking, and eating. A class's instances are objects.

Method: A method is just a behavior. Many methods can be found in a class. The logic is created, the data is changed, and all the actions are carried out in the methods.

Instance Variable: A unique set of instance variables is present for every object. The values assigned to these instance variables define the state of an object.

Datatypes in C++

C++ programme Structure

C++ programme Structure  in deatail.

#include <iostream.h>
using namespace std;
int main()
    cout << Hello this is Learn Ttutorial for C++";


Let's examine the various parts of the program mentioned above.

[1]  #include <iostream.h>

Like in a C program, header files are included at the beginning. In this case, the header file iostream gives us access to input and output streams. Predeclared function libraries are contained in header files, which the users can utilize at their convenience.

[2]  using namespace std;

The compiler is instructed to utilize the standard namespace by using the namespace std. The namespace is a collection of variable, class, and object identifiers. The nameSpace can be used in programs in one of two ways: either by including a using statement at the beginning, as we did in the program indicated above or by prefixing the identifier with the name of the namespace using the scope resolution kind of operator.

[3]  main()

The main() function, its return type is int, contains the code that runs the program.

[4]   cout << 

Similar to printf in C language, cout is used to print anything on the screen. The main difference between cout and cin and scanf and printf is that with cout & cin you do not need to mention format specifiers like %d for int etc.

How compiling and execution is done in a C++ program?

Let's take the same example which is given above

Let's examine how to save the file, build the program, and how run it. 

  • First, the text editor will be opened, then enter the code as directed.
  • hello.cpp is used to save the file
  • then open a command prompt after that just move to the directory where we have saved the file.
  • In order to compile the code just type 'g++ hello.cpp'  and finally press the enter key. 
  • The command prompt will forward you to the next line and create an executable file called a.out if your code is error-free.
  • Now to run the program just type ‘a.out' 
  • "Hello this is Learn eTutorial for C++"   will be printed on the window.

$ g++ hello.cpp
$ ./a.out
Hello this is  elearn tutorial for C++   

Make sure hello.cpp is running in the directory where g++ is located and that g++ is in your path.

Semicolons in c++

The semicolon serves as a statement terminator in C++. That is, a semicolon must be used to end each sentence. It denotes the conclusion of a single logical object.

Here are three different statements as an illustration:

a = b;
b = b+ 1;
add(a, b);

Blocks in C++

A block is a group of statements that are logically related and are enclosed in opening and closing braces. Example:

cout << "Hello welcome to elearn tutorials"; // prints Hello welcome to elearn tutorials
return 0;

Control Structures in C++

The compiler reads the code line by line when a program is running (from top to bottom, and for the most part left to right). Calling this as "code flow". The code may reach a point during the top-to-bottom reading process where a choice must be made. The program might jump to a different section of the code depending on the choice. It might even force the compiler to run a particular section again or simply skip a lot of code. 

C++ Variables

The foundation of any programming language is the use of variables.
A variable is just a place to save data for later use. By using a "word" that will describe this information, we can access this value or data. Once declared and defined, they can be used numerous times inside the declared scope.

Is C++ the greatest language for programming?

The solution relies on the situation and the needs. In C++, some jobs can be completed, although slowly. Creating GUI screens for apps is one example.GUI design components are built into other languages like Visual Basic and Python. They are therefore more appropriate for GUI-related tasks. Several scripting languages give applications more programmability. Instead of C++, Basic-based programs like MS Word and even Photoshop are more common. The majority of the most well-known applications still use C++ as their foundation.