Like
other functions in C++, constructors can also be defined with default
arguments. If an arguments are initialized at the time of constructor
declaration/definition, then that type of constructors are called default argument constructor.
The
initialization of arguments at the time of constructor declaration must be in
trailing order. And the missing arguments must be the trailing ones.
Program: Illustrate the default argument
constructor
#include<iostream>
usage namespace std;
class power
{
int num;
int pwr;
int ans;
public:
power(int n = 3 , int
p = 2 );
void show( )
{
cout<<
num <<”power”<< pwr <<”is:”<< ans ;
}
};
power :: power( int n,int p)
{
num = n;
pwr = p;
ans = pow( n , p ) // math pow function
}
int
main( )
{
clrscr( );
power p1, p2(5);
p1.show( );
p2.show( );
return 0;
}
Result:
3
power 2 is 9
5 power 2 is 25
0 comments:
Post a Comment