Page 216 - computer science (868) class 11
P. 216

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

              8.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="";


                214214  Touchpad Computer Science-XI
   211   212   213   214   215   216   217   218   219   220   221