C++ Program Structure
Following is the structure of C++ Program:
#include <iostream> using namespace std; int main ( ) { program statement -1; program statement -2; . . . program statement n; return 0; }
Description of the C++ program structure is as follows:
- C++ program begins with the inclusion of the header files, header files contain instructions that are necessary to execute programs. Header file “iostream” conducts input output in the form of streams. When a program takes input from the user then the input stream works when the program provides output to the user then the output stream works.
- using namespace std; directs C++ compiler to use std namespace, namespace std defines the cout, cin functionalities.
- main ( ) is the in-built function from where the program execution begins. To execute a program C++ compiler looks for main ( ) in the program.
- Program statement -1, program statement -2….program statement – n are programming instructions developed to get desired output.
- return 0 is the last statement of the C++ program and that returns value 0 to the calling process.
C++ Program Structure with Classes
A C++ Program Structure with Classes is as follows:
- Header file declaration section
- Global declaration section
- Declaration of classes
- main ( ) section
- Creating instances of class (object declaration)
- Using class function
- Programming instructions
Header file section: this is also called link section. This section links C++ program to the file required to execute it successfully. C++ compilers supports different types of header files such as: iostream.h, conio.h, math.h, dos.h, string.h, ctype.h, process.h etc., Syntax to include header file is:
# include <header file> or # include “header file”
Global declaration section: this is the section after the header file declaration section and before main ( ) program. Variables declared in this section are accessible through-out the program and in all defined functions. Global variables default value is 0. The way of declaring global variable is like this:
#include <iostream.h> using namespace std; int a = 10; int main ( ) { program instruction - 1; program instruction - 2; . . . Program instruction - n; return 0; }
In the above code a is a global variable.
Declaration of Class: A C++ class consists of data members and member functions. Data members are variables or constants having specific data types to be used in the program. Member function contains programming instructions that operate on data members to achieve desired objectives. Class in C++ is a type. Class specifies behaviour of objects such as their creation, manipulation and destruction. Class must be designed in such a way that it should represent a single concept. Syntax of class declaration is as follows:
class alpha …………………... //name of the class { private: int a,b; ……………….. //data members void add ( );...................//data functions public: int x, y;……………….. //data members void sub ( );...................//data functions protected: int m, n;……………….. //data members void mul ( );...................//data functions } ; void main ( ) { alpha obj;...................//Object declaration of Class obj.add( );...................//member function call using object obj.sub( ); obj.mul( ); }
Classes occupy space in memory that space needs to be accessed. In high level language memory space is accessed using an identifier. An identifier in C++ can be alphanumeric. There are specific rules using which C++ identifier can be declared. In the above code alpha is the name of the class.
Number of data members and member functions declared within the class depends on the objective to be achieved through C++ program. Data members and member functions can be public, private or protected. Public, private and protected are access specifiers. Access specifiers specify the access rights.
Data members and member functions declared as public are accessible from all the functions of the class.
Data member and member functions declared as private can be accessed by the member functions within the class.
Data members and member functions declared as protected can not be accessed outside the class. Protected data member and member functions of the class can be accessed using friend class and drive class.
main ( ) program execution begins from main ( ). Main contains program logic. Main ( ) function begins with left { and ends with right braces }. In main ( ) variables and constants are declared. Class objects are also instantiated. When class objects are instantiated memory is allocated to them. Amount of memory space they consume depends on the memory consumed by data members and member function of the class. Class objects are used to access the member function and data members of the class. In the above code obj is an object of class alpha and obj.add() is the way to access class member functions.
Relations of C++ Program Structure
Basic structure of C++ program is derived from the C program structure and C program structure is taken from BCPL. Comments ( // ) used in C and C + + are taken from BCPL.
Class concept of C++ is taken from Simula67. The concept of derived class and virtual function is also taken from Simula67.
Concept of operator overloading is taken from Algol68. In C++ declaration statements can be placed where and when it is used, this style of programming is also taken from Algol68.
The Template declaration of C++ is inspired from Ada. In addition to this exception handling technique is taken from Ada, Clu and ML.
Concepts such as multiple inheritance, pure virtual functions, and namespaces are developed based on experience.
In C++ program structure supports hierarchical ordering. Hierarchical ordering is possible with the help of derived classes. In C++, programs can be structured as a tree or directed acyclic graph.
A program is a representation of a group of ideas as a solution to a specific problem. Structure of the program should correspond to those ideas.
A generalized guideline to create structure of a C++ program is as follows:
- A separate idea should represent a class.
- An entity should represent an object.
- Two classes having the same interface are represented as abstract classes.
- If two classes have common features then that common feature should be represented as base class.
- If a class has to be used many times then it should be declared as a template.
- Logically related classes, templates, etc are placed in common namespace.
A class that does not represent a mathematical entity or a programming structure that is not used by all the functions of C++ program then in that case following guidelines should be followed to generate C++ programing structure
- Avoid using global data.
- Avoid using global functions.
- Avoid using data members.
- Avoid friend function.
- Avoid inline functions.
- Use Virtual functions.
Leave a Reply