C++ Program Structure

The C++ programming language supports both procedure-oriented and object-oriented programming paradigms. So, we can write a C++ program using the POP structure and OOP structure too.

  • Procedure-Oriented Programming Structure
  • Object-Oriented Programming Structure

POP structure of C++ program

We use the following general structure to write C++ programs using Procedure-Oriented Programming Paradigm.

General Structure of C++ Program (POP)
/*documentation*/
pre-processing statements 
global declaration;
void main()
{
    Local declaration;
    Executable statements;
    .
    .
    .
}
User defined functions 
{
    function body
    .
    .
}                               

Let's create a C++ program using the POP structure.

C++ Program to perform addition of two numbers using POP structure
#include < iostream.h > 
int a, b;
void main()
{
    int c;
    void get_data();
    int sum(int, int);
    
    get_data();    
    c = sum(a, b);    
    cout << "Sum = " << endl;
}
void get_data() 
{
    cout << "Enter any two numbers: ";
    cin >> a >> b;
}

int sum(int a, int b)
{
    return a + b;
}                               

OOP structure of C++ program

We use the following general structure to write C++ programs using Object-Oriented Programming Paradigm.

General Structure of C++ Program (OOP)
/*documentation*/
pre processing statements 
class ClassName
{
    member variable declaration;
    .
    .
    .
    member functions(){
        function body
        .
        .
    }
};
void main ()
{
    ClassName object;
    object.member;
}                               

Let's create a C++ program using the OOP structure.

C++ Program to perform addition of two numbers using OOP structure
#include < iostream.h > 

class Addition{
    int a, b;
    public:
        get_data(){            
            cout << "Enter any two numbers: ";
            cin >> a >> b;
        }
        int sum(){
            get_data();
            return a + b;
        }
};

void main(){
    Addition obj;
    cout << "Sum = " << sum() << endl;
}                               
  • The POP structure program follows the top-bottom flow of execution.
  • The OOP structure program follows the bottom-top flow of execution.