You are on page 1of 3

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:

1. Standard Libraries Section


2. Main Function Section
3. 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;
}

1. Standard libraries section

#include <iostream>
using namespace std;

• #include is 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.

• namespace is a prefix that is applied to all the names in a certain set. iostream file defines
two names used in this program - cout and endl.

• This code is saying: Use the cout and endl tools from the std toolbox.

2. Main function section

int main() {}

• The starting point of all C++ programs is the main function.

• 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.


3. Function body section

cout << "Hello World" << endl;


return 0;

• The name cout is short for character output and displays whatever is between the << brackets.

• Symbols such as << can also behave like functions and are used with the keyword cout.

• The return keyword tells the program to return a value to the function int main

• After the return statement, execution control returns to the operating system component that
launched this program.

• Execution of the code terminates here.

Namespace In C++ programming language

Definition and Creation:


Namespaces allow us to group named entities that otherwise would have global scope into narrower
scopes, giving them namespace scope. This allows organizing the elements of programs into different
logical scopes referred to by names.
• Namespace is a feature added in C++ and not present in C.
• A namespace is a declarative region that provides a scope to the identifiers (names of the
types, function, variables etc) inside it.
• Multiple namespace blocks with the same name are allowed. All declarations within those
blocks are declared in the named scope.
A namespace definition begins with the keyword namespace followed by the namespace name as
follows:
namespace namespace_name
{
int x, y; // code declarations where
// x and y are declared in
// namespace_name's scope
}
• Namespace declarations appear only at global scope.
• Namespace declarations can be nested within another namespace.
• Namespace declarations don’t have access specifiers. (Public or private)
• No need to give semicolon after the closing brace of definition of namespace.
• We can split the definition of namespace over several units.

// Creating namespaces
#include <iostream>
using namespace std;
namespace ns1
{
int value() { return 5; }
}
namespace ns2
{
const double x = 100;
double value() { return 2*x; }
}

int main()
{
// Access value function within ns1
cout << ns1::value() << '\n';

// Access value function within ns2


cout << ns2::value() << '\n';

// Access variable x directly


cout << ns2::x << '\n';

return 0;
}

Output:
5
200
100

You might also like