Default constructors, parameterized constructors in c++
In the C++ programming language, constructors are built-in functions or features that create special class members for a user-defined class program. They are automatically called when an object of an existing class is created. User-declared constructors in a class program initialize an object and its data type member values.

There are two main types of constructors in the C++ programming language.
- Default Constructor
- Parameterized Constructor
Default Constructor in C++.
A default constructor in a C++ program is a user-defined class constructor that does not accept any parameter values as input from the current program or that has default user-defined program arguments. If the class does not provide a constructor in the user program, the current compiler automatically creates a default constructor for the class, unless another constructor data type is manually defined in the current program.
If a C++ user creates a custom self-constructor in a program, the C++ compiler will not create a default constructor in that program unless the C++ user explicitly defines it.
Features of a Default Constructor in C++.
- A user-defined default constructor program has no parameter values or all parameters have default values.
- It initializes the class object values in the current class program with a default value.
Example of a default constructor in C++.
#include <iostream>
#include <string>
class Course {
public:
// Here we define a default constructor for the course class
Course() {
course_name = “Random”;
course_price = 000;
}
// Here we declare a Course class member function to display course_preview information.
void course_preview() {
std::cout << “the course_name is – ” << course_name << “\n the course_price – ” << course_price << std::endl;
}
private:
std::string course_name;
int course_price;
};
int main() {
// Here we are creating a Course class object using the default constructor method.
Course course1;
course1.course_preview(); // Result – the course_name is – Random the course_price – 0
return 0;
}
Explanation of a default constructor in C++.
- In this example, a Course object is created without any class arguments. So, in this program, the constructor Course() initializes course_name to “Random” and course_price to 0.
Parameterized constructor in C++.
A parameterized constructor in a C++ program allows a C++ user to initialize a class object with specific class parameter values passed to an existing class constructor when creating a user-defined class object. Such a user-defined class constructor takes one or more parameter values as input.
Features of a parameterized constructor.
- A parameterized constructor takes one or more parameter class values as input to an existing class.
- A parameterized constructor provides features for initializing a user-defined class object in an easier order.
Example of a parameterized constructor in C++.
#include <iostream>
#include <string>
class Course {
Public:
// here we declare a course name Parameterized constructor
Course(std::string cn, int cp) {
c_name = cn;
c_price = cp;
}
// here we define a member function to display course class information
void Course_preview() {
std::cout << “The course Name – ” << c_name << “, The course price is – ” << c_price << std::endl;
}
private:
std::string c_name;
int c_price;
};
int main() {
// Here we Creating an course name class object using the parameterized constructor method
Course coursefirst(“Javascript”, 3099);
coursefirst.Course_preview(); // Result – The course name is Javascript, the course price is 3099
Course coursesecond(“Python”, 4999);
coursesecond.Course_preview(); // Result – The course name is Python, the course price is 4999
return 0;
}
Explanation of a parameterized constructor in C++.
- In this example, the constructor Course(std::string cn, int cp) takes the default parameterized constructor value input as parameters cn (for the course name) and cp (for the course price) to initialize the Course class object with a custom value.
- When coursefirst and coursesecond course class objects are created in the current Course class, the existing course values ”Javascript”, 3099 and “Python”, 4999 are passed to the constructor to initialize their respective class members.
Default vs. Parameterized Constructors in C++ Programming.
default constructor in C++.
- A default constructor in a class program initializes or initializes a class object with default values.
- No arguments are passed to a default constructor when creating a new class object within an existing class.
- Default constructors are used in C++ programs when you want to create a new class object with pre-defined class values.
Parameterized constructors in C++.
- A parameterized constructor initializes a class object with a user-defined class value passed as an argument.
- Parameterized constructors are much more flexible because you can initialize the object with different class values each time you create an instance of the class.
