Friday, 29 November 2013

Constructors in C++

:

A constructor is a special method that is created when the object is created or defined. This particular method holds the same name as that of the object and it initializes the instance of the object whenever that object is created. The constructor also usually holds the initializations of the different declared member variables of its object. Unlike some of the other methods, the constructor does not return a value, not even void.



When you create an object, if you do not declare a constructor, the compiler would create one for your program; this is useful because it lets all other objects and functions of the program know that this object exists. This compiler created constructor is called the default constructor. If you want to declare your own constructor, simply add a method with the same name as the object in the public section of the object. When you declare an instance of an object, whether you use that object or not, a constructor for the object is created and signals itself. The main use of constructors is to initialize objects. The function of initialization is automatically carried out by the use of a special member function called a constructor. 

General Syntax of Constructor :

A constructor is a special member function that takes the same name as the class name. The syntax generally is as given below:

{ arguments};

The default constructor for a class X has the form
X::X()

In the above example, the arguments are optional. The constructor is automatically named when an object is created. A constructor is named whenever an object is defined or dynamically allocated using the "new" operator. A constructor is declared without a return value, that also excludes void.
Therefore, when implemented, do not return a value:

Constructor Example :

class rectangle {              // A simple class
   int height;
   int width;
public:
   rectangle(void);            // with a constuctor,
   ~rectangle(void);           // and a destructor
};

rectangle::rectangle(void)     // constuctor
{
   height = 6;
   width = 6;
}

The Constructor Initializer :

A constructor does not exist simply for cosmetic reasons. It can be used to initialize the member variables of an object. Therefore, a constructor provides a valuable alternative to a method initializer, the type of method we saw earlier.

To use a constructor to initialize the member variables of an object, provide as arguments the necessary variables that you intend to initialize. You do not have to initialize all member variables in the constructor, only those that need to be initialized. In fact, you should initialize only those members that you think the other objects or functions would need to provide when calling this object; this means that your object may have member variables that, either the external objects or functions do not need to modify (or access) or the member variable will be initialized later when called from the needed object or function.

There are several forms in which a constructor can take its shape namely : 

Default Constructor :

A constructor is declared without a return value, that also excludes void. Therefore, when implemented, do not return a value:

//---
#include
using namespace std;
//---

struct TBook
{
public:
    TBook();    // Constructor
};
//---
TBook::TBook()
{
    cout << "I see a book...\n";
}
//---
int main()
{
    TBook B;

    return 0;
}
//---

This would produce:

I see a book…
This book constructor is a programmer created constructor and is empty. You might find it sometimes convenient to create your own constructor because, whether you create an empty constructor or not, this does not negatively impact your program but makes it more lively and allows other parts of the program to conveniently call the object using its constructor. A constructor is easily implemented once you have created one.

Copy constructor :

This constructor takes one argument, also called one argument constructor. The main use of copy constructor is to initialize the objects while in creation, also used to copy an object. The copy constructor allows the programmer to create a new object from an existing one by initialization.

For example to invoke a copy constructor the programmer writes:

Exforsys e3(e2);

or

Exforsys e3=e2;

Both the above formats can be sued to invoke a copy constructor. 

For Example :

Sample Code-

    #include <iostream>
    using namespace std;
    class Exforsys
    {
        private:
            int a;
        public:
            Exforsys()
            { }
            Exforsys(int w)
        {
            a=w;
        }
        Exforsys(Exforsys& e)
        {
            a=e.a;
            cout << " Example of Copy Constructor";
        }
        void result()
        {
            cout<< a;
        }
    };
    void main()
    {
        Exforsys e1(50);
        Exforsys e3(e1);
        cout<< "ne3=";e3.result();
    }

In the above the copy constructor takes one argument an object of type Exforsys which is passed by reference.

Some important points about constructors :

   - A constructor takes the same name as the class name.
   - The programmer cannot declare a constructor as virtual or static, nor can the programmer declare a constructor as const, volatile, or const volatile.
   - No return type is specified for a constructor.
   - The constructor must be defined in the public. The constructor must be a public member.
   - Overloading of constructors is possible.

Constructor Overloading :

Like an ordinary method, a construction can be overloaded. This means that you can have different constructors following the rules of overloading a function. Since we saw that a constructor can be used to initialize the member variables of its object, you can use multiple constructors to apply different initializations.


