C++ Basic Syntax - GeeksforGeeks
C++ Basic Syntax - GeeksforGeeks
C++ Basic Syntax - GeeksforGeeks
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.
C++
https://www.geeksforgeeks.org/cpp-basic-syntax/ 1/10
12/4/24, 11:08 AM C++ Basic Syntax - GeeksforGeeks
// Output
21 cout << result << endl;
22
23 // Return Statement
24 return 0;
25 }
Output
58
The program 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. 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 standard input/output
streams that we have used for input and output. #include is a
preprocessor directive using which we import header files.
2. Namespace
3. Main Function
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
https://www.geeksforgeeks.org/cpp-basic-syntax/ 2/10
12/4/24, 11:08 AM C++ Basic Syntax - GeeksforGeeks
are called from the main function. In C++, the main function is required
to return some value indicating the execution status.
4. Blocks
Blocks are the group of statements that are enclosed within { } braces.
The body of the main function is from line #4 to line #9 enclosed within
{ }.
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.
6. Identifiers
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.
In line #7, we have used the cout stream object (declared in the
<iostream> header file) to print the sum of two numbers to the standard
output stream (stdout).
https://www.geeksforgeeks.org/cpp-basic-syntax/ 3/10
12/4/24, 11:08 AM C++ Basic Syntax - GeeksforGeeks
C++
1 #include <iostream>
2 using namespace std;
3
4 class Calculate{
5
6 // Access Modifiers
7 public:
8 // data member
9 int num1 = 50;
10 int num2 = 30;
11
12 // member function
13 int addition() {
14 int result = num1 + num2;
15 cout << result << endl;
16 }
17 };
18
19 int main() {
20
21 // object declaration
Courses @35%22Off C++Calculate
Data Types add;
C++ Input/Output C++ Arrays C++ Pointers C++ OOPs C++ ST
23 // member function calling
24 add.addition();
25
26 return 0;
27 }
Output
80
1. Class
https://www.geeksforgeeks.org/cpp-basic-syntax/ 4/10
12/4/24, 11:08 AM C++ Basic Syntax - GeeksforGeeks
A class is a user-defined data type. A class has its own attributes (data
members) and behavior (member functions). In line #3, we have
declared a class named Calculate and its body expands from line #3 to
line #7.
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.
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.
3. Object
BLACK FRIDAY OFFER! Subscribe for 1 Year and get 1 Extra year of
access completely FREE! Upgrade to GeeksforGeeks Premium today!
https://www.geeksforgeeks.org/cpp-basic-syntax/ 5/10