P Constructor
P Constructor
class Cube
{
int side;
public:
Cube()
{
side=10;
}
};
int main()
{
Cube c;
cout << c.side;
}
Output : 10
Parameterized Constructor
These are the constructors with parameter. Using this Constructor you can provide different
values to data members of different objects, by passing the appropriate values as argument.
Example-1 :
#include<iostream>
using namespace std;
class A
{
int a;
public:
A(int b):a(b)
{
}
void disp()
{
cout<<a;
}
};
int main()
{
A ob(10);
ob.disp();
return 0;
}