Constructors in C++.

-- 
Regards,

Preeti Bagad [BE(CS)] 
SW Engineer Cum Blogger

On Line Assistence :
Y! Messenger : PreetiB.A1Soft@yahoo.com


Thursday, 28 November 2013

IO package in Java



IO package in Java :

The Java Input/Output (I/O) is a part of java . io package. The java . io  package contains a relatively large number of classes that support  input and output operations. The classes in the package are primarily abstract classes and stream-oriented that define methods and subclasses which allow bytes to be read from and written to files or other input and output sources. The InputStream and OutputStream  are central classes in the package which are used for reading from and writing to byte streams, respectively.

Input and Output - Source and Destination :

The terms "input" and "output" can sometimes be a bit confusing. The input of one part of an application is often the output of another. Is an OutputStream a stream where output is written to, or output comes out from (for you to read)? After all, an InputStream outputs its data to the reading program, doesn't it? Personally, I found this a bit confusing back in the day when I first started out learning about Java IO.

In an attempt to clear out this possible confusion, I have tried to put some different names on input and output to try to link them conceptually to where the input comes from, and where the output goes.

Java's IO package mostly concerns itself with the reading of raw data from a source and writing of raw data to a destination. The most typical sources and destinations of data are these:

   - Files
   - Pipes
   - Network Connections
   - In-memory Buffers (e.g. arrays)
   - System.in, System.out, System.error

 


InputStream :

The InputStream class is used for reading the data such as a byte and array of bytes from an input source. An input source can be a file, a string, or memory that may contain the data. It is an abstract class that defines the programming interface for all input streams that are inherited from it. An input stream is automatically opened when you create it. You cans explicitly close a stream with the close( ) method, or let it be closed implicitly when the object is found as a garbage.

The subclasses inherited from the InputStream class can be seen in a hierarchy manner shown below:

InputStream is inherited from the Object class. Each class of the InputStreams provided by the java.io package is intended for a different purpose.

OutputStream :

The OutputStream class is a sibling to InputStream that is used for writing byte and array of bytes to an output source. Similar to input sources, an output source can be anything such as a file, a string, or memory containing the data. Like an input stream, an output stream is automatically opened when you create it. You can explicitly close an output stream with the close( ) method, or let it be closed implicitly when the object is garbage collected.

The classes inherited from the OutputStream class can be seen in a hierarchy structure shown below:

How Files and Streams Work :

Java uses streams to handle I/O operations through which the data is flowed from one location to another. For example, an InputStream can flow the data from a disk file to the  internal memory and an OutputStream can flow the data from the internal memory to a disk file. The disk-file may be a text file or a binary file. When we work with a text file,  we use a character stream where one character is treated as per byte on disk. When we work with a binary file,  we use a binary stream.

The working process of the I/O streams can be shown in the given diagram.






Java IO Purposes and Features :

The Java IO classes, which mostly consists of streams and readers / writers, are addressing various purposes. That is why there are so many different classes. The purposes addressed are summarized below:

   - File Access,
   - Network Access,
   - Internal Memory Buffer Access,
   - Inter-Thread Communication (Pipes),
   - Buffering,
   - Filtering,
   - Parsing,
   - Reading and Writing Text (Readers / Writers)
   - Reading and Writing Primitive Data (long, int etc.)
   - Reading and Writing Objects



These purposes are nice to know about when reading through the Java IO classes. They make it somewhat easier to understand what the classes are targeting.


IO package in Java.

-- 
Regards,

Preeti Bagad [BE(CS)] 
SW Engineer Cum Blogger

On Line Assistence :
Y! Messenger : PreetiB.A1Soft@yahoo.com

What is a File?



Abstractly, a file is a collection of bytes stored on a secondary storage device, which is generally a disk of some kind. The collection of bytes may be interpreted, for example, as characters, words, lines, paragraphs and pages from a textual document; fields and records belonging to a database; or pixels from a graphical image. The meaning attached to a particular file is determined entirely by the data structures and operations used by a program to process the file. It is conceivable (and it sometimes happens) that a graphics file will be read and displayed by a program designed to process textual data. The result is that no meaningful output occurs (probably) and this is to be expected. A file is simply a machine decipherable storage media where programs and data are stored for machine usage.

Essentially there are two kinds of files that programmers deal with text files and binary files.

