A Parameterized Constructor is a constructor that takes one or more parameters to initialize an object with user-defined values.
1️⃣ Characteristics of a Parameterized Constructor
✅ Used to initialize objects with custom values.
✅ Takes arguments when the object is created.
✅ Has the same name as the class and no return type.
✅ Overloading is possible (multiple constructors with different parameters).
2️⃣ Example: Parameterized Constructor
#include <iostream>
using namespace std;
class Car {
public:
string brand;
int year;
// Parameterized Constructor
Car(string b, int y) {
brand = b;
year = y;
}
void display() {
cout << "Car Brand: " << brand << ", Year: " << year << endl;
}
};
int main() {
Car car1("Toyota", 2022); // Object initialized with arguments
Car car2("Honda", 2023);
car1.display();
car2.display();
return 0;
}
Output:
Car Brand: Toyota, Year: 2022
Car Brand: Honda, Year: 2023
Explanation:
The constructor Car(string b, int y) initializes objects with user-defined values.
car1("Toyota", 2022) assigns "Toyota" to brand and 2022 to year.
3️⃣ Example: Parameterized Constructor with Private Members
#include <iostream>
using namespace std;
class Student {
private:
string name;
int age;
public:
// Parameterized Constructor
Student(string n, int a) {
name = n;
age = a;
}
void display() {
cout << "Name: " << name << ", Age: " << age << endl;
}
};
int main() {
Student s1("Alice", 20);
Student s2("Bob", 22);
s1.display();
s2.display();
return 0;
}
Output:
Name: Alice, Age: 20
Name: Bob, Age: 22
Explanation:
Private members name and age can only be modified through the constructor.
4️⃣ Example: Using this Pointer in Parameterized Constructor
#include <iostream>
using namespace std;
class Rectangle {
private:
int width, height;
public:
// Parameterized Constructor using this pointer
Rectangle(int width, int height) {
this->width = width;
this->height = height;
}
int area() {
return width * height;
}
};
int main() {
Rectangle rect(10, 5);
cout << "Area: " << rect.area() << endl;
return 0;
}
Output:
Area: 50
Explanation:
The this pointer distinguishes between local variables and class members.
5️⃣ Example: Constructor Overloading (Multiple Constructors)
#include <iostream>
using namespace std;
class Box {
private:
int length, width, height;
public:
// Default Constructor
Box() {
length = width = height = 1;
}
// Parameterized Constructor
Box(int l, int w, int h) {
length = l;
width = w;
height = h;
}
int volume() {
return length * width * height;
}
};
int main() {
Box box1; // Calls Default Constructor
Box box2(4, 5, 6); // Calls Parameterized Constructor
cout << "Volume of Box1: " << box1.volume() << endl;
cout << "Volume of Box2: " << box2.volume() << endl;
return 0;
}
Output:
Volume of Box1: 1
Volume of Box2: 120
Explanation:
Constructor Overloading allows multiple constructors with different parameters.
6️⃣ Summary
7️⃣ When to Use a Parameterized Constructor?
✅ When you want custom initialization of objects.
✅ When you need to pass values at the time of object creation.
✅ When implementing constructor overloading for flexibility.
Would you like to learn about Copy Constructors next? 🚀
0 Comments