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++:

C++ PROGRAM STRUCTURE



{tocify} $title={Table of Contents}
Header File Declaration Section
Global declaration Section
Class Declaration
And
Method Definition Section

Main Function

Ø Header File Declaration Section:-


Header file is a file in a C++, it declare in the top of C++ program because it contain definitions of Function and Variables, which is imported or used into any C++program by using the pre-processor #include statement.
.h is an extension of header file which contain C++ function declaration and macro definition, it use with name of the header file for example:-
<iostream.h>
     Example of header file is:-
1.    Iostream.h
2.    Conio.h
3.    Stdio.h
4.    Math.h
5.    Dos.h
And etc. C++ have many type of header file that used in the program.
Syntax of header file:-
#include<iostream.h>

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


In this program “a” is global variable that declare in the bottom of header file.


Ø Class Declaration And Method Definition Section:-

                                                                                                                                          The building block of C++ that leads to Object Oriented programming is a Class. It is user define data type, which holds its own data member and member function, which can be accessed and used by creating an instance of that class. Class is a wrapping of the data member and function member in a single program. The data member include the variable that we can provide the security by using access spesifier. The function member used to access the data member to be used and most important is in a C++ every class must end with the semicolon ‘ ; ‘ .

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:
#include<iostream.h>
#include<conio.h>
class add
{
public:
int a;
int b;
int c;

public:
void addition(void);
};
void  add::addition(void)
{
cout<<endl<<"plese enter the value of a and b:";
cin>>a>>b;
cout<<endl<<a;
cout<<endl<<b;
c=a+b;
cout<<endl<<"the sum of those numbers:"<<c;
}
void main()
{
clrscr();
add A;
A.addition();
getch();
}

Class is a keyword and ClassName is a name of the class
Access specifier used to give the security to the program
Data member, variable to be used
Data member, it is used a method to access the data member to used


Ø 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)
2.    Return type (int)
Void: is a keyword in c++ language, void means nothing, whenever we use void as a function return type then that function nothing return. here main() function no return value.
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


Data science & data analyst

C++

Algorithms

Technology


Post a Comment

Ask any query by comments

Previous Post Next Post