Yes, In Java and C++ Constructor can be Parameterized.
- Constructor can take value that is called argument.
- Argument can be any type e.g Integer, Char, Array or any object itself.
- Constructor can take any number of argument.
In C++,Constructor is automatically called when object(instance of class) create.It is special member function of the class.Which constructor has arguments thats called Parameterized Constructor.
Example:
#include<iostream>
#include<conio.h>
using namespace std;
class Example {
// Variable Declaration
int a,b;
public:
//Constructor
Example(int x,int y) {
// Assign Values In Constructor
a=x;
b=y;
cout<<"Im Constructor\n";
}
void Display() {
cout<<"Values :"<<a<<"\t"<<b;
}
};
int main() {
Example Object(10,20);
// Constructor invoked.
Object.Display();
// Wait For Output Screen
getch();
return 0;
}