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 “II”.
The general syntax is: II Single Text line
For Example: II Add Two Numbers (Heading of the program)
C= a + b; II Store the value of a + b in C.(Comment to explain given statement)
(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 */
For Example: /* Write a C++ program to find the sum and average of five numbers. */
We’ll be covering the following topics in this tutorial:
Preprocessor Directives
These are compiler preprocessor statements. These are also optional statements, but becomes compulsory if your compiler has INCLUDE and LIB subdirectories in the specified drive TCPLUS or having any name.
Pre-compiler statements are divided into two parts.
(i) Link Section: In the Link Section, we can link the compiler function like cout<<, cin>>, sqrt ( ), fmod ( ), sleep ( ), clrscr ( ), exitO, strcatO etc. with the INCLUDE subdirectory having header files like iostream.h, conio.h, math.h, dos.h, string.h, ctype.h, process.h etc. It becomes very useful during the compilation and the linkage phase.
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 variable name value
For example:
#define PI 3.142
#define A 100
#define NAME “Dinesh”
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. These variables are called global variables. In some programs we use function sub-programs. So we declare some variables in the main program and function sub program having common name. This creates the duplicity or redundancy in defining the variables. So we can take the common variable declaration above the main program, which is also called global variable declaration. The global variables are automatically initialized with zero, hence there is no chance of garbage value. The global variable declaration can be done by the statement given below.
The general syntax is:
data type vl,v2,v3………. vn;
Here data types are int, float, char, double etc. vl,v2,v3, …. vn is the list of global variables(v). For example: Following are the some valid global declaration statement as:
int a, b, c;
float x,y,z;
char ch, name[10], address[20];
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>
{
privat:
data members;
member functions O;
public:
data members;
member functions O;
protected:
data members;
member functions O;
};
mainO
{
<name of class> obj1;
obj1.member function O;
obj1.member functionO;
getchO;
}
(i) Class name or name of class
It is mainly the name given to a particular class. It serves as a name specifier for the class, using which we can create objects. The class is specified by keyword “class”.
For example: class Circle
Here Circle is the class name.
(ii) Data Members
These are the data-type properties that describe the characteristics of a class. We can declare any number of data members of any type in a class. We can say that the variables in C and data members in C++.
For example: float area;
(iii) Member Functions
These are the various operations that can be performed to data members of that class. We can declare any number of member functions of any type in a class. Member functions are access using object and dot operator.
For example: void read(); void display();
How member function access: cl.read();
Here c1 is object of class circle and operator dot (.) is used to access member functions.
(iv) Access Specifiers
Access Specifiers are used to identify access rights for the data members and member functions of the class. Depending upon the access level of a class member, access to it is allowed or denied.
There are three main types of access Specifiers in C++ programming language:
1. Private: A private member within a class denotes that only members of the same class have accessibility. The private member is not accessible from outside the class.
2. Public: Public members are accessible from outside the class.
3. Protected: A protected access specifier is a stage between private and public access. If member functions defined in a class are protected, they cannot be accessed from outside the class but can be accessed from the derived class (inheritance).
main()
main function is where a program starts execution. Main () program is the C++ program’s main structure in which we process some statements. The main function usually organizes at a high level the functionality of the rest of the program.
The general syntax is: main ( )
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. A class declaration only uses to builds the structure of an object. The data members and member functions are combined in the class. The declaration of objects is same as declaration of variables of basic data types. Defining objects of class data type is known as class instantiation (instances of class).When we create objects during that moment,· memory is allocated to them.
For example: c1 is the object of the class Circle. We can create any number of objects of a given class.
(ii) Accessing member functions (using dot operator)
The member function define in the class are access by using dot (.) operator. Suppose we have two member functions void read () and void display (). We have to access these member function in the main program.
The general syntax is:
Obj1. member function();
For example: c1.read();
Here c1 is object and read() is the member function of class Circle ..
Ending of The Main Program: Right Brace}
(The right curly brace “}” is used to end the main program).
Program 2: Write a C++ program to find the area of circle using basic structure of C++ program with c1asses.
1. Documentation Section: Use single line comment
II Program to find the area of circle using basic structure of C++ program
2. Preprocessor Directives: Link and Definition Section
#include <iostream.h> II Link Section
#include <conio.h> II Link Section
#define PI 3.142 II Definition Section
3. Global Declaration Section
float r;
4. Class declaration or definition
class Circle // Class definition (Circle class is created)
{
private:
float area; // Data member
public:
void readO // Member function
{
couk<“Enter the radius of the circle:”;
cin>>r;
}
void displayO
{
.area = PI * r*r;
Cout<< “Area of circle is ” <<area;
}
};
5. Main program starts
main ( )
6. Beginning of the main program: Left brace
{
Circle c1;
c1.readO;
c1.displayO;
getch( );
7. Ending of the main program: Right brace
}