Since Constructor is a special member function, Constructors can also be overloaded like functions.
Constructor overloading is similar to function overloading.
A class defined with multiple constructors is called constructor
overloading.
Overloaded constructors have the same name, they differ in their
signature, that is, number of arguments and sequence of arguments passed.
A constructor is called depending upon the number and
type of arguments passed.
Example. //Demonstration on constructor overloading
#include<iostream.h>
#include<conio.h>
class sample
{
private:
int a;
public:
sample()
{
cout<<"\nInside default constructor...";
a=0;
}
sample(int x)
{
cout<<"\nInside parameterized constructor...";
a=x;
}
void show()
{
cout<<"\n a= "<<a;
}
};
void main()
{
clrscr();
sample s1;
s1.show();
sample s2(10);
s2.show();
getch();
}
0 comments:
Post a Comment