Friday, November 19, 2010

Exception Handling Basics

Basics:

  • Events are abnormal activities that occur during runtime.
  • Errors are rather cataclysmic; there is no recovery or handling.
  • Exceptions are the unwanted problems in a program that don’t cause irrecoverable damage. Exceptions are, in effect, just errors that can be handled by guessing their occurrence in a particular section of code. They are of two types, Checked or Unchecked.
  • Runtime Environment is aware of most errors, called “Unchecked” Exceptions. E.g. Division by zero, typecasting between incompatible types, etc. Handling of these unchecked exceptions is not required, i.e. they don’t have to be “caught” or “declared thrown”.
  • On the other hand, “Checked” Exceptions must be caught and thrown explicitly. But in terms of functionality both are the same and no extra feature exists in either class of exceptions.
  • Handling is the practice of transferring control to special functions called handlers. Code is inspected in a “try” block, exception cases are detected and caught by “catch” blocks that work to handle and neutralize the problem imposed by the exception.

Important concepts:
(mainly C++)
1. The “try” block
Any statement or group of statements capable of generating an exception should be located in a try block. Exception objects are thrown in Java. In C++, a parameter is thrown whose type is matched to a catch block.

2. The “throw” keyword
This keyword is used to literally throw a value (C++) or object (Java) that is directly sent to catch blocks for evaluation. For example, in C++ one may write
throw 1;
Now a call will be made to a catch block that has the following prototype:
            catch(int arg) {;}

3. The “catch” block
The catch block catches the exception object(Java) or data type(C++). In C++, the type of the thrown parameter is matched to the prototype of the catch block. The match found is used to handle the thrown exception. Catch blocks contain error messages or substitute code that can help avoid the exceptional case.
A single try block can have multiple catch blocks but at least one catch is necessary that can handle all possible exceptions thrown from its parent try.
A default catch block that can accommodate all types is given below. It is used for default handling code.
            catch(…) {;}
“…” is an ellipsis that indicates no type specification.

Tuesday, November 16, 2010

GO Programming Language

GO is a systems programming language by Google.Inc. 
The official website http://golang.org/ describes this language as simple, fast, concurrent, safe, fun, open source and what not. So far it has not been developed for the Windows platform, so if interested, it means you need the good ol' Linux.

GO is basically an OOPs based language that looks a lot like Java but personally I felt it was more like C\C++ given a lot of resources and power. What differentiates it from Java is basically a heavy use of low level features, which anyone would have started missing if they used Java for too long. Three instruction sets for amd86, x86\x86-32, ARM are supported.

Installation is easy if you have ever installed Java before, because it involves setting environmental variables. Programs have a ".go" extension and when compiled the extension changes to name of current commpilor, eg. "file.6". The Linked files are named as ".out"

The main comparisons to C++\Java are implicit Garbage-collection, use of imports through package files, no implicit type-conversions (type-casts are used, called conversions), etc. Also, unlike Java, Pointers ARE supported, but pointer arithmetic is not. Pleas visit the mentioned website for details...

Stack Template Class

The stack template I made. Overflow conditions have been skipped as they practically dont occur on modern PC's.


Please specify class T when you declare the object, for example for 'int',
stack  type_name > stack_object_name;

template class T >
class stack
{
struct node
{
T data;
struct node *next;
}*top;

public:
stack()
{
  top=NULL;
}

void push(const T & value)
{
  struct node *ptr;
  ptr=new node;
  ptr->data=value;
  ptr->next=NULL;
  if(top!=NULL)
  ptr->next=top;
  top=ptr;
  cout<<"\nNew item is inserted to the stack!!!";
  getch();
}

T pop()
{
  struct node *temp;
  if(top==NULL)
  {
  cout<<"\nThe stack is empty!!!";
  getch();
  return;
  }
  temp=top;
  top=top->next;
  T t=temp->data;
  delete temp;
  return t;
}

void show()
{
        if(top==NULL)
   {
  cout<<"\nThe stack is empty!!!";
  getch();
  return;
   }
  struct node *ptr1=top;
  cout<<"\nThe stack is\n";
  while(ptr1!=NULL)
  {
  cout<data<<" ->";
  ptr1=ptr1->next;
  }
}
};

Tuesday, October 26, 2010

Queue Template

The queue template I made. Overflow conditions have been skipped as they practically dont occur on modern PC's.

Please specify class T when you declare the object, for example for 'int',
queue type_name > queue_object_name;

template class T >
class queue
{

   struct node{
   T data;
   struct node *next;
   }*frnt,*rear;

public:
queue()
{
                  frnt=rear=NULL;
}

void insert(const T & value)
{
   struct node *ptr;
   ptr=new node;
   ptr->data=value;
   ptr->next=NULL;
   if(frnt==NULL)
                   frnt=ptr;
   else
                   rear->next=ptr;
   rear=ptr;
   cout<<"\nNew item is inserted to the Queue!!!";
   getch();
}

T del()
{
   if(frnt==NULL)
   {
                   cout<<"\nQueue is empty!!";
                   getch();
                   return;
   }
   struct node *temp;
   temp=frnt;
   frnt=frnt->next;
   T t=temp->data;
   delete temp;
   return t;
}

void show()
{
   struct node *ptr1=frnt;
   if(frnt==NULL)
   {
                   cout<<"The Queue is empty!!";
                   getch();
                   return;
   }
   cout<<"\nThe Queue is\n";
   while(ptr1!=NULL)
   {
                    cout<data<<" ->";
                    ptr1=ptr1->next;
   }
}
};

