Featured

    Featured Posts

Constructors in an Programming Language




     When we create an object using new, we get an uninitialized object. We then have to set the values of the instance variables to sensible values (almost always this has to be done via appropriate methods, because these variables will be private). It is natural to ask for the
analogue of a declaration of the form:

int i = 10;

which “creates” a variable i of type int and also, simultaneously, sets its initial state. In the object-oriented paradigm, this initialization is performed by special methods, called constructors. A constructor is a method that is called implicitly when the object is created. It cannot be called after the object has been created.

     In Java, a constructor is written as a public method with no return value which takes the same name as the class. For example, we can define a class Date as follows:

class Date{
private int day, month, year;
public Date(int d, int m, int y){
day = d;
month = m;
year = y;
}
}


       Now, if we write

Date d = new Date(27,1,2003);

d points to a new Date object whose values are initialized with day = 27, month = 1 and year = 2003.

       We may want to have more than one way to construct an object. If no year is supplied, we might set the field year to the current year, by default. We can add a second constructor as follows:

public Date(int d, int m)
day = d;
month = m;
year = 2003;
}

Now, if we write

Date d1 = new Date(27,1,2002);
Date d2 = new Date(27,1);

d1 calls the first constructor, as before, while d2 calls the second constructor (where year is set to 2003 by default).

     This ability to have multiple constructors with different “signatures” extends to methods as well. In Java, the signature of a method is its name plus the types of its arguments. One can overload method names just like we have overloaded the constructors above. In fact, in the Java built-in class Arrays, there is a static method sort that sorts arbitrary arrays of scalars. In other words, we can write:
 

double[] darr = new double[100];
int[] iarr = new int[500];
...
Arrays.sort(darr); // sorts contents of darr
Arrays.sort(iarr); // sorts contents of iarr

     This is done by defining, within the class Arrays, multiple methods named sort with different signatures, as follows:

class Arrays{
...
public static void sort(double[] a){ // sorts arrays of double[]
...
}
public static void sort(int[] a){ // sorts arrays of int[]
...
}
...
}

      When we invoke Arrays.sort with an argument a , the type of a
automatically determines which version of the overloaded method sort is used.
      Coming back to constructors, what happens if we have no constructors defined? In this case, Java provides a “default” constructor that takes no arguments and sets all instance variables to some sensible defaults (e.g., an int is set to 0). Suppose we have a class as follows:

class no_constructor{
private int i;
// some methods below
...
}

 

      We would then write something like:

no_constructor n = new no_constructor(); // Note the () after                                                          //the class name

      However, if there is at least one constructor defined in the class, the default constructor is withdrawn. So, if we have the class Date
as given above, it is an error to write:

Date d = new Date();

If we want this to work, we must explicitly add a new constructor that has no arguments.
     Remember that it is not possible to invoke a constructor later. Though Date(int,int,int) is a public “method” in the class, it has a different interpretation. We cannot say;

Date d = new Date(27,1,2003);
...
d.Date(27,1,2003);

One constructor can call another, using the word
this. We can rewrite the two constructors in the class Date as follows:

class Date{
private int day, month, year;
public Date(int d, int m, int y){
day = d;
month = m;
year = y;
}
public Date(int d, int m){
this(d,m,2003);
}
}

The second constructor invokes the first one by supplying a fixed third argument. In Java, such an invocation using this must be the first statement in the constructor. We can reverse the constructors as follows:

class Date{
private int day, month, year;
public Date(int d, int m){
day = d;
month = m;
year = 2003;
}
public Date(int d, int m, int y){
this(d,m); // this sets year to 2003
year = y; // reset year to the value supplied
}
}

The fundamentals of C Programming.


     Communicating with a computer involves speaking the language the computer understands, which immediately rules out English as the language of communication with computer.  However, there is a close analogy between learning English language and learning C language.  The classical method of learning English is to first learn the alphabets used in the language, then learn to combine these alphabets to form words, which in turn, are combined to form sentences and sentences are combined to form paragraphs.  Learning C is similar and easier.  Instead of straight-away learning how to write programs, we must first know what alphabets, numbers and special symbols are used in C, then how using them constants, variables and keywords are constructed, and finally, how are these combined to form an instruction.  A group of instructions would be combined later on to form a program.

The C character Set

     A character denotes any alphabet, digit or special symbol used to represent information.  For example :

Alphabets : A,B,C.........X,Z. or a,b,c........x,y,z.

Digits : 0,1,2,3,4,5,6,7,8,9.

Special symbols : ' ! @ #  % & ^ ~ * ( ) _ - + = \ { } [ ] ; : " < > , . ? /

