C++ Program Structure


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


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




To learn more you can just click the below topics:

C++

INTRODUCTION OF C++ || Definition of C++
Brief history of C++ || history of C++
Features of C++ || why we use C++ || concept of C++
Concept of OOP || What is OOP || Object oriented programming language
Difference Between OOP And POP || Different Between C and C++
Characteristics of C++
Interesting fact about C++ || Top 10 interesting fact about C++
Writing first program in C++ || how to write hello world in C++
Basic Data Type And Variable In C++
Identifier in C++
Keywords in C++
Token in C++
Comment in C++
Constant in C++
Modifier in C++
Taking User Input in C++ | User input in C++
Input Output Operator In C++
C++ Operators | Operator in programming language
How to Add two number in C++
Polymorphism in C++
Compile Time Polymorphism in C++
Function overloading in C++
Operator Overloading in C++
What are Control Structures in C++ || Understanding Control Structures in C++ | How to use if, else, switch
What are Functions and Recursion in C++ | How to Defining and Calling Functions

Class in C++
Object in C++

Data Science

The History and Evolution of Data Science
Scope of Data Science
Why learn Data Science? | Why Data Science?
Impact of Data Science
The Importance of Data in Science | Introduction to Data Science
What is Data Analysis | Data Analyst for Beginners

Algorithm

Why algorithm | The Importance of Algorithms in Modern Technology

Tech to know

Which is better | BSc in Computer Science or BTech?

Post a Comment

Ask any query by comments

Previous Post Next Post