Sunday, September 19, 2010

Windows 7 and the Inevitble Headache of 32-bit C++

Windows 7 is here,
Windows 7 is the future,
Windows 7 doesnt support my C++!!!!!

TurboC is definitely functional but NOT COMPLETELY. This is alarming, but most conventional compilors and IDE's that worked on the traditional 16-bit environment will not work anymore, atleast not properly. The more advanced the OS becomes, the more detached it becomes from its roots, or so is the trend these days...


This means we cant use the good old c++ we learnt in school anymore, as the current IDE's that are based on 32-bit systems use Mingw Compilor or Minimalist GNU for Windows. These are some problems you will encounter.. 

★ Mingw doesnt support many headers that we would be familiar with, like conio.h, graphics.h, stdio.h. Instead, trimmed versions may be available of conio and stdio depending on where you downloaded it from.
REASON - Well all these headers (and more) are implementations by Borland and are not required to be present on every Compilor we see, virtually sending us in Coder's Hell.
SOLUTION - It took me 30 minutes to search for a Mingw-compatible conio header that was complete. Same effort was required to find graphics libraries similar to what we used in TurboC++. A good alternative was the Devpak called WinBGIm.

★ Another problem is the "iostream" header file, firstly, you cant refer to any variables like cin or cout just like that, they throw errors as if they were never there(declared).
REASON - C++ namespaces allows us to group a set of global classes, objects and/or functions under a name. Namespace std contains all the classes, objects and functions of the standard C++ library. Borland allowed users to write code with Borland specific libraries without mentioning namespaces but now this habbit will have to change for good.
SOLUTION - Learn to live with it. Either use std:: everywhere or specify to the compiler that you are using namespace 'std', as given in the following code...
#include <iostream>
using namespace std;
int main()
{
      cout << "Hello";
      return 0;
}

 Another problem that needs a special mention..."GRAPHICS"!!!, its different from what most coders have done, although it retains many functions from the original "graphics.h", so do read the documentation on WinBGIm before venturing any further.

Besides these there will be minor adjustment  and coding issues for those who were used to TurboC.

Thursday, August 26, 2010

LinkLists in C++: Part 4

Fourth in the series of Link-List funtions.

/***************************************************/
#include< iostream.h >
#include< conio.h >
#include< stdlib.h >

struct node
{
int info;
node* next;
}*start, *end, *t;

void reverse()
{
int n;
t = start;
while(t != NULL)
{
n++;
t = t->next;
}
for(int i = 0;i <= n-3;i++)
{
t = start;
while(t->next->next->next != NULL)
t = t->next;
t->next->next->next = t->next;
t->next->next = NULL;
}
t = start;
t->next->next = start;
t->next = NULL;
t1 = start;
start = end;
end = t1;
}

void sort()
{
t = start;
while(t != NULL)
{
t1 = start;
while(t1 != NULL)
{
if(t1->next->info > t1->next->next->info)
{
t1->next = t1->next->next;
t1->next->next = t1->next->next->next;
t1->next->next->next = t1->next;
}
t1 = t1->next;
}
t = t->next
}
}

LinkLists in C++: Part 3

Third in the series of Link-List funtions.

/***************************************************/
#include< iostream.h >
#include< conio.h >
#include< stdlib.h >

struct node
{
int info;
node* next;
}*start, *end, *t;


void listnode()
{
t = start;
cout <<"\nTHE CONTENTS ARE\n";
while(t != NULL)
{
cout <info <
t = t->next;
}
}

void count()
{
t = start;
int ctr = 0;
while(t != NULL)
{
ctr++;
t = t->next;
}
cout <<"\nNUMBER OF NODES ARE" <
}

void search()
{
t = start;
int n, flag = 0;
cout <<"\nENTER INFO TO BE SEARCHED ::";
cin >>n;
while(t!= NULL)
{
if(t->info == n)
{
flag = 1;
break;
}
}
if(flag == 0)
cout <<"\nNOT FOUND";
else
cout <<"\nFOUND";
}

LinkLists in C++: Part 2

Second in the series of Link-List funtions.

/***************************************************/
#include< iostream.h >
#include< conio.h >
#include< stdlib.h >

struct node
{
int info;
node* next;
}*start, *end, *t;



void delnode()
{
int ch;
cout <<"\n1.DELETE IN THE BEGINNING.";
cout <<"\n2.DELETE AT THE END.";
cout <<"\n3.DELETE IN THE MIDDLE.";
cout <<"\nENTER CHOICE";
cin >> ch;
switch(ch)
{
case 1:delbeg();
break;
case 2:delend();
break;
case 3:delmid();
break;
default:cout <<"\nWRONG CHOICE";
}
}

void delbeg()
{
t = start->next;
start->next = NULL;
delete start;
start = t;
}

void delend()
{
t = start;
while(t->next->next != NULL)
t=t->next;
t->next = NULL;
delete end;
end = t;
}

void delmid()
{
int n;
cout <<"\nENTER THE INFO TO BE DELETED";
cin >>n;
t = start;
while(t != NULL)
{
if(t->next->info == n)
{
t1 = t->next;
t->next = t1->next;
t1->next = NULL;
delete t1;
break;
}
t = t->next;
}
}