Open In App
Related Articles

C++ Basic Syntax

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

C++ is a general-purpose, object-oriented programming language. It was developed in 1979 by Bjarne Stroustrup at AT & T Bell Laboratory. It is a high-level programming language & advanced version of C programming language. As Compared to other programming languages like Java, and Python, it is the fastest programming language.

What is Syntax?

Syntax refers to the rules and regulations for writing statements in a programming language. They can also be viewed as the grammatical rules defining the structure of a programming language.

The C++ language also has its syntax for the functionalities it provides. Different statements have different syntax specifying their usage but C++ programs also have basic syntax rules that are followed throughout all the programs.

Basic Syntax of a C++ Program

We can learn about basic C++ Syntax using the following program.

Structure of C++ Program

 

The image above shows the basic C++ program that contains header files, main function, namespace declaration, etc. Let’s try to understand them one by one.

1. Header File

The header files contain the definition of the functions and macros we are using in our program. They are defined on the top of the C++ program.

In line #1, we used the #include  <iostream> statement to tell the compiler to include an iostream header file library which stores the definition of the cin and cout methods that we have used for input and output. #include is a preprocessor directive using which we import header files.

Syntax:

#include <library_name>

To know more about header files, please refer to the article – Header Files in C/C++.

2. Namespace

A namespace in C++ is used to provide a scope or a region where we define identifiers. It is used to avoid name conflicts between two identifiers as only unique names can be used as identifiers.

In line #2, we have used the using namespace std statement for specifying that we will be the standard namespace where all the standard library functions are defined.

Syntax:

using namespace std;

To know more about namespaces in C++, please refer to the article – Namespaces in C++.

3. Main Function

Functions are basic building blocks of a C++ program that contains the instructions for performing some specific task. Apart from the instructions present in its body, a function definition also contains information about its return type and parameters. To know more about C++ functions, please refer to the article Functions in C++.

In line #3, we defined the main function as int main(). The main function is the most important part of any C++ program. The program execution always starts from the main function. All the other functions are called from the main function. In C++, the main function is required to return some value indicating the execution status.

Syntax:

int main() {

    ... code ....
    return 0;
}

4. Blocks

Blocks are the group of statements that are enclosed within { } braces. They define the scope of the identifiers and are generally used to enclose the body of functions and control statements.

The body of the main function is from line #4 to line #9 enclosed within  { }.

Syntax:

{
      
  // Body of the Function
  
    return 0;
}

5. Semicolons

As you may have noticed by now, each statement in the above code is followed by a ( ; ) semicolon symbol. It is used to terminate each line of the statement of the program. When the compiler sees this semicolon, it terminates the operation of that line and moves to the next line.

Syntax:

any_statement ;

6. Identifiers

We use identifiers for the naming of variables, functions, and other user-defined data types. An identifier may consist of uppercase and lowercase alphabetical characters, underscore, and digits. The first letter must be an underscore or an alphabet.

Example:

int num1 = 24;
int num2 = 34;

num1 & num2 are the identifiers and int is the data type.

7. Keywords

In the C++ programming language, there are some reserved words that are used for some special meaning in the C++ program. It can’t be used for identifiers.

For example, the words int, return, and using are some keywords used in our program. These all have some predefined meaning in the C++ language.

There are total 95 keywords in C++. These are some keywords.

int           void          if            while          for           auto            bool        break
     
this         static        new            true          false          case            char        class

To know more about Identifiers and Keywords in C++, refer to the article C/C++ Tokens.

8. Basic Output cout

In line #7, we have used the cout method which is the basic output method in C++ to output the sum of two numbers in the standard output stream (stdout).

Syntax:

cout << result << endl;

To know more about basic input and output in C++, please refer to the article – Basic Input and Output in C.

Now, we have a better understanding of the basic syntax structure of the above C++ program. Let’s try to execute this program and see if it works correctly.

C++

// C++ program to demonstrate the basic syntax
// Header File Library
#include <iostream>
  
// Standard Namespace
using namespace std;
  
// Main Function
int main()
{
  
    // Body of the Function
  
    // Declaration of Variable
    int num1 = 24;
    int num2 = 34;
  
    int result = num1 + num2;
  
    // Output
    cout << result << endl;
  
    // Return Statement
    return 0;
}

                    

Output
58

The above program runs correctly and displays the specified output because we have followed the syntax rules of C++. We will learn more about these basic elements later.

Object-Oriented Programming in C++

C++ programming language supports both procedural-oriented and object-oriented programming. The above example is based on the procedural-oriented programming paradigm. So let’s take another example to discuss Object Oriented Programming in C++.

Structure of object oriented Program

 

1. Class

A class is a template of an object. For example, the animal is a class & dog is the object of the animal class. It is a user-defined data type. A class has its own attributes (data members) and behavior (member functions). The first letter of the class name is always capitalized & use the class keyword for creating the class.

In line #3, we have declared a class named Calculate and its body expands from line #3 to line #7.

Syntax:

class class_name{

    // class body

};

To know more about classes in C++, please refer to this article.

2. Data Members & Member Functions

The attributes or data in the class are defined by the data members & the functions that work on these data members are called the member functions.

Example:

class Calculate{
    
    public:
        int num1 = 50;    // data member
        int num2 = 30;    // data member
        
        // member function
        int addition() {
            int result = num1 + num2;
            cout << result << endl;
        }
};

In the above example, num1 and num2 are the data member & addition() is a member function that is working on these two data members. There is a keyword here public that is access modifiers. The access modifier decides who has access to these data members & member functions. public access modifier means these data members & member functions can get access by anyone.

3. Object

The object is an instance of a class. The class itself is just a template that is not allocated any memory. To use the data and methods defined in the class, we have to create an object of that class.

They are declared in the similar way we declare variables in C++.

Syntax:

class_name object_name;

We use dot operator ( . ) for accessing data and methods of an object.

To know more about objects in C++, please refer to the article – C++ Classes and Objects.

Now, let’s execute our code to see the output of the program.

C++

#include <iostream>
using namespace std;
  
class Calculate{
      
      // Access Modifiers
    public:
          // data member
        int num1 = 50;
        int num2 = 30;
          
          // memeber function
        int addition() {
            int result = num1 + num2;
            cout << result << endl;
        }
};
  
int main() {
      
    // object declaration
    Calculate add;
      // member function calling
    add.addition();
  
    return 0;
}

                    

Output
80


Last Updated : 11 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads