What is Constructor overloading ?
It is possible to declare more than one constructor in a same class of different type. But default constructor must be one in a class. This satisfies the concept of function overloading. But as we have mentioned the constructor it becomes the constructor overloading in a class.
When an object of a class is instantiated, the class writer can provide various constructors each with a different purpose. Multiple constructors can be declared in a class. There can be any number of constructors in a class of different types.
Syntax:
class myFoo
{
private:
int Useful1;
int Useful2;
public:
myFoo(){ // default constructor
Useful1 = 5;
Useful2 = 10;
}
myFoo( int a, int b = 0 ) { // two possible cases when invoked
Useful1 = a;
Useful2 = b;
}
};
};
The above code can have different cases while calling:
myFoo Find; // default constructor,
myFoo Find( 8 ); // overloaded constructor case 1,
myFoo Find( 8, 256 ); // overloaded constructor case 2,
You must log in to post a comment.