Constructor With Default Argument In C++
A default argument is a value provided in a function
declaration that is automatically assigned by the compiler if the caller of the
function doesn’t provide a value for the argument with a default value.
Fig: Constructor With Default Argument In C++ |
Syntax:
class class name
{
Public:
Classname(datatype variablename=value);
----
----
};
Program:
#include<iostream.h>
#include<conio.h>
class
BCA
{
int
a,b;
public:
BCA(int
x,int y=30);
void
display();
};
BCA::BCA(int
x,int y)
{
a=x;
b=y;
}
void
BCA:: display()
{
cout<<”a=”<<a;
cout<<”b=”<<b;
}
void
main()
{
BCA
a1(10);
a1.display();
getch();
}
Output:
a=10
b=30
No comments:
Post a Comment