ASCII Text files :

A text file can be a stream of characters that a computer can process sequentially. It is not only processed sequentially but only in forward direction. For this reason a text file is usually opened for only one kind of operation (reading, writing, or appending) at any given time.

Similarly, since text files only process characters, they can only read or write data one character at a time. (In C Programming Language, Functions are provided that deal with lines of text, but these still essentially process data one character at a time.) A text stream in C is a special kind of file. Depending on the requirements of the operating system, newline characters may be converted to or from carriage-return/linefeed combinations depending on whether data is being written to, or read from, the file. Other character conversions may also occur to satisfy the storage requirements of the operating system. These translations occur transparently and they occur because the programmer has signalled the intention to process a text file.

Binary files :

A binary file is no different to a text file. It is a collection of bytes. In C Programming Language a byte and a character are equivalent. Hence a binary file is also referred to as a character stream, but there are two essential differences.

   1. No special processing of the data occurs and each byte of data is transferred to or from the disk unprocessed.
   2. C Programming Language places no constructs on the file, and it may be read from, or written to, in any manner chosen by the programmer.

Binary files can be either processed sequentially or, depending on the needs of the application, they can be processed using random access techniques. In C Programming Language, processing a file using random access techniques involves moving the current file position to an appropriate place in the file before reading or writing data. This indicates a second characteristic of binary files.
They a generally processed using read and write operations simultaneously.

For example, a database file will be created and processed as a binary file. A record update operation will involve locating the appropriate record, reading the record into memory, modifying it in some way, and finally writing the record back to disk at its appropriate location in the file. These kinds of operations are common to many binary files, but are rarely found in applications that process text files.

C supports a number of functions that have the ability to perform basic file operations, which include:

1. Naming a file
2. Opening a file
3. Reading from a file
4. Writing data into a file
5. Closing a file 

   Real life situations involve large volume of data and in such cases, the console oriented I/O operations pose two major problems. It becomes cumbersome and time consuming to handle large volumes of data through terminals. The entire data is lost when either the program is terminated or computer is turned off therefore it is necessary to have more flexible approach where data can be stored on the disks and read whenever necessary, without destroying the data. This method employs the concept of files to store data.

File operation functions in C:

Function Name Operation

fopen() Creates a new file for use
Opens a new existing file for use

fclose() Closes a file which has been opened for use

getc() Reads a character from a file

putc() Writes a character to a file

fprintf() Writes a set of data values to a file

fscanf() Reads a set of data values from a file

getw() Reads a integer from a file

putw() Writes an integer to the file

fseek() Sets the position to a desired point in the file

ftell() Gives the current position in the file

rewind() Sets the position to the begining of the file


1. Defining and opening a file :

If we want to store data in a file into the secondary memory, we must specify certain things about the file to the operating system. They include the fielname, data structure, purpose.

The general format of the function used for opening a file is

FILE *fp;
fp=fopen(“filename”,”mode”);

The first statement declares the variable fp as a pointer to the data type FILE. As stated earlier, File is a structure that is defined in the I/O Library. The second statement opens the file named filename and assigns an identifier to the FILE type pointer fp. This pointer, which contains all the information about the file, is subsequently used as a communication link between the system and the program.
The second statement also specifies the purpose of opening the file. The mode does this job.

R open the file for read only.
W open the file for writing only.
A open the file for appending data to it.

Consider the following statements:

FILE *p1, *p2;
p1=fopen(“data”,”r”);
p2=fopen(“results”,”w”);

In these statements the p1 and p2 are created and assigned to open the files data and results respectively the file data is opened for reading and result is opened for writing. In case the results file already exists, its contents are deleted and the files are opened as a new file. If data file does not exist error will occur. 

2. Closing a file :

The input output library supports the function to close a file; it is in the following format.

fclose(file_pointer); 

A file must be closed as soon as all operations on it have been completed. This would close the file associated with the file pointer.
Observe the following program.

….
FILE *p1 *p2;
p1=fopen (“Input”,”w”);
p2=fopen (“Output”,”r”);
….
fclose(p1);
fclose(p2)

The above program opens two files and closes them after all operations on them are completed, once a file is closed its file pointer can be reversed on other file.

The getc and putc functions are analogous to getchar and putchar functions and handle one character at a time. The putc function writes the character contained in character variable c to the file associated with the pointer fp1. ex putc(c,fp1); similarly getc function is used to read a character from a file that has been open in read mode. c=getc(fp2). 

