C++ Class Constructor With parametersIn C++, Like we mentioned in Constructor section, it's a special method that is automatically called when an object of that class is created.
The constructor function has the same name of Class followed by parenthesis.
In section you will learn that constructor may also accept parameters, these parameters for example can be used to assign variables values when creating a new object.
C++ Example of Class constructor With parametersIn the below example you will see we created a public function called:
Vehicle(string vehicleColor, string vehicleMake, string vehicleModel)which is the same as the class name but has parameters, in this function we are assigning default values for color, make, model.
When we defined the
Vehicle newVehicle;it will run the code within the constructor and add default values.
My color is: Red
In this page (written and validated by A. Gawali) you learned about C++ Class Constructor With parameters . What's Next? If you are interested in completing Cpp tutorial, your next topic will be learning about: Cpp Create File.
Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: firstname.lastname@example.org.