Constructor overloading is a feature in C++ that allows a class to have multiple constructors with different parameter lists. In other words, a class can have multiple constructors with the same name, but different arguments. This provides the flexibility to initialize the object's data members in different ways, depending on the constructor used to create the object.
For example, a class may have a default constructor that takes no arguments and initializes data members to default values, and a parameterized constructor that takes arguments to initialize data members to specific values.
Constructor overloading is useful when you want to provide different ways of initializing an object or when you want to provide default values for some of the object's data members.
#include <iostream>
using namespace std;
class Cls{
private:
string name = "John";
int age = 30;
public:
Cls() {
cout << "DEFAULT DATA - From Private: " << endl;
cout << "Default name: " << name << endl;
cout << "Default age: " << age << endl;
}
Cls(string abs_x) {
cout << "Case when name is changed" << endl;
name = abs_x;
}
Cls(int abs_age) {
cout << "Case when age is changed" << endl;
age = abs_age;
}
Cls(string x, int y) {
cout << "We are changing bout of those" << endl;
name = x;
age = y;
}
void Report() {
cout << "Real Data Atm: " << endl;
cout << "Name Atm: " << name<< endl;
cout << "Age Atm: " << age << endl;
}
};
int main() {
Cls Object;
return 0;
}
Constructor overloading refers to the ability to define multiple constructors in a class, each with a different signature (i.e., different number or types of parameters).
In our example, the Cls
class has four different constructors with different sets of parameters. The first one is a default constructor, which sets default values for the name
and age
variables.
The other three constructors are parameterized constructors. One takes a string parameter to change the value of name
, another takes an integer parameter to change the value of age
, and the third one takes both a string and an integer parameter to change both values at once.
By providing multiple constructors, the class can be initialized in different ways depending on the specific needs of the program.
int main() {
Cls Object("Michael");
Object.Report();
return 0;
}
In the previous code, the constructor with the string parameter is being called, which sets the name to "Michael" and leaves the age with the default value of 30.
int main() {
Cls Object(55);
Object.Report();
return 0;
}
In this case, the constructor Cls(int abs_age)
is called with the argument 55
, which sets the age
variable to 55. Then, the Report()
method is called, which prints out the updated value of age
and the default value of name
.
int main() {
Cls Object("Samantha", 43);
Object.Report();
return 0;
}
This code will create an object of class Cls
with name
set to "Samantha" and age
set to 43, using the constructor that takes both string
and int
arguments. Then, it will call the Report()
member function to print the current values of name
and age
.
No comments:
Post a Comment