Structure of a C++ program

Estudies4you
Structure of C++ Program, Datatypes, Declarations of Variables, Expressions

Q15. Illustrate the structure of a C++ program.
Answer :

Structure of C++ Program
The structure of a C++ program is as shown below,

Section 1
Header Files Declaration
Section 2
Class Declaration or Definition
Section 3
Class Function Definition
Section 4
Main Function
                              Table: Structure of C++ Program
Section-1 :
                This section includes the following,
(i) All the header files that are required for the program.
(ii) Prototype declaration associated with various library functions.
(iii) User-defined header files.
(iv) Preprocessor directives that are necessary for the program.
Section-2
                This section includes the class declaration or definition along with its function definitions.
Section-3
                This section includes member functions definition of the class.
Section-4
                This section includes the following,
(i) main( ) function that initiates the execution of program.This function is automatically called by the operating system.
(ii) It allows to create the objects for the class.
(iii) It is responsible for calling every individual function directly/indirectly.

Example
C++ program for finding the Area of a circle.
Header Files
#include <iostream.h>
#include <conio.h>
#define PI 3.142

Class Declarations
float r;
class Circle
{
private:
float area;
public:

Declaration of Member Function // Class Function Definition
void read()
{
cout<<“Enter the radius of the circle:”;
cin>>r;
}
void display( )
{
area = PI * r*r;
cout<< “Area of circle is” <<area;
}
};

Main Function
int main()
{
clrscr();
Circle cl;
cl.read();
cl .display();
getch();
return 0;
}

OUTPUT:


To Top