Saturday, May 19, 2012

 ExecuteReader, ExecuteNonQuery and ExecuteScalar

What is difference between ExecuteReader, ExecuteNonQuery and ExecuteScalar.

ExecuteReader : Use for accessing data. It provides a forward-only, read-only, connected recordset
ExecuteNonQuery : Use for data manipulation, such as Insert, Update, Delete.
ExecuteScalar : Use for retriving 1 row 1 col. value., i.e. Single value. eg: for retriving aggregate 
function. It is faster than other ways of retriving a single value from DB.


Posted By : Pratik Pachani
Stay Connected

Monday, April 30, 2012

POINTERS, VIRTUAL FUNCTIONS AND POLYMORPHISM

• Polymorphism simply means ‘one name multiple forms.’

ACHIEVING POLYMORPHISM COMPILE TIME POLYMORPHISM


The overloaded member functions are selected for invoking by matching arguments, both type and member. The compiler knows this information at the compile time and therefore, compiler is able to select the appropriate function for a particular call at the compile time itself. This is called early or static binding or static linking. It means that an object is bound to its function call at compile time.

RUN TIME POLYMORPHISM

An appropriate member function is selected while the program is running. C++ supports run-time polymorphism with the help of virtual functions. It is called late or dynamic binding because the appropriate function is selected dynamically at run time. Dynamic binding requires use of pointers to objects and is one of the powerful features of C++.
For e.g.
class A
{ int x;
public:
  void show() {….}  
  //show() in the base class
};
 Class B:public A
{ int y;
public:
void show() {…} 
// show() in derived class
};

Since, the prototype of show() is the same in both the places, the function is not overloaded and therefore static binding does not apply. (Class resolution operator can be also used). But, it would be nice if the appropriate member function could be selected while the function could be selected while the program is running. This is known as run time polymorphism and is achieved through virtual functions.

POINTERS

1. A pointer is a derived data type that refers to another data variable by storing the variable’s memory address rather than data.
2. A pointer variable defines where to get the value of a specific data variable instead of defining the actual data.
3. Pointers provide an alternative approach to access other data objects.
NOTE: we can locate asterisk (*) immediately before the pointer variable, or between the data type and the pointer variable, or immediately after the data type. It does not cause any effect in the execution process.
e.g.
#include<iostream.h>
#include<conio.h>
void main()
{ int a,*ptr1,**ptr2; 
//double pointer refers to memory address of the pointer
 clrscr();
 a=10;
 ptr1=&a;
 ptr2=&ptr1;
 cout<<"address of a:"<<ptr1<<endl;
 cout<<"address of ptr2:"<<ptr2<<endl;
 cout<<'after incrementing address values \n";
 ptr1=ptr1+2; // 204+2*2bytes
 ptr2=ptr2+2;
 cout<<"address of a:"<<ptr1;
 cout<<"\n address of ptr1":ptr2;
 cout<<"the value of a:"<<a;
 (*ptr1)=(*ptr1)/2;
 cout<<"\n value of a after calculation is"<< a; 
 //here a prints the value of *ptr
}

OUTPUT:
address of a:204
address of ptr1:306
after incrementing address values:
address of a:208
address of ptr1:310
the value of a is 10
value of a after calculation is 5
4. Void pointers, also known as generic pointers refer to variables of any data type. Before using void pointers, we must type cast the variables to the specific data types that they point to.
5. The pointers, which are not initialized in a program, are called null pointers. Pointers of any data type can be assigned with one value i.e. , ‘0’ called null address.
6. A pointer can be manipulated with the indirection operator i.e. ‘*’, which is also known as dereference operator.
7. Dereferencing a pointer allow us to get the content of the memory location that the pointer points to. Using the dereference operator, we can change the contents of the memory location.
8. Before dereferencing a pointer, it is essential to assign a value to the pointer. If we attempt to dereference an uninitialized pointer, it will cause runtime error by referring to any other location in memory.
9. C++ allows pointers to perform the following arithmetic operations: • A pointer can be incremented (++) (or) decremented (--).
• Any integer can be added to or subtracted from a pointer.
• One pointer can be subtracted from another.
For e.g.
#include<iostream.h>
#include<conio.h>
void main()
{int num[]={56,75,22,18,90};
 int *ptr,i;
 clrscr();
cout<<"the array values are:\n";
for(i=0;i<5;i++)
  cout<<num[i]<<"\n";
ptr=num;
cout<<"\n value of ptr:"<<*ptr;
ptr++;
cout<<"\n value of ptr++:"<<*ptr;
ptr--;
cout<<"\n value of ptr--:"<<*ptr;
ptr=ptr+2;
cout<<"\n value of ptr+2:"<<*ptr;
getch();
}

