Page 240 - Cs_withBlue_J_C11_Flipbook
P. 240

The output of the preceding program is as follows:
              Result 9
              Result 9

              9.12.3 Constructor Overloading
              When a class has two or more constructors, then it is known as Constructor Overloading. It depends on the number of
              parameters or different types of parameters passed to the constructor to assign the data members of the class while
              creating the objects.

              For example,
                  class cons_over
                  {
                      int a,b;
                      cons_over()
                      {
                          a=0;
                          b=0;
                      }
                      cons_over(int n1,int n2)
                      {
                          a=n1;
                          b=n2;
                      }
                  }


               Some More Programs                                                   #Digital Literacy
                                                                                    #Coding & Computational Thinking

                Program 1      Define a class Employee with the following specifications:
                               Data Members                    :   ename, basicsal, hra, da, pf, gross, net
                               Member Methods
                               Default constructor             :   Initialises all members to their default values
                               Parameterised constructor       :   Takes ename and basicsal as parameters
                               calculate()                     :   calculates the following
                                                                   hra        :   10% of basic
                                                                   da         :   55% of basic
                                                                   pf         :   8.33% of basic
                                                                   gross      :   basicsal+hra+da
                                                                   net        :   gross-pf
                                                                   display()   :   Displays all the data members

                1       import java.util.*;

                2       class Employee
                3       {

                4           String ename;
                5           double basicsal, hra, da, pf, gross, net;

                6           Employee()
                7           {

                8               ename="";


                238238  Touchpad Computer Science-XI
   235   236   237   238   239   240   241   242   243   244   245