C++ program structure
The class declaration are placed in a header and the definition of member function go into another file.
This approach enables the programmer to separate the abstract
specification of the interface(class definition) from the implementation details(member function definition) finally the main program that uses that class is placed in a third file which includes the previous two files as well as any other files require
Documentation Section:
1. This section comes at first and it is used to document the about the program that the programmer going to code.
2. It can be also used as comment to write for purpose of the program.
3. Whatever written in the documentation section this is comment and also it is skip by the compiler and not compiled by the compiler.
4. Documentation Section is optional.
Example with the simple program with print String
<iostream>
using namespace std;
main()
{
cout<<"C++ is better than c \n";
}
|
Linking Section:
The linking section contains two parts:
Header Files:
1. General program includes various things like operators, functions, constants , variables , classes , keywords etc
2. In order to use pre-defined elements in a program, an appropriate header file should be include or import
3. The Standard headers file are specified in a program by the preprocessor directive #include.
4. The iostream header file is used. When the compiler process the instruction #include,
this includes the contents of the stream in the program. This includes the standard input, output, and error facilities that are provided only by the standard streams defined in header file . These standard streams process the data as a stream of characters, that is, data is read and displayed in a continuous flow. The standard streams defined in header are listed here.
Namespaces:
1. A namespace allow grouping of various entities like classes,functions, objects, and various C++ tokens, etc. under a single name.
2. And any user can create separate own namespaces and can use them in other program.
3. namespace std contains declarations for cout, cin, endl, etc. statements.
Definition Section:
1. This place is used to declare constants and assign them any value.
2. At this section, anyone can define user defined data type or your own datatype using primitive data types.
3. In #define directive is a compiler directive which tells the compiler whenever the text or message is found replace this with “Factorial\n” .
4. typedef int j; this statement telling the compiler that whenever you will encounter j replace it by int and as you have declared k as datatype you cannot use it as an identifier.
Global Declaration Section:
1. In this section the class definitions and variables which will be used in the program are declared to make them global.
2. The scope of the variable declared inside this section lasts until the entire program terminates.
3. These variables can accessible within the user-defined functions also.
Function Declaration Section:
1. This section contains all the functions which we required or our main functions need.
2. Generally, this section contains the User-defined functions.
3. This section of the program can be written after the main function but for this,
4. we will have to write the function prototype or function declaration in this section for the function which for you are going to write code(function definition) after the main function.
Main Function:
1. The main function is the starting point of execution of the program. Compiler search the main function to execute the program
2. All the statements that are to be executed are written inside the main function
3. Once all instructions inside the main function executed control out from the main function and
the program terminates.
4. The compiler executes all the instructions which present inside the curly braces {} which encloses the body of the main function.