C++ PROGRAM STRUCTURE
C++ PROGRAM STRUCTURE:-
A C++ program is organized in a particular and unique way. The
following components make up a program in C++:
Header File Declaration Section
|
Global declaration Section
|
Class Declaration
And
Method Definition Section
|
Main Function
|
Ø Header File Declaration Section:-
Example of header file in a simple hello world program:-
#include <iostream.h>
#include <conio.h>
void main()
{
cout<<”Hello world”;
getch();
}
|
Output:-
Hello world
|
In this program we use a two header file:-
<iostream.h>
<conio.h>
The header file <iostream.h> is input
output stream, it is use to control the input output operation in C++ language.
Another header file <conio.h> is console
input output, it is used in an old DOS compilers to create text user
interfaces.
Ø Global declaration Section:-
Global declaration is also known as a global variable in a C++. It is a variable that declare outside of any function, and they can be accessed on any function in the program.
The simple definition of global declaration is:-
The variable which are declared outside of the function and accessed from all function including main function are known as GLOBAL VARIABLE AND DECLARATION
Example of global variable given blow:-
#include<iostream.h>
#include<conio.h>
Int
a=10; // this is global
variable and declaration
Void
main() //main function()
{
Int
b,c;
B=20;
C=b+c;
cout<<”The
value of C is :”<<C;
getch();
}
|
Output:-
The
value of C is 3
|
Ø Class Declaration And Method Definition Section:-
Example of class declaration and method definition section:-
class
ClassName
{
Access
specifier: // can
be private, public or protected
Data
member; //
Variable to be used
Member
function() {} // Method to access data members
); // Class name ends with semicolon
|
ex:
|
Class is a keyword and ClassName is a name of the class
Ø Main Function:-
A function is a group of statements that together perform a task. Every C++ program has at least one function, which is main(), and all the most trivial programs can define additional functions. The main function can in-turn call other function. When main calls a function
Two type of main function in a c++ are given blow:-
1. Null type (void)Int: it is return type of a main function, at that time main() return integer type value.
Simple example of main():-
#include<iostream.h>
#include<conio.h>
Void main()
{
Cout<<”Hello
world”;
getch();
}
|
Output:-
Hello world
|