Constants, Variables and Keywords 

     The alphabets, numbers and special symbols when properly combined form constants, variables and keywords. A constant is an entity that doesn't change, whereas, a variable is an entity that may change.  In any program we typically do lots of calculations.  The results of these calculations are stored in computer's memory.  Like human memory, the computer's memory also consists of millions of cells.  The calculated values are stored in these memory cells. To make the retrieval and usage of these values easy these memory cells are given names.  Since the value stored in each locations may change the names given to these locations are called variable names. 

Types of C constants

     C constants can be divided into two major categories :
(a) Primary Constants
(b) Secondary Constants


Rules for constructing Integer Constants :

  1. An integer constant must have at least one digit.
  2. It must not have a decimal point.
  3. It can be either +ve or -ve.
  4. If no sign precedes and integer constant, it is assumed to be positive.
  5. No commas or blanks are allowed within an integer constant.
  6. The allowable range for integer constants is -32768 to 32767.
     The range of an Integer constant depends upon the compiler.  For a 16-bit compiler like Turbo C or Turbo C++ the range is -32767 to 32767.  For a 32-bit compile, the range would be even greater.
Example : 896
+598
-736

Rules for constructing Real Constants :

     Real constants are often called Floating Point constants.  The real constants could be written in two forms- Fractional form and Exponential form.
Following rules must be observed while constructing real constants expressed in fractional form :
  1. A real constant must have at least one digit.
  2. It must have a decimal point.
  3. It could be either positive or negative.
  4. Default sign is positive.
  5. No commas or blanks are allowed within a real constant.
Ex:  +356.56
125.0
-56.83

Exponential form of representation of real constants is usually used if the value of the constant is either too small or too large.  It, however, doesn't restrict us in any way from using exponential form of representation for other real constants.  In exponential form of representation the real constant is represented in two parts.  The part appearing before 'e' is called mantissa, whereas the part following 'e' is called exponent.  Thus 0.000586 can be represented in exponential form as 5.86e-4

The programmers choiec "C" programming language... Why one should start learning programming with C here's the reasons read on..





     C is a programming language developed at AT & T's Bell Laboratories of USA in 1972.  It was designed and written by a man named Dennis Ritchie.  In the late seventies C began to replace the more familiar languages of the time like PL/I, ALGOL, FORTRAN etc.  No one pushed C language.  It wasn't made the 'official' Bell labs language.  This, without any advertisement, C's reputation spread and its pool of users grew.

     Possibly why C seems so popular is because it is reliable, simple and easy to use.  Moreover, in an industry where newer languages, tools and technologies emerge and vanish day in and day out, a language that has survived for more than three decades has to be really good.

   So, an opinion that is often heard today is C has already super-ceded by languages like C++, C# and Java, "Why we bother to learn C today ?".  There are several reasons for this :
  1. What i believe is that nobody can learn C++ or java directly.  This is because while learning these languages you have things like classes, objects, inheritance, polymorphism, templates, exception handling, references etc. do deal with apart from knowing the actual language elements.  That's why one should learn all the language elements very thoroughly using C language before migrating to C++, C# or Java.  Though this two step learning process may take more time, but at the end of it you will definitely find it worth the trouble.
  2. C++,C# or Java make use of a principle called Object Oriented Programming (OOP) to organize the program.  This organizing principle has lots of advantages to offer.  But even while using this organizing principle you would still need a good hold over the language elements of C and the basic programming skills.
  3. Though many C++ and Java based programming tools and frameworks have evolved over the years the importance of C is still unchallenged because knowingly or unknowingly while using these frameworks and tools you would be still required to use the core C language elements - another good reason why one should learn C before C++, C# or Java.
  4. Major parts of popular operating systems like windows, UNIX, Linus are still written in C.  This is because even today when it comes to performance nothing beats C.  Moreover, if one is to extend the operating  system to work with new devices one needs to write device driver programs.  These programs are exclusively written in C.
  5. Mobile devices phones and palmtops have become rage of today.  Also, common consumer devices like microwave ovens, washing machines and digital cameras are getting smarter by the day.  This smartness comes from a microprocessor, an operating system and a program embedded in these devices.  These programs not only have to run fast but also have to work in limited amount of memory.  No wonder that such programs are written in C.  With these constraints on time and space, C is the language of choice while building such operating systems and programs.
  6. You must have seen several professional 3D computer games where the user navigates some objects, like say a spaceship and fires bullets at the invaders.  The essence of all such games is speed.  Needless to say, such games won't become popular if they takes a long time to move the spaceship or to fire a bullet.  To match the expectations of the player the game has to react fast to the user inputs.  This is where C language scores other languages.  Many popular gaming frameworks have been build using C language.
  7. At times one is required to very closely interact with the hardware devices.  Since C provides several language elements that make this interaction feasible without compromising the  
The fundamentals of C Programming

www.CodeNirvana.in

www.posthatke.com. Powered by Blogger.
Copyright © www.posthatke.com | Blogger Templates | Designed By posthatke.com