Structure of C++ program
It is common to organize a program into 3 files. Class declarations are placed in one header file and member function definitions move to another file.This approach allows the programmer to separate the abstract interface specification (class definition) and implementation details (member function definition). Finally, the main program that uses the class is put into a third file that “includes” the two previous files and any other required files.A typical program contains 4 sections, some of them(sections) are optional.
1) Include Files:
- Similar to C, C++ programs are also dependent on certain header files.
- Each header file has a ‘. h’ extension.
- For example, #include<iostream.h>
2) Class Declaration or Definition:
- A Class contains variables declaration, functions declaration/definition.
- The class declaration must be enclosed with curly braces ({ }) and terminated by a semicolon(;).
Syntax:
class <class name>
{
Variables declaration; // data members or member variables
Function declaration/definition; // called as member functions
}; // End of class
3) Class function definitions:
- A class function definition can be done outside or inside the class declaration.
- If the function is small then define it inside the class.
- When the function is large, define it outside the class. In this case, the prototype or declaration of a function should be reported within the class.
4) main( ) function:
All the C and C++ programs start execution from main( ) function.
