Parameterized Constructor in C++

Parameterized Constructors make it possible to pass arguments to constructors. Typically, these arguments help initialize an object when it is created. To create a parameterized constructor, simply add parameters to it the way you would to any other function. When you define the constructor’s body, use the parameters to initialize the object.

Syntax of Parameterized Constructor

className (parameters...) {
// body
}

Examples of Parameterized Constructor

The below examples demonstrate how to use the parameterized constructors in C++.

Example 1: Defining Parameterized Constructor Inside the Class.

CPP




// CPP program to illustrate parameterized constructors
#include <iostream>
using namespace std;
 
class Point {
private:
    int x, y;
 
public:
    // Parameterized Constructor
    Point(int x1, int y1)
    {
        x = x1;
        y = y1;
    }
 
    int getX() { return x; }
    int getY() { return y; }
};
 
int main()
{
    // Constructor called
    Point p1(10, 15);
 
    // Access values assigned by constructor
    cout << "p1.x = " << p1.getX()
         << ", p1.y = " << p1.getY();
 
    return 0;
}


Output

p1.x = 10, p1.y = 15

Example 2: Defining Parameterized Constructor Outside the Class.

C++




// C++ Program to illustrate how to define the parameterized
// constructor outside the class
#include <iostream>
#include <string.h>
using namespace std;
 
// class definition
class student {
    int rno;
    char name[50];
    double fee;
 
public:
    student(int, char[], double);
    void display();
};
 
// parameterized constructor outside class
student::student(int no, char n[], double f)
{
    rno = no;
    strcpy(name, n);
    fee = f;
}
 
void student::display()
{
    cout << endl << rno << "\t" << name << "\t" << fee;
}
 
// driver code
int main()
{
    student s(1001, "Ram", 10000);
    s.display();
    return 0;
}


Output

1001    Ram    10000

When an object is declared in a parameterized constructor, the initial values have to be passed as arguments to the constructor function. The normal way of object declaration may not work. The parameterized constructors can be called explicitly or implicitly:

Example e = Example(0, 50);    // Explicit call
Example e(0, 50); // Implicit call

When the parameterized constructor is defined and no default constructor is defined explicitly, the compiler will not implicitly create the default constructor and hence create a simple object as:

Student s;

will flash an error.

Example 3:

C++




// C++ Program to illustrate the error caused be not
// defining the explicit defualt constructor after
// parameterized constructor
#include <iostream>
#include <string.h>
using namespace std;
 
// class definition
class student {
    int rno;
    char name[50];
    double fee;
 
public:
    student(int no, char n[], double f)
    {
        rno = no;
        strcpy(name, n);
        fee = f;
    }
};
 
// driver code
int main()
{
    student s; // this will cause error
    return 0;
}


Output

main.cpp: In function ‘int main()’:
main.cpp:25:13: error: no matching function for call to ‘student::student()’
25 | student s; // this will cause error
| ^
main.cpp:14:5: note: candidate: ‘student::student(int, char*, double)’
14 | student(int no, char n[], double f)
| ^~~~~~~
main.cpp:14:5: note: candidate expects 3 arguments, 0 provided
main.cpp:8:7: note: candidate: ‘constexpr student::student(const student&)’
8 | class student {
| ^~~~~~~
main.cpp:8:7: note: candidate expects 1 argument, 0 provided
main.cpp:8:7: note: candidate: ‘constexpr student::student(student&&)’
main.cpp:8:7: note: candidate expects 1 argument, 0 provided ^~

Important Note: Whenever we define one or more non-default constructors( with parameters ) for a class, a default constructor( without parameters ) should also be explicitly defined as the compiler will not provide a default constructor in this case. However, it is not necessary but it’s considered to be the best practice to always define a default constructor.

Uses of Parameterized Constructor

  • It is used to initialize the various data elements of different objects with different values when they are created.
  • It is used to overload constructors.

Default Arguments with C++ Parameterized Constructor

Just like normal functions, we can also define default values for the arguments of parameterized constructors. All the rules of the default arguments will be applied to these parameters.

Example 3: Defining Parameterized Constructor with Default Values

C++




// C++ Program to illustrate how to use default arguments
// with parameterized constructor
#include <iostream>
using namespace std;
 
// class
class GFG {
private:
    int data;
 
public:
    // parameterized constructor with default values
    GFG(int x = 0) { data = x; }
 
    int getData() { return data; }
};
 
int main()
{
 
    GFG obj1; // will not throw error
 
    GFG obj2(25);
 
    cout << "First Object Data: " << obj1.getData() << endl;
    cout << "Second Object Data: " << obj2.getData()
         << endl;
 
    return 0;
}


Output

First Object Data: 0
Second Object Data: 25

As we can see, when the default values are assigned to every argument of the parameterized constructor, it is legal to create the object without passing any parameters just like default constructors. So, this type of constructor works as both a default and parameterized constructor.

Constructors in C++

Constructor in C++ is a special method that is invoked automatically at the time of object creation. It is used to initialize the data members of new objects generally. The constructor in C++ has the same name as the class or structure. It constructs the values i.e. provides data for the object which is why it is known as a constructor.

  • Constructor is a member function of a class, whose name is the same as the class name.
  • Constructor is a special type of member function that is used to initialize the data members for an object of a class automatically when an object of the same class is created.
  • Constructor is invoked at the time of object creation. It constructs the values i.e. provides data for the object that is why it is known as a constructor.
  • Constructors do not return value, hence they do not have a return type.
  • A constructor gets called automatically when we create the object of the class.
  • Constructors can be overloaded.
  • A constructor can not be declared virtual.

Similar Reads

Syntax of Constructors in C++

The prototype of the constructor looks like this:...

Examples of Constructors in C++

The below examples demonstrate how to declare constructors for a class in C++:...

Characteristics of Constructors in C++

...

Types of Constructor in C++

...

1. Default Constructor in C++

The following are some main characteristics of the constructors in C++:...

2. Parameterized Constructor in C++

Constructors can be classified based on in which situations they are being used. There are 4 types of constructors in C++:...

3. Copy Constructor in C++

A default constructor is a constructor that doesn’t take any argument. It has no parameters. It is also called a zero-argument constructor....

4. Move Constructor in C++

...

Destructors in C++

...

Frequently Asked Questions on C++ Constructors

Parameterized Constructors make it possible to pass arguments to constructors. Typically, these arguments help initialize an object when it is created. To create a parameterized constructor, simply add parameters to it the way you would to any other function. When you define the constructor’s body, use the parameters to initialize the object....