Like normal functions, constructors can also be defined with default arguments.
When an object of a class is created, the C++ calls the suitable
constructor for initializing that object.
The default arguments are specified in the definition of a constructor.
Syntax:-
public:
Classname( datatype var=value, datatype var=value, …)
{
Statements;
}
Example. //Demonstration of constructor with default arguments
#include<iostream.h>
#include<conio.h>
class sample
{
private:
int a,b;
public:
sample()
{ a=b=0; }
sample(int x,int y=0)
{
a=x;
b=y;
}
void show()
{ cout<<"\n a= "<<a<<"\t b=
"<<b;
}
};
void main()
{
clrscr();
sample s(10);
s.show();
getch();
}
0 comments:
Post a Comment