Saturday 12 January 2013

Class X - Constructors

Its not ROCKET  SCIENCE.....

Its just COMPUTER SCIENCE .....
Constructor is similar to method .
  • It has the same name as the class.
  • It has no return value not even void.
  for e.g
                  int print( )
                  print ( )
from the above two functions ,the first one cannot be a constructor because of the data type int before the function name.
Characteristics of constructors :
  • It initializes the data members .
  • It is called when the object is created.
  • It cannot be inherited.
There are two types of constructors :
  • Default constructor or non parameterized
  • Parameterized constructor
Default constructor accepts no parameter.
Parameterized constructor accepts parameter.

Example 1:
A class commision is defined with the following specifications :
Class name   : commision
Data members  : double salary(per week)
                              double com(amount per item sold)
                               int qty(total items sold in a week)
Member functions :
commision(double s,double c,int q)
void earn( ) -calculate the total salary
Write a main method to create the object.
class commision
{
double salary,com;------------------------------1
int qty;--------------------------------------------2
commision(double s,double c,int q)-----------3
{
salary =s;-----------------------------------------4
com=c;-------------------------------------------5
qty=q;--------------------------------------------6
}

void earn()
{
     System.out.print((salary+com*qty));
}
public  void main( )
{
commision ob=new commision(20000.0, 15.5, 300);--------7
ob.earn(); ------------------------------------8
}
}
The class name and function name are same so it is a constructor.There are values passed to it,so it is a parameterized constructor.
Steps 1 and 2 are global variables.Refer methods to learn about it.
Step 3 is the papameterized constructor.
In steps 4 to 6 local values are assigned to global variables.earn ia method,since the name is different and also there is a data type void.
In step 7, ob is created, and the values are in order.
commision(sal,com,qty).
In step 8, earn() is called. DONOT call a constructor using the object coz constructors are called automatically  called when the object is created.So do not write ob.commision().

Example 2 :
Define a class Number to find the sum of the digits.
class name     :Number
Data members : int num
Member functions  :
 Number() - assign 0 to num
Number(int n) - assign n to num
void sum()
class Number
{
int num;
Number()
{
num=0;
}
Number (int n)
{
num=n;
}
void sum( )
{ int a,s=0;
while(num>0)
{
a=num%10;
s=s+a;
num=num/10;
}
System.out.print(s);
}
public void main( )
{
Number ob=new Number(145);
ob.sum();
}
}
In the above program both the constructor types are used.Just follow what is given in the question.Make sure the names and variables are used as it is given in the question.
Remember to write the global variable on the left hand side and local variable on the right hand side in  the constructor Number(int n).Donot mix it up

I will keep posting the other chapters in the coming days.If you have any doubts mail me at kavitha.m.samuel@gmail.com

No comments:

Post a Comment