OUTPUT:
the array values are:56,75,22,18,90
value of ptr:56
value of ptr++:75
value of ptr--:56
value of ptr+2:22
10. Pointers are useful to allocate arrays dynamically, i.e., we can decide the array size at run time.
11. There is no error checking of array bounds in C++. Suppose we declare an array of size 25. The compiler issues no warnings if we attempt to access 26th location. It is the programmer’s task to check the array limits.
12. The array of pointers represents a collection of addresses. It points to an array of data items. Each element of the pointer array points to the element of the data array. Data items can be accessed either directly or by dereferencing the elements of pointer array.
13. char num[]=”one”; //creates an array of 4 characters which point to ‘o’, ’n’, ’e’,’\0’.
const char * numptr=”one”; // generates a pointer variable, which points to the first character, i.e., ‘o’ of the string.
14. The concept of pointer to function acts as a base for pointers to members.
15. The pointer to function is known as callback function.
16. Using function pointers, we can allow a C++ program to select a function dynamically at run time.
17. The function pointers cannot be dereferenced.
18. C++ provides 2 types of function pointers:
• Function pointers that point to static member functions
• Function pointers that point to non-static member functions. (require hidden arguments)
• Both are incompatible with each other.
SYNTAX: data-type(*function_name)();
For e.g.
#include<iostream.h>
typedef void(*funptr)(int,int);
void add(int i,int j)
{ cout<<i<<"+"<<j<<"="<<i+j;
}
void subtract(int i,int j)
{ cout<<i<<"-"<<j<<"="<<i-j;
}
int main()
{ funptr ptr;
  ptr=&add;
  ptr(1,2);
  ptr=&subtract;
  ptr(3,2);
  return 0;
}

OUTPUT:
1+2=3
3-2+1

POINTERS TO OBJECTS

1. Object pointers are useful in creating objects at the run time.
2. Member functions of a class can be referenced in 2 ways:
• Using dot operator and object x.getdata(100,2), or x.show();
• Using arrow operator and object pointer Ptr->getdata(100,2) or ptr->show(); We can also use this method, (*ptr).show(); //here *ptr is an alias for x.
[The parenthesis is necessary because the dot operator has higher precedence than the indirection operator *]
Syntax: class-name *pointer-name=&object
3. We can also create objects using pointers and new operator as follows: Item *ptr=new item; //item is a class-name This statement allocates enough memory for the data members in the object structure and assigns the address of the memory space to ptr.
4. If a class has a constructor with arguments and does not include an empty constructor, then we must supply the arguments when the object is created.
5. We can also create an array of objects using pointers.
For e.g. , item *ptr=new item[10]; //array of 10 objects creates memory space for an array of 10 objects of item. Remember, in such cases, if the class contains constructors, it must also contain an empty constructor.
For e.g.:
#include<iostream.h>
class item
{ int code;
  float price;
public:
 void getdata(int a,float b)
{ code=a; price=b;}
void show(void)
{cout<<code<<price;}
};
const int size=2;
int main()
{ item *p=new item[size];
  item *d=p;
  item x,i; 
  float y;
for(i=0;i<size;i++)
{ cin>>x>>y;
  p->getdata(x,y);
  p++;
}
for(i=0;i<size;i++)
{ d->show();
  d++;
}
return 0;
}

OUTPUT:
40 500
50 600
code:40
price:500
code:50
price:600

POINTERS TO DERIVED CLASS

• Pointers to objects of a base class are type-compatible with pointers to objects of a derived class. Therefore, a single pointer variable can be made to point to objects belonging to different classes.
• A base pointer, even when it is made to contain the address of a derived class, always executes the function in the base class. The compiler simply ignores the contents of the pointer and chooses the member function that matches the types of the pointer.
• Although, a base pointer can be made to point to any number of derived objects, it cannot directly objects, it cannot directly access the members defined by a derived class.

VIRTUAL FUNCTIONS


• When we use the same function name in both the derived and base classes, the function in base class is declared as virtual using the keyword virtual preceding its normal declaration.
• When a function is made virtual, C++ determines which function to use at run time based on the type of object pointed to by the base pointer, rather than the type of the pointer. Thus, by making a base pointer. Thus, by making the base pointer to point to different objects, we can execute different versions of the virtual functions.
• To achieve run- time polymorphism, we must access virtual functions through the use of a pointer declared as a pointer to the base class.
For e.g.:
#include<iostream.h>
class base{
public:
void display()             
// display is not virtual.Hence, called both the times
{ cout<<"display base";}
virtual void show()
{cout<<"show base";}
};
class derived
{ public:
  int d;
void display()
{cout<<"display derived";}
void show()
{cout<<"show derived";}
};
int main()
{ base B; 
  derived D;
  base *ptr; bptr=&B;
  bptr->display();  //calls base version
  bptr->show(); //calls base version
  bptr->d;  //does not work
  bptr=&D;
  bptr->display(); //calls base version
  bptr->display(); //calls derived version
return 0;
}

