×

Structure of C++ Program

Many people believe that C++, an object-oriented programming (OOP) language, is the finest language for developing demanding applications. A superset of the C language is C++. Java, a closely comparable programming language, is based on C++ and is suited for the distribution of program objects across a network like the Internet. In this tutorial, we will explore the basic structure of a C++ Program.

Following are the structural parts of C++ program:

1. Documentation Part
2. Link Part
3. Definition Part
4. Global Declaration Part
5. Function Definition Part
6. Main Function Part

Let's briefly discuss the topics mentioned above to better comprehend them.

1. Documentation Part:

  1. This part is the first and serves as a description of the program's logic before it is coded.
  2. Additionally, it can be used to write for a program's purpose.
  3. The compiler does not compile anything that is written in the documentation section; it is basically a comment.
  4. Because the application can run without them, the documentation section is optional. The following is a example of the same:
//A C++ Program to Find the max number between the three digits

Or, we can also write it like below:

/* A C++ Program to Find the max number between the three digits
Here we will use if-else statement */

2. Linking Part:

There are two primary components in the linking portion:

Header File:

  • Typically, a programme consists of multiple programming components including built-in functions, classes, keywords, constants, operators, etc. which are already predefined in the standard C++ library.
  • A suitable header must be provided in the application in order to utilise such pre-defined elements.
  • A program's standard headers are provided using the preprocessor instruction #include. The iostream header is utilised in Figure. The contents of the stream are added to the programme when the compiler processes the instruction #include<iostream>. Due to the fact that standard streams specified in< iostream> are the only ones that include input, output, and error capabilities, the programmer is now able to use these facilities. These common streams read and display data in a continuous stream while processing it as a stream of characters. Here is a list of the common streams that <iostream> defines.
#include <iostream>

Namespace:

  • A namespace enables the association of different entities, such as classes, objects, functions, and different C++ tokens, under a single word.
  • Any user has the ability to establish unique namespaces of their own and use them in other program.
  • Namespace std provides declarations for the statements cout, cin, endl, etc. in the code examples below.
using namespace std;

3. Definition Part:

  • A few constants are declared and given values using it.
  • Anyone can use primitive data types to define their own datatype in this section.
  • A compiler directive found in #define instructs the compiler to substitute "Factorial\n" for the message whenever it is found.
  • typedef int K instructs the compiler that wherever it encounters K, it should be replaced by an int and that because K has been declared as a datatype, it cannot be used as an identifier.

4. Global Declaration Part:

  • Declaring these variables and class definitions at this place makes them global for use throughout the program.
  • The scope of the variable declared in this part will last until the end of the program.
  • The user-defined functions can also access these variables.

5. Function Declaration Part:

  • All of the functions that our main functions require are present in it.
  • The user-defined functions are often found in this section.
  • This section of the program may be written after the main function, but in order to do so, we must first write the function prototype for the function in this section for which we will write code after the main function.

C++ Code:

//implementation of the function to find max


void max(int a, int b, int c)
{
	
	//utilizing if-else condition
	
	if (a > b)
{
		if (a > c)
{
			cout << a <<" is the maximum number between the three digits";
		}
		else
{
			cout << c <<" is the maximum number between the three digits";
		}
	}
	else
{
		if (b > c)
{
			cout << b <<" is the maximum number between the three digits";
		}
		else
{
			cout << c <<" is the maximum number between the three digits";
		}
	}
}

6. Main Function Part:

  • The main function instructs the compiler on where to begin the program's execution. The main function is where the programme is first put into action.
  • The main function contains a complete list of the statements that must be executed.
  • The curly braces {} that enclose the main function's body contain all the instructions that the compiler is supposed to carry out.
  • Control leaves the main function and the programme terminates after all of the main function's instructions have been carried out.

This program will demonstrate this:

C++ Code:

//Documentation Part
/* A C++ Program to Find the max number between the three digits.
Here we will use if-else statement */


//Linking Part
#include <iostream>
using namespace std;


//Definition Part
#define integer int


//Global Declaration Part
integer a = 0; 


//Function Part
void max(integer a, integer b, integer c)


{
//utilizing if-else condition
	if (a > b)
{
		if (a > c)
{
			cout << a <<" is the maximum number between the three digits";
		}
		else
{
			cout << c <<" is the maximum number between the three digits";
		}
	}
	else
{
		if (b > c)
{
			cout << b <<" is the maximum number between the three digits";
		}
		else
{
			cout << c <<" is the maximum number between the three digits";
		}
	}
}


//Main Function Part
int main()
{
	integer b = 0, c = 0;
	cout <<"Enter three values to compare : ";
	cin >> a >> b >> c;
	max (a, b, c);
	return 0;
}

If the user puts 65, 78, 69 as inputs,

Output:

Enter three values to compare : 65
78
69
78 is the maximum number between the three digits

Related Topics

Virtual Function Vs Pure Virtual Function

Virtual activity is a member function defined in the foundation phase that can be redefined by acquired classes. Let's have a look at an example: #include <iostream>   #include <bits/stdc++.h> #include <stdlib> using namespace std;   class Base   {    ...

