General structure of c++ program with classes

General Structure of C++ Program with Classes:
A C++ program can be developed from a basic structure. The general structure of C++ program with classes is as shown below (which is also called overview of a C++ program):
1. Documentation Section
2. Preprocessor Directives or Compiler Directives Section
(i) Link Section
(ii) Definition Section
3. Global Declaration Section
4. Class declaration or definition
5. Main C++ program function called main ( )
6. Beginning of the program: Left brace {
(i) Object declaration part;
(ii) Accessing member functions (using dot operator);
7. End of the main program: Right brace }

Documentation Section

In Documentation section we give the Heading and Comments. Comment statement is the non-executable statement. Comment can be given in two ways:
(i) Single Line Comment: Comment can be given in single line by using "//".
The general syntax is: // Single Text line
 (ii) Multiple Line Comment: Comment can be given in multiple lines starting by using "/*" and end with "*/".
The general syntax is: /* Text Line 1
Text Line 2
Text Line 3 */

2.Preprocessor Directives
These are compiler preprocessor statements. These are also optional statements.
Pre-compiler statements are divided into two parts.
(i) Link Section: To link external files
The general syntax is: #include <header file> .or #include "header file"
For example:
#include <iostream.h>
#include <conio.h>
#include "dos.h"
ii) Definition Section: The second section is the Definition section by using which we can define a variable with its value. For this purpose #define statement is used.
The general syntax is: #define  variablename  value
For example:
#define  PI  3.142
#define  A  100
#define  NAME  "Dinesh"
3. Global Declaration Section
In this section, we declare some variables before starting of the main program or outside the main program.
These variables are globally declared and used by the main function or sub function.
The general syntax is:
data type  vl,v2,v3………. vn;
4. Class Declaration or Definition
A class is an organization of data and functions which operate on them.
 Data types are called data members and the functions are called member functions.
The combination of data members and member functions constitute a data object or simply an object.

The general syntax or general form of the class declaration is as follows:

class <name of class>
{
private:
data members;
member functions();
public:
data members;
member functions();
protected:
data members;
member functions ();
};
main()
{
<name of class> obj1;
obj1.member function();
obj1.member function();
getch();
}

5.main()
main function is where a program starts execution.
The general syntax is: main ( )
6.Beginning of the Main Program: Left Brace {

(The beginning of the main program can be done by using left curly brace "{").

(i)Object declaration part
We can declare objects of class inside the main program or outside the main program.
 (ii) Accessing member functions (using dot operator)
The member function define in the class are access by using dot (.) operator.
The general syntax is:
Obj1. member function();
For example: c1.read();
       
7.Ending of The Main Program: Right Brace }
(The right curly brace "}" is used to end the main program).

Comments

Popular Posts