Questions And Answers

More Tutorials

C++ Class Constructor With parameters

In 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 parameters

In 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.

#include <iostream>
#include <string>
using namespace std;

class Vehicle {
    Vehicle(string vehicleColor, string vehicleMake, string vehicleModel) {     
          color = vehicleColor;
          make = vehicleMake;
          model = vehicleModel;
    string color;        
    string make;         
    string model;
    string getColorAsMessage() { 
      return "My color is: " + color;

int main() {

  // Vehicle section
  Vehicle newVehicle("Red", "BMW", "X5");  
  cout << newVehicle.getColorAsMessage() << "\n"; 
  cout << newVehicle.make << "\n"; 

  return 0;


My color is: Red


In this page (written and validated by ) 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:

Share On:

Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using, you agree to have read and accepted our terms of use, cookies and privacy policy.