ab_tanjir

Untitled

Jan 8th, 2019
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.05 KB | None | 0 0
  1. class Rectangle
  2. {
  3.         int length;
  4.         int breadth;
  5.         //constructor to initialize length and bredth of rectang of rectangle
  6.         Rectangle(int l, int b)
  7.         {  
  8.            length = l;
  9.            breadth= b;
  10.         }
  11.         //copy constructor
  12.         Rectangle(Rectangle obj)
  13.         {
  14.           System.out.println("Copy Constructor Invoked");
  15.           length = obj.length;
  16.           breadth= obj.breadth;
  17.         }
  18.        //method to calcuate area of rectangle
  19.        int area()
  20.        {
  21.           return (length * breadth);
  22.        }
  23. }
  24.        //class to create Rectangle object and calculate area
  25.        class CopyConstructor
  26. {
  27.           public static void main(String[] args)
  28.           {
  29.             Rectangle firstRect = new Rectangle(5,6);
  30.             Rectangle secondRect= new Rectangle(firstRect);
  31.             System.out.println("Area  of First Rectangle : "+ firstRect.area());
  32.             System .out.println("Area of First Second Rectangle : "+ secondRect.area());
  33.           }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment