Structure of C++ Program
Many people believe that C++, an object-oriented programming (OOP) language, is the finest language for developing demanding applications. A superset of the C language is C++. Java, a closely comparable programming language, is based on C++ and is suited for the distribution of program objects across a network like the Internet. In this tutorial, we will explore the basic structure of a C++ Program.
Following are the structural parts of C++ program:
1. Documentation Part |
2. Link Part |
3. Definition Part |
4. Global Declaration Part |
5. Function Definition Part |
6. Main Function Part |
Let's briefly discuss the topics mentioned above to better comprehend them.
1. Documentation Part:
- This part is the first and serves as a description of the program's logic before it is coded.
- Additionally, it can be used to write for a program's purpose.
- The compiler does not compile anything that is written in the documentation section; it is basically a comment.
- Because the application can run without them, the documentation section is optional. The following is a example of the same:
//A C++ Program to Find the max number between the three digits
Or, we can also write it like below:
/* A C++ Program to Find the max number between the three digits
Here we will use if-else statement */
2. Linking Part:
There are two primary components in the linking portion:
Header File:
- Typically, a programme consists of multiple programming components including built-in functions, classes, keywords, constants, operators, etc. which are already predefined in the standard C++ library.
- A suitable header must be provided in the application in order to utilise such pre-defined elements.
- A program's standard headers are provided using the preprocessor instruction #include. The iostream header is utilised in Figure. The contents of the stream are added to the programme when the compiler processes the instruction #include<iostream>. Due to the fact that standard streams specified in< iostream> are the only ones that include input, output, and error capabilities, the programmer is now able to use these facilities. These common streams read and display data in a continuous stream while processing it as a stream of characters. Here is a list of the common streams that <iostream> defines.
#include <iostream>
Namespace:
- A namespace enables the association of different entities, such as classes, objects, functions, and different C++ tokens, under a single word.
- Any user has the ability to establish unique namespaces of their own and use them in other program.
- Namespace std provides declarations for the statements cout, cin, endl, etc. in the code examples below.
using namespace std;
3. Definition Part:
- A few constants are declared and given values using it.
- Anyone can use primitive data types to define their own datatype in this section.
- A compiler directive found in #define instructs the compiler to substitute "Factorial\n" for the message whenever it is found.
- typedef int K instructs the compiler that wherever it encounters K, it should be replaced by an int and that because K has been declared as a datatype, it cannot be used as an identifier.
4. Global Declaration Part:
- Declaring these variables and class definitions at this place makes them global for use throughout the program.
- The scope of the variable declared in this part will last until the end of the program.
- The user-defined functions can also access these variables.
5. Function Declaration Part:
- All of the functions that our main functions require are present in it.
- The user-defined functions are often found in this section.
- This section of the program may be written after the main function, but in order to do so, we must first write the function prototype for the function in this section for which we will write code after the main function.
C++ Code:
//implementation of the function to find max
void max(int a, int b, int c)
{
//utilizing if-else condition
if (a > b)
{
if (a > c)
{
cout << a <<" is the maximum number between the three digits";
}
else
{
cout << c <<" is the maximum number between the three digits";
}
}
else
{
if (b > c)
{
cout << b <<" is the maximum number between the three digits";
}
else
{
cout << c <<" is the maximum number between the three digits";
}
}
}
6. Main Function Part:
- The main function instructs the compiler on where to begin the program's execution. The main function is where the programme is first put into action.
- The main function contains a complete list of the statements that must be executed.
- The curly braces {} that enclose the main function's body contain all the instructions that the compiler is supposed to carry out.
- Control leaves the main function and the programme terminates after all of the main function's instructions have been carried out.
This program will demonstrate this:
C++ Code:
//Documentation Part
/* A C++ Program to Find the max number between the three digits.
Here we will use if-else statement */
//Linking Part
#include <iostream>
using namespace std;
//Definition Part
#define integer int
//Global Declaration Part
integer a = 0;
//Function Part
void max(integer a, integer b, integer c)
{
//utilizing if-else condition
if (a > b)
{
if (a > c)
{
cout << a <<" is the maximum number between the three digits";
}
else
{
cout << c <<" is the maximum number between the three digits";
}
}
else
{
if (b > c)
{
cout << b <<" is the maximum number between the three digits";
}
else
{
cout << c <<" is the maximum number between the three digits";
}
}
}
//Main Function Part
int main()
{
integer b = 0, c = 0;
cout <<"Enter three values to compare : ";
cin >> a >> b >> c;
max (a, b, c);
return 0;
}
If the user puts 65, 78, 69 as inputs,
Output:
Enter three values to compare : 65
78
69
78 is the maximum number between the three digits