5 minutes read.

Stack in C++

Stack: The stack is a very popular data structure. It is the form of data structure that follows a particular order called FIFO(First-In-First-Out). In simple words, a stack is an Abstract...

4 minutes read.

C++ int into String

Data type conversion is a standard editing process. You may need to convert variable from one type of data to another in a variety of situations. There are two ways...

5 minutes read.

Program to convert infix to postfix expression in C++

Parentheses are frequently employed in mathematical formulas to make their interpretation easier to understand. However, with computers, parenthesis in an expression might lengthen the time it takes to find a...

7 minutes read.

Advanced C++ with Boost Library

The goal of the Boost Libraries is to be widely applicable and used in a variety of applications. For instance, they can in handy when working with huge numbers whose...

4 minutes read.

C++ Program to find the element that occurs once

Write a program to find the element in the array that occurs once. Given that all the numbers in the array are present two times and the array is sorted,...

7 minutes read.

Check for Balanced Brackets in an Expression (well-formedness) using Stack

Write a program that check the correctness of the pairs and ordering of the characters “{“, “}”, “(“, “)”, “[“, “]” in the expression string exp. Example: Checking for balanced parenthesis is one of...

2 minutes read.

Sizeof() Operators in C++

sizeof() Operators in C++ The sizeof() operator in C++ defines the size of variables, constants, or data types. It is a unique operator that manipulates other operators and returns the size...

4 minutes read.

While Loop Examples in C++

While Loop A while loop repeats all code in its body, also known as a while statement, as long as a specific condition is satisfied. The loop ends if or when...

4 minutes read.

Handling multiple clients on the Server with multithreading using Socket Programming in C or C++

To understand this guide completely, the reader is assumed to be familiar with the foundations of server and client models and socket programming. If one wants to create any scalable...

7 minutes read.

Constructor Overloading

The program contains more than one constructor in a class with the same name, and different types of arguments are called constructor overloading. Calling of constructor depends on the number and types...

2 minutes read.

Print Table Using For-loop in C++

Multiplication Table In mathematics, a table is created by multiplying a certain number by all of the counting numbers, i.e., 1, 2, 3, 4, 5, 6, and so on. It is...

3 minutes read.

Hybrid Inheritance

C++ Hybrid Inheritance When more than one type of inheritance is combined in single inheritance is called as hybrid inheritance. C++ Hybrid Inheritance Example #include<iostream>   using namespace std;   class Student{       protected:           int rollno;       public:           void getRoll(int a){               rollno=a;           }           void putRoll(void){               cout <<"Roll No: "<< rollno<<endl;           }   };   class Test : public Student{       protected:           float subject1, subject2;       public:           void getMarks(float x, float y){               subject1=x;               subject2=y;           }           void putMarks(void){               cout<< "Marks gain: "<<endl <<"Subject1 =  "<<subject1<<endl<<"Subject2 = "<<subject2 <<endl;           }   };   class Sport{       protected:           float score;       public:           void getScore(float s){               score=s;           }           void putScore(void){               cout<<"Sports score: "<<score<<endl;           }   };   class Result : public Test, public Sport{       float total;       public:           void display(void);   };   void Result:: display(void){       total=subject1+subject2+score;       putRoll();       putMarks();       putScore();       cout<<"Total Score: "<<total<<endl;   }   int main(){       Result stu;       stu.getRoll(10);       stu.getMarks(40,50);       stu.getScore(60);       stu.display();       return 0;   } Output: Roll No: 10 Marks gain: Subject1 = 40 Subject2 = 50 Sports score: 60 Total...

1 minute read.

Exception Handling in C++ vs Java

Nowadays, exception handling is a feature found in virtually all object-oriented languages. We can also find this type of feature in Java and C++. The try-catch and block is required...

3 minutes read.

Web Development in C++

Before learning above C++ web development, we need to learn about CGI What is CGI? CGI stands for common gateway interface. CGI is a standard that tells us how the exchange of...

4 minutes read.

Features of OOPS in C++

What is OOPs? The main reason programmers prefer C ++ language over C is because of the support of object-oriented programing in C++. As the name suggests, object-oriented programming or OOPs...

3 minutes read.

New Operator in C++

Dynamic memory allocation in C++ means manually allocating the memory by the developer duing run-time. The dynamic memory is allocated in the heap section of the RAM, whereas the static...

3 minutes read.

C++ Fibonacci Series

What is a Fibonacci series? A Fibonacci series or sequence is a very popular programming paradigm. The next element occurring in the N terms series is determined by the sum of...

2 minutes read.

Maps in C++

Maps: Maps in C++ are the containers associated with key and mapped values. By keys and mapped values, we mean that the maps are used to store elements formed by the...

4 minutes read.

How to concatenate two strings in C++

In the C++ programming language, the concatenation of two or even more strings is covered in this section. The term "string concatenation" refers to a collection of characters that join two...

4 minutes read.