cpp program structure

C++ program structure

c++ program structure picture

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

/* This is Document section , it is tell about the program */
#include <iostream>
using namespace std;
int main()
{
cout<<"C++ is better than c \n"; //C++ statement
}

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.

  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise