What is the basic structure of a C++ program?
Structure of a C++ program
A C++ program is structured in a specific and particular manner. In C++, a program is divided into the following three sections:
- Standard Libraries Section
- Main Function Section
- Function Body Section
For example, let’s look at the implementation of the Hello World program:
#include <iostream>using namespace std;int main() {cout << "Hello World!" << endl;return 0;}
Standard libraries section
#include <iostream>using namespace std;
-
#includeis a specific preprocessor command that effectively copies and pastes the entire text of the file, specified between the angle brackets, into the source code. -
The file
<iostream>, which is a standard file that should come with the C++ compiler, is short for input-output streams. This command contains code for displaying and getting an input from the user. -
namespaceis a prefix that is applied to all the names in a certain set.iostreamfile defines two names used in this program - cout and endl. -
This code is saying: Use the cout and endl tools from the std toolbox.
Main function section
int main() {}
-
The starting point of all C++ programs is the
mainfunction. -
This function is called by the operating system when your program is executed by the computer.
-
{signifies the start of a block of code, and}signifies the end.
Function body section
cout << "Hello World" << endl;return 0;
-
The name
coutis short for character output and displays whatever is between the<<brackets. -
Symbols such as
<<can also behave like functions and are used with the keywordcout. -
The
returnkeyword tells the program to return a value to the functionint main -
After the return statement, execution control returns to the operating system component that launched this program.
-
Execution of the code terminates here.
Coding example
Now, let's conclude this topic with an extended coding example.
#include <iostream>using namespace std;// Function to calculate the factorial of a numberint factorial(int n) {if (n == 0 || n == 1)return 1;elsereturn n * factorial(n - 1);}// Function to print a Fibonacci series up to a given number of termsvoid fibonacci(int n) {int first = 0, second = 1, next;cout << "Fibonacci Series up to " << n << " terms: ";for (int i = 1; i <= n; ++i) {cout << first << " ";next = first + second;first = second;second = next;}cout << endl;}int main() {// Example of using cout to print "Hello World!"cout << "Hello World!" << endl;// Example of using cout to print the factorial of 5cout << "Factorial of 5 is: " << factorial(5) << endl;// Example of using cout to print the Fibonacci series up to 10 termsfibonacci(10);return 0;}
Standard Libraries Section:
Lines 1&2: This section includes the necessary header file
<iostream>for input-output operations and utilizes theusing namespace stddirective to avoid prefixing standard library elements likecoutandendlwithstd::.
Function body section:
This section defines two functions:
factorial()andfibonacci().factorial()computes the factorial of a given number, whilefibonacci()prints the Fibonacci series up to a specified number of terms. These functions are invoked in themain()function for demonstration purposes.
Main Function Section:
Lines 25-36: In this section of the code, The
main()function serves as the entry point for the program execution. It demonstrates the usage ofcoutto output "Hello World!", calculate the factorial of 5 using thefactorial()function, and print the Fibonacci series up to 10 terms using thefibonacci()function.
Free Resources