Pages

100 Contemporary Living Room Designs

Sunday, August 5, 2012

CONSTRUCTOR OVERLOADING

Program to find the area of a rectangle using constructor overloading.


#include<iostream.h>
#include<conio.h>
class area
{
   int a,l,b;
   public:
   area() // simple constructor definition.
   {
      l=5;
      b=6;
      cout<<"Simple constructor called\n";
      cout<<"length="<<l<<"\nbreadth="<<b<<endl;
   }
   area(int x,int y) // parameterised constructor
   {
      l=x;
      b=y;
   }
   void calc();
   void print();
};
void area::calc()
{
    a=l*b;
}
void area::print()
{
    cout<<"Area is : "<<a<<endl;
}
void main()
{
    int l,b;
    clrscr();
    area a1; // simple constructor is called.
    a1.calc();
    a1.print();
    cout<<"Enter length and breadth for parameterised         constructor:\n";
    cin>>l>>b;
    area a2(l,b); // parameterised constructor is called.
    a2.calc();
    a2.print();
    getch();
}

output



2 comments: