Multiple Constructor in a class(Constructor Overloading) in c++

Multiple Constructor in a class(Constructor Overloading)

-In a single class there may be more than one constructor
This situation is referred as overloaded constructor or we can say construction is overloaded

#include<iostream>
#include<conio.h>
using namespace std;
class rectangle
{
int length, breadth;
public:
rectangle()
{
length=0;
breadth=0;
}
rectangle(int a, int b)
{
length=a;
breadth=b;
}
rectangle(int x)
{
length=x;
breadth=x;
}
void display()
{
cout<<"Area is:"<< (length*breadth)<<endl;;
}
};
int main()
{
rectangle r1;
rectangle r2(10);
rectangle r3(10,5);
r1.display();
r2.display();
r3.display();
getch();

}



Output:


2 comments:

  1. Constructors method of a class is a representation of object so the return is an object instant , some times we use arguments to build objects that passed through class method constructor called from main program

    ReplyDelete
  2. because it's not called directly by your code, it's called by the memory allocation and object initialization code in the runtime. Its return value (if it actually has one when compiled down to machine code)

    ReplyDelete

Powered by Blogger.