OUTPUT:
display base
show base
display base
show derived

PURE VIRTUAL FUNCTIONS

• A virtual function, equated to zero is called a ‘pure virtual function’ or ‘do-nothing’ functions. They only work as a placeholder.
• It is a function declared in a base class that has no definition related to the base class.
• A class containing such pure function is called an abstract class.

RULES FOR VIRTUAL FUNCTIONS

1. Must be members of some class.
2. Cannot be static members.
3. Accessed by using object pointers.
4. Can be a friend of another class.
5. It must be defined in a base class, even though it may not be used.
6. The prototypes of the base class version of a virtual function and all the derived class versions must be identical. If two functions with the same name have different prototypes, C++ considers them as overloaded functions, and the virtual function mechanism is ignored.
7. We cannot have virtual constructors, but we can have virtual destructors.
8. While a base pointer can type of the derived object, the reverse is not true. That is to say, we cannot use a pointer to a derived class to access an object of the base type. (this is because, base class pointer is compatible to both base and derived class, whereas the reverse is not true)
9. When a base pointer points to a derived class, incrementing or decrementing will not make it to point to the next object of the derived class. It is incremented or decremented only relative to its base type. Therefore, we should not use this method to use this method to move the pointer to the next object.
10. If a virtual function is defined in the base class, it need not be necessarily redefined in the derived class. In such cases, calls will invoke the base function.
NOTE:
If in the previous example, no inheritance was done, then bptr=&D would have shown a matching error or a compatibility error. Since, inheritance had been done, so no error takes place. Instead, it works as a void pointer and because of generic programming, both the time, shows the display function of the base class.

THIS POINTER

1. A ‘this pointer’ refers to an object that currently invokes a member function.
For example, the function call a show() will set the pointer ’this’ to the address of the object ‘a’. The starting address is the same as the address of the first variable in the class structure.
2. This keyword is basically used to represent an object that invokes a member function.
3. This unique pointer is automatically passed to a member function when it is called. The pointer this acts as an implicit argument to all the member functions.
For example:
class ABC 
{ int a;
…
..};
a=123;  this -> a=123;
APPLICATIONS
1. Implicit used while overloading the operators using member function.
• When a binary operator is overloaded using a member function, we pass only one argument to the function. The other argument is implicitly passed using the pointer ‘this’. 2. returns the object it points to return * this; inside a member function will return the object that invoked the function. It is important, when we want to compare two or more objects inside a member function and return the invoking object as a result. For e.g.
person & person::greater(person & x)
{ if(x.age>age)
  return x;
else
return *this;
}
Is invoked by function call as max=A.greater(B);
The above function returns object B, if age of person A. else, it will return object A, i.e. invoking object using pointer this.

Saturday, November 26, 2011

What is Android? and Android Application?

Android is a software stack for mobile devices that includes an operating system, middleware and key applications. TheAndroid SDK provides the tools and APIs necessary to begin developing applications on the Android platform using the Java programming language.



Features

  • Application framework enabling reuse and replacement of components
  • Dalvik virtual machine optimized for mobile devices
  • Integrated browser based on the open source WebKit engine
  • Optimized graphics powered by a custom 2D graphics library; 3D graphics based on the OpenGL ES 1.0 specification (hardware acceleration optional)
  • SQLite for structured data storage
  • Media support for common audio, video, and still image formats (MPEG4, H.264, MP3, AAC, AMR, JPG, PNG, GIF)
  • GSM Telephony (hardware dependent)
  • Bluetooth, EDGE, 3G, and WiFi (hardware dependent)
  • Camera, GPS, compass, and accelerometer (hardware dependent)
  • Rich development environment including a device emulator, tools for debugging, memory and performance profiling, and a plugin for the Eclipse IDE.

Application Fundamentals

Android applications are written in the Java programming language. The Android SDK tools compile the code—along with any data and resource files—into an Android package, an archive file with an .apk suffix. All the code in a single .apk file is considered to be one application and is the file that Android-powered devices use to install the application.
Once installed on a device, each Android application lives in its own security sandbox:
  • The Android operating system is a multi-user Linux system in which each application is a different user.
  • By default, the system assigns each application a unique Linux user ID (the ID is used only by the system and is unknown to the application). The system sets permissions for all the files in an application so that only the user ID assigned to that application can access them.
  • Each process has its own virtual machine (VM), so an application's code runs in isolation from other applications.
  • By default, every application runs in its own Linux process. Android starts the process when any of the application's components need to be executed, then shuts down the process when it's no longer needed or when the system must recover memory for other applications.
