Page 124 - 2617_JSSPS_C-7
P. 124

By Assigning the Values of an Object into Another

              There is another way to create a copy constructor by assigning the values of an object to another.
              For example:

              1  class rectangle3

              2  {

              3     int length, breadth, area, perimeter;
              4     rectangle3 (int l, int b)

              5     {

              6       length=l;
              7       breadth=b;

              8     }

              9     void cal()
              10     {

              11       area = length * breadth;

              12       perimeter = 2*(length+breadth);

              13     }
              14

              15     void display ()

              16     {   System.out.println("Area of Rectangle:" + area);
              17       System.out.println("Perimeter of Rectangle:" + perimeter);

              18     }

              19     public static void main(String[] args)

              20     {
              21       rectangle3 ob1 = new rectangle3 (5,4);

              22       rectangle3 ob2 = ob1;

              23       ob1.cal ();
              24       ob1.display();

              25       ob2.cal ();

              26       ob2.display();
              27       }

              28  }
              The preceding program display the same output as the previous program.






                        Premium Edition-VII
                122
   119   120   121   122   123   124   125   126   127   128   129