PACKAGES IN C++

So far, we know that the packages concept can be implemented in JAVA, but we can also implement packages concept in C++.

Basically package means some things packed as single one. Here, in JAVA, Package means package of classes. All classes of particular type are packed under one package. so we can use those classes, when ever we want ,by importing them. Similarly, we can include some methods and data items defined in other packages in the current program.

How packages work:

PACKAGE


// SOME DATA ITEMS/MEMBERS AND FUNCTIONS CAN BE WRITTEN HERE.

/*THESE DATA ITEMS/MEMBERS  CAN BE INCLUDED IN OTHER PROGRAMS,SO THAT THEY CAN BE USED FURTHER*/

PROGRAM IMPORTING PACKAGE

 

/*THE PROGRAM WRITTEN IN THIS BLOCK SHOULD IMPORT THE PACKAGE CREATED INORDER TO ACCESS THEM (EITHER DATA ITEMS/FUNCTIONS).

THIS BLOCK CAN CONTAIN THE STATEMENTS IMPORTING PACKAGES AS WELL AS SOME OTHER LINES OF CODE  PERFORMING SOME OTHER TASK*/

 

Package is nothing but a program, which collection of data members/items and functions.
The items /functions defined in package can be used by importing them in the user program. All those written in the package should be public.

Till now discussed how packages work. Now, let us see the syntax to include the user defined Package.

Unlike the header files like <stdio.h>, <conio.h>and <iostream.h>, the packages do not have “.h” extension. The Packages are not header files. So we cannot save the package created with “.h” extension. So we in turn save the package with either “.C “or “.CPP” extension.

Suppose if we write as follows:

1. #include<stdio.h>

2. #include<iostream.h>

The above line will include that particular header file to the current program.
Under this package, simple instruction to print ie., printf() method is defined under <stdio.h> and cout ,which is used to print in C++, is defined under <iostream.h>

(Input Output Stream Header file).

In the similar way, we can write a simple statement stating to include the file with either “.C” or “.CPP” extension.

Let us consider the following example.

If a package named “add” is created ie., nothing but  an ordinary  program performing a small addition operation on the given inputs and if that package “add” is to be imported in the current program, then we can include that particular package by writing the small statement :

#include <package name.C> 

Or

#include <package name.CPP>   

As CPP is an extension to C language,”.C” extension is also allowable.

Here, in the example considered, if we want to include the add.cpp into the current program, it should be written as:

//Let this program be named “packagedemo.cpp”
#include<add.cpp>
#include<iostream.h>
void main()
{
int d;

d= addition (10, 20); //add is a method defined in the package “add.cpp”  
cout<<”the result after adding 2 no’s is :”<< d;
 }

In the above program,”add.cpp” package is imported and the method addition is used to add the given two inputs .the method returns an integer type value.

The Package “add.cpp” will be like this:

#include<iostream.h>

void main()
{      
Public: 
int c;

    int addition (int a, int b)
{

c = a+b;
return c;
}
}

      
This is small program considered for the illustration of  creating a package of our own in C++.

The package must be created before using that in the other programs. so the “add.cpp” must be created before using the method “addition()” in any other program. here it  should be noted that “add.cpp” must be written before the addition() function is used in
“Packagedemo.cpp”

The result of the  program(packagedemo.cpp) is:

the result after adding 2 no’s is :30

NOTE: As we pass the parameters directly in the function call itself, no need to give inputs specially.

ADVANTAGES:

The main advantage of Packages is:

It reduces the space complexity ie., less space will be used.

As we are using the method created already in one program, the space required to create other programs will be undoubtedly reduced.

Will be very simple for a programmer as it doesn’t need to write the same content of logic again and again.
.
Though this looks a bit different, adaptation of this concept will be so beneficial and useful.

DISADVANTAGES:

 If a package (program) is created, and if that is used in another program .it works fine until the package remains saved. If the program, that we are using as package is no longer useful for us, even then we can’t delete that package (program) as some other programs depend on it.
So, we can say that this concept will be advantageous only when there are more number of programs depends on a particular package.

In the above case ie., one package included in one program will be little disadvantageous as the time complexity matters here. In this case, space complexity is not considered because the space required will be same either if we write logic in one package or if we write the logic in same block of code. In this case, time complexity comes into focus as the control should be transferred from one memory location to another memory location as many times the methods /items of package are called in the main program.

In the diagram shown below., it is clear that many programs are using the same package.

so as the same code is used in various programs ,the space required will be decreased when compared to the case in which the block of logic code written separately for different programs.

Most of us write the following statement to include to a particular header file to program.

#include<stdio.h>

This statement will include the <stdio.h> (Standard Input Output Header file) .all the programmers usually write in this format. But there is another format to include the header files to a program. Another format is:

#include”stdio.h”
Though both the above do perform the same task ie., including header files to a program, there is a little bit difference between the two. The difference is in the searching method, the way the compiler will search to include those particular header files to the program.

If we write the first one ie., # include<stdio.h>., it will search in the local memory space ie., with in the file/folder. But if we write #include”stdio.h” , this will search in the global ie., it will also search beyond the file/folder.

 Hence, it will be a good practice to write the include statement using “ “rather than <>.  especially while including the user created packages. So it will be always a good practice to write as follows:

# include”add.cpp”

While compiling the program containing this statement, after the compiler has encountered this statement, firstly it will search for the particular header file/program Written in the inverted commas and then compiler will include that particular one to the program.

As shown in the figure, packages and  actual programs will be stored in separate memory locations. So when ever it is encountered a statement stating to include a particular package, the control must jump to that location and return to actual program.
 
This type of practice is permissible if there are many programs using a single package so that space complexity decreases, but if there is single program, time complexity increases.

Further, let us consider another example to illustrate this concept of package .

Usually while doing projects in C++, we import too many header files and programs we write. So in order decrease that repeatedly writing all the list of include section ie., list of header files we can write the list alone as one program and save that program with some name with extension “.CPP” and include that program in every newly writing program so that it will reduce the burden to programmer to write the list every time.

#include”iostream.h”
#include”conio.h”
#include”dos.h”
#include”math.h”
#include”time.h”
#include”string.h”

Save this list as one program with some name “all.cpp”. so while writing another new program, we can include the package/program  all.cpp instead writing all those every time.

NOTE:
writing in the above fashion will be beneficial iff it uses most of the header files included in the list. If not, then space complexity will be increased unnecessarily.








}