Technical Discussion :: C++
Home > Technical Interview Q&A > C++ > Discussion
2 / 187
What is parameterized constructors, Give example?
Answer:
It may be necessary to initialize the various data elements of different objects with different values when they are created.
C++ permits us to achieve this objective by passing an argument to the constructor function when the objects are created. The constructors that can take arguments are called parameterized constructors.
Eg:
Class A
{
int m,n;
public:
A(int x,int y); //parameterized constructor
………..
………..
};
A : : A(int x,int y)
{
m=x;
n=y;
}
Asked In ::
Harry
23 Feb, 2019 5:07 AM
It may be necessary to initialize the various data elements of different objects with different values when they are created.
C++ permits us to achieve this objective by passing an argument to the constructor function when the objects are created. The constructors that can take arguments are called parameterized constructors.
Eg:
Class A
{
int m,n;
public:
A(int x,int y); //parameterized constructor
………..
………..
};
A : : A(int x,int y)
{
m=x;
n=y;
}
C++ permits us to achieve this objective by passing an argument to the constructor function when the objects are created. The constructors that can take arguments are called parameterized constructors.
Eg:
Class A
{
int m,n;
public:
A(int x,int y); //parameterized constructor
………..
………..
};
A : : A(int x,int y)
{
m=x;
n=y;
}