> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# C++ Parameterized Constructor

A parameterized constructor is a special class member function that accepts one or more arguments to initialize an object's state. In C++, object creation is a sequential process: memory is first allocated (e.g., via stack frame setup or `operator new`), and the constructor is subsequently invoked on that already-allocated memory to initialize the member variables with caller-provided values.

## Syntax and Implementation

A parameterized constructor shares the exact name of its class and has no return type. The standard practice in C++ is to initialize member variables using a **member initializer list** rather than assigning values within the constructor body. This approach directly constructs the members, avoiding a default-construction phase followed by an assignment operation.

```cpp theme={"dark"}
class Vector3D {
private:
    double x;
    double y;
    double z;

public:
    // Parameterized constructor declaration
    Vector3D(double x_val, double y_val, double z_val);
};

// Out-of-line definition using a member initializer list
Vector3D::Vector3D(double x_val, double y_val, double z_val) 
    : x(x_val), y(y_val), z(z_val) {
    // Constructor body executes after the initializer list
}
```

## Instantiation Mechanisms

C++ provides multiple syntactical approaches to invoke a parameterized constructor during object creation:

```cpp theme={"dark"}
// 1. Direct Initialization
Vector3D v1(1.0, 2.0, 3.0);

// 2. Explicit Call
Vector3D v2 = Vector3D(4.0, 5.0, 6.0);

// 3. Direct-List-Initialization / Brace Initialization (C++11 and later)
// Prevents narrowing conversions
Vector3D v3{7.0, 8.0, 9.0};

// 4. Dynamic Allocation
Vector3D* v4 = new Vector3D(10.0, 11.0, 12.0);
```

## Technical Characteristics

**Suppression of the Implicit Default Constructor**
When a parameterized constructor is declared, the C++ compiler automatically suppresses the generation of the implicit default constructor. If the class requires both parameterized and default instantiation, the default constructor must be explicitly declared or defaulted.

```cpp theme={"dark"}
class Matrix {
public:
    Matrix(int rows, int cols); // Suppresses default constructor
    Matrix() = default;         // Explicitly restores default constructor
};
```

**Constructor Overloading**
A class can define multiple parameterized constructors. The compiler resolves which constructor to invoke based on the arity (number of arguments) and the data types of the arguments provided during instantiation.

```cpp theme={"dark"}
class StringWrapper {
public:
    StringWrapper(const char* str);        // Overload 1
    StringWrapper(const char* str, int n); // Overload 2
};
```

**Converting Constructors and the `explicit` Specifier**
By default, constructors can act as implicit conversion operators. A constructor callable with a single argument—whether it defines exactly one parameter, or defines multiple parameters where all but the first possess default arguments (e.g., `MyClass(int a, int b = 0)`)—enables implicit conversion from the argument type to the class type.

Furthermore, since C++11, constructors with multiple parameters also act as implicit converting constructors during copy-list-initialization. To strictly enforce direct initialization and prevent unintended implicit conversions, the `explicit` keyword must be applied to the constructor declaration.

```cpp theme={"dark"}
class Identifier {
private:
    int id;
    int sub_id;
public:
    // Callable with one argument due to the default parameter.
    // 'explicit' prevents implicit conversions and copy-list-initialization.
    explicit Identifier(int id_val, int sub_val = 0) : id(id_val), sub_id(sub_val) {}
};

// Identifier obj1 = 42;      // Error: Implicit conversion blocked by 'explicit'
// Identifier obj2 = {42, 1}; // Error: Copy-list-initialization blocked by 'explicit'
Identifier obj3(42);          // Valid: Direct initialization
Identifier obj4{42, 1};       // Valid: Direct-list-initialization
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor C++ Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
