/* */ Click to Join Live Class with Shankar sir Call 9798158723

What is Parameterized Constructor in C++ ?


The parameterized constructor can be used to pass the arguments to the constructor. The arguments will help to initialize an object when it is created. It is used to provide different values to distinct objects.We can define more than one parameterized constructor according to the need of the user, but we have to follow the rules of the function overloading, like a different set of arguments must be there for each constructor. or default constructor which do not have any parameters, it is however possible to have one or more parameters in a constructor. this type of constructor is knows as Parameters constructor.

Q.) Example of Parameterized constructor in Java ?
    class Car {
	String name;
	String color;

	// parameterize constructor
	public Car(String name, String color) {
		this.name = name;
		this.color = color;
	}

	void run() {
		System.out.println(color + " " + name + " " + " Car is running...");
	}
}

public class Sample {

	public static void main(String[] args) {

		// We we crate an object, the consturctor of the
		// class will be called automatically.
		Car maruti = new Car("Maruti", "Red");
		maruti.run();

		Car honda = new Car("Honda", "Black");
		honda.run();

	}

}

Output
Red Maruti Car is running…,
Black Honda Car is running…