The getw and putw functions :

These are integer-oriented functions. They are similar to get c and putc functions and are used to read and write integer values. These functions would be usefull when we deal with only integer data. The general forms of getw and putw are:

putw(integer,fp);
getw(fp);

The fprintf & fscanf functions :

The fprintf and fscanf functions are identical to printf and scanf functions except that they work on files. The first argument of theses functions is a file pointer which specifies the file to be used. The general form of fprintf is

fprintf(fp,”control string”, list);

Where fp id a file pointer associated with a file that has been opened for writing. The control string is file output specifications list may include variable, constant and string.

fprintf(f1,%s%d%f”,name,age,7.5);

Here name is an array variable of type char and age is an int variable
The general format of fscanf is

fscanf(fp,”controlstring”,list);

This statement would cause the reading of items in the control string. 

Random access to files :

Sometimes it is required to access only a particular part of the and not the complete file. This can be accomplished by using the following function:

1 > fseek

fseek function :

The general format of fseek function is a s follows:

fseek(file pointer,offset, position);

This function is used to move the file position to a desired location within the file. Fileptr
is a pointer to the file concerned. Offset is a number or variable of type long, and position in an integer number. Offset specifies the number of positions (bytes) to be moved from the location specified bt the position. The position can take the 3 values.

Value Meaning
0 Beginning of the file
1 Current position
2 End of the file. 


 C.

-- 
Regards,

Preeti Bagad [BE(CS)] 
SW Engineer Cum Blogger

On Line Assistence :
Y! Messenger : PreetiB.A1Soft@yahoo.com

Meta Tags Optimization





 There used to be a time when the contents of a pages Meta Tags was very important, it was around about the same time the Berlin Wall was still standing!! Today meta tags hold little value to Search Engine Optimization (SEO).
What Are Meta Tags? Way, way back when that wall was still upright search engine algorithms were so dumb they couldn’t work out what a page was about just from the content. So some bright spark had the ingenious idea to create a set of tags (meta tags) that inferred information about a pages content to the search engines.

Meta tags are lines of HTML code added into web pages that are utilized by search engines to bank information about your website. The standard Meta Tags are the keyword Meta Tag, the description Meta Tag, and the Title tag, which exist in approximately every web page. These Meta Tags, oftenly called as 'Tags', store meta data including keywords, keyphrases, descriptions, site author information, copyright information, site titles and other details. There are other important Meta Tags as well, which is why Meta tag optimization must be part of any web site optimization service. Meta Tags are among the several factors that the search engines look for, that any search engine optimization strategy needs to keep on top of.





Meta tags are incorporated in the 'HEAD' Tag of an HTML document. If you are using meta tags to Boost your Rankings in search engines, then you should concentrate on your description and keywords. Great idea, except there was nothing to stop a webmaster stuffing or spamming their Meta Tags with irrelevant, but very high traffic keywords and keyword phrases. Which of course they did with enthusiasm, you would find adult sites using words like Disney and Pokemon in their Keywords Meta Tag for the traffic!! Today the vast majority of meta tags are worthless and those that are still considered by search engines aren’t worth that much. For example Google confers no benefit from any meta tags, so if you expect a high Google ranking from perfectly optimised keywords in your meta tags, don’t hold your breath.



Which Meta Tags Should You Use?

For Google adding the Description Meta Tag won’t result in a boost in the Search Engine Results Pages (SERPs), but the description might be used for the description for your SERP listings in Google. So though you won’t get a ranking boost, if your write an interesting Description Meta Tag and Google uses it (not guaranteed), you might get a higher click through rate compared to a random snippet of text from your pages. All other meta tags (including the Keywords Meta Tag) are either completely ignored or won’t result in a SERPs boost.

Yahoo says they use the Keyword Meta Tag when it ranks a page. So it makes sense to add one for Yahoo and any other minor search engines that still use. Also there are directories and other websites that automatically take this information to create a listing to your site. Don’t fret over it though, add the main phrase for that page to the Keywords Meta Tag and user friendly description of the page to the Description Meta Tag and forget about it.

Use the title tag and description tag to define what your page signifies. Search engines that use it will supply the content of title tag and description tag while displaying a list of links. For example, if you do a search on Google, you will find the description listed on the search results page.