In this way, the Android system implements the principle of least privilege. That is, each application, by default, has access only to the components that it requires to do its work and no more. This creates a very secure environment in which an application cannot access parts of the system for which it is not given permission.
However, there are ways for an application to share data with other applications and for an application to access system services:
  • It's possible to arrange for two applications to share the same Linux user ID, in which case they are able to access each other's files. To conserve system resources, applications with the same user ID can also arrange to run in the same Linux process and share the same VM (the applications must also be signed with the same certificate).
  • An application can request permission to access device data such as the user's contacts, SMS messages, the mountable storage (SD card), camera, Bluetooth, and more. All application permissions must be granted by the user at install time.
That covers the basics regarding how an Android application exists within the system. The rest of this document introduces you to:
  • The core framework components that define your application.
  • The manifest file in which you declare components and required device features for your application.
  • Resources that are separate from the application code and allow your application to gracefully optimize its behavior for a variety of device configurations.

Thursday, November 10, 2011

Microsoft release new operating system : Windows 8


The next major release for the Microsoft Windows operating system, Windows 8 is expected to make its debut in the first half of 2012. While many of its features and capabilities are not known at this point, Windows 8 is expected to be a completely redesigned operating system that's been developed from the ground up with touchscreen use in mind as well as near-instant-on capabilities that enable a Windows 8 PC to load and start up in a matter of seconds rather than in minutes – although it should be noted that this is a feature that's been promised in countless previous releases of Windows as well.
Windows 8 is also expected to replace the more traditional Microsoft WindowsOS look and feel with a new "Metro" design system interface that first debuted in the Windows Phone 7 mobile operating system. The Metro user interface primarily consists of a "Start screen" made up of "Live Tiles," which are links to applications and features that are dynamic and update in real time.  Users will also be able to switch between apps in Metro by simply swiping across the screen.
Windows 8 is expected to include the latest Internet Explorer Web browser, Internet Explorer 10, as well as integrated cloud-based online services like Microsoft’s app store and Office 365.  The new operating system will also likely add built-in support for newer technologies like USB 3.0 and Bluetooth 3.0.

Windows Phone


A mobile operating system for smartphones and mobile devices that serves as the successor to Microsoft's initial mobile OS platform system, Windows Mobile. Unlike Windows Mobile, Windows Phone 7 (also referred to as WinPho7) is targeted more to the consumer market than the enterprise market, and it replaces the more traditional Microsoft Windows OS look and feel with a new "Metro" design system user interface. 
Metro's interface consists largely of a "Start screen" made up of "Live Tiles," which are links to applications and features that are dynamic and update in real time.  The Metro design is also expected to be prominently featured in Windows 8 as well. 
As with Apple's iOS, Google's Android OS and the BlackBerry OS, third-party applications can be developed for Microsoft's Windows Phone 7 and are made available through Microsoft's Windows Marketplace for Mobile. Microsoft doesn't enable smartphones running versions of the older Windows Mobile operating system to be upgraded to Windows Phone 7, and Windows Phone 7 also doesn't offer backward compatibility with Windows Mobile applications.
Windows Phone 7 features a multi-tab Internet Explorer Mobile Web browser that uses a rendering engine based on Internet Explorer 9 as well Microsoft Office Mobile, a version of Microsoft Office that’s tailored for mobile devices. A new update for Windows Phone 7 called "Mango" is expected to add a variety of critical new features, including visual voicemail, third-party multitasking, a fully HTML5-compliant Web browser (Internet Explorer 9 Mobile), integrated instant messaging via Facebook and Windows Live Messenger, face-detection software capabilities, custom ringtones, threaded e-mail and messaging conversations support and more.

What is Website Optimization?


1) Also called search engine optimization (SEO),  website optimization is a phrase that describes the procedures used to optimize – or to design from scratch – a website to rank well in search engines.  Website optimization includes processes such as adding relevant keyword and phrases on the website, editing meta tags, image tags, and optimizing other components of your website to ensure that it is accessible to a search engine and improve the overall chances that the website will be indexed by search engines.
2) A phrase used to describe the procedures to optimize the speed at which your website loads in a Web browser.  This type of optimization generally involves editing your website to optimize scripts, HTML or CSS code for faster loading.  It's also reduces the number of components such as images, scripts, or video components that are needed to render the webpage.

Software


Computer instructions or data. Anything that can be stored electronically is software. The storage devices and display devices are hardware.
The terms software and hardware are used as both nouns and adjectives. For example, you can say: "The problem lies in the software," meaning that there is a problem with the program or data, not with the computer itself. You can also say: "It's a software problem."
The distinction between software and hardware is sometimes confusing because they are so integrally linked. Clearly, when you purchase a program, you are buying software. But to buy the software, you need to buy the disk(hardware) on which the software is recorded.
Software is often divided into two categories:

  • systems software : Includes the operating system and all theutilities that enable the computer to function.

  • applications software : Includes programs that do real work forusers. For example, word processors, spreadsheets, and database management systems fall under the category of applications software.