Program to find the area of a rectangle using constructor overloading.
output
#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
Constructor Overloading in C++
ReplyDeleteThis comment has been removed by the author.
ReplyDelete