The keywords tag :

Keywords help search engines to categorize your site, and allow people to find speedily your page. However, most search engines have limits as to how many Meta Keywords are viewed. It is suggested to review your keywords and ensure that they are concise and specific. Implementing these tags aptly is crucial to generate high rankings. The most significant aspect of meta-tags is that search engines pay a lot of importance to them and hence they have come to be widely accepted in the SEO process.

Meta tag optimization must be included as only a part of the overall SEO strategy. It is critical to first pay close attention to your website architecture and content strategy. If the search engine spiders cannot find your pages and your Meta tags do not support the content on the page, each Meta tag will be meaningless. “Most SEO Experts rank the page title as the most important element to optimize in order to build authority for the primary keyword on the page.”

Example Meta Tags :

Below you will find an example set of meta tags. This is for a page you want fully indexed in all search engines.


<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”>
<HTML><HEAD>
<TITLE>SEO Tutorial – Meta Tags Optimization</TITLE>
<meta name=”description” content=”Create the perfect meta tags for high search engine placement.”>
<meta name=”keywords” content=”Meta Tags Optimization”>
<meta http-equiv=”Content-Type” content=”text/html; charset=ISO-8859-1?>
<link rel=”stylesheet” type=”text/css” href=”../seo-gold.css” media=”all”>
</HEAD>
<body>

The DOCTYPE-

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”>

Is the DOCTYPE, it’s not a meta tag and it’s not essential you add this to a page for good search engine placement, but if you want a page to validate in a HTML validator (i.e. http://validator.w3.org/) you’ll need to add the right one.

The TITLE-

<TITLE>SEO Tutorial – Meta Tags Optimization</TITLE>

Again this isn’t a meta tag, but it’s sometimes referred to as a meta tag by those who don’t fully understand meta tags. The title element is very, very important to a pages optimisation, which is why we have an entire page dedicated to Title Optimization. The title should include the most important phrase for that page and possibly one or two highly relevant keywords to create related phrases. The one above helps several important phrases including Meta Tags Optimization, Meta Tags, Meta Tags Tutorial, SEO Tutorial, SEO Meta Tags etc…. Don’t go over the top with adding lots of keywords, keep it short and sweet so each word gets a reasonable boost and don’t forget potential visitors (they have to read it).

The DESCRIPTION META TAG-

<meta name=”description” content=”Create the perfect meta tags for high search engine placement.”>

As covered earlier in Google the contents of the description meta tag will not have an impact on the pages search engine rankings, but may be used as the description in the search results. So be descriptive, think about what a potential visitor might click on not keyword stuffing.

The KEYWORDS META TAG-

<meta name=”keywords” content=”Meta Tags Optimization”>

Of no value to Google and probably little value to other major search engines. Easiest way to fill this meta tag is by pasting the same contents as the TITLE minus anything you added for visitors to read. In the example above we removed “SEO Tutorial – ” because that is there for visitors who read the TITLE element as part of a search engine listing.

The Character Set and links external files-

<meta http-equiv=”Content-Type” content=”text/html; charset=ISO-8859-1?>
<link rel=”stylesheet” type=”text/css” href=”../seo-gold.css” media=”all”>

Not meta tags and will have no impact on a pages search engine placement, the Character set is used by browsers so the right set of characters are used to display your page. Don’t add one and the browser will use its default (it will guess), this might mean your page doesn’t look right to a visitor. External style sheets (CSS files) and external javascript (JS files) are referenced here. Again no impact on SEO, but if you can push some javascript or markup off your page you should. It saves bandwidth and means your pages load faster.

Meta tag optimization MUST be included as part of any good search engine optimization services but if your SEO efforts end here don’t expect to get great rankings.




-- 
Regards,

Preeti Bagad [BE(CS)] 
SW Engineer Cum Blogger

On Line Assistence :
Y! Messenger : PreetiB.A1Soft@yahoo.com

We are Looking for Paid Guest Post 

On More then 700 Blogs



DA 90+
PR 1+
Index 1000+

Post live within 12-18 hours

Your Price must be good, 

want to work continue on a long term

add or PM @  



Thanks & Regards,
Vidhya Ethiraj [BE (ECE) &MBA (HR)]
Manager HR
On Line Assistance:
Mail me on: pay2flycrew@gmail.com