Constructor in C++ programming with parameters is known as a parameterized constructor. It will take at least one argument to termed it as parameterized constructors.
#include<iostream.h>
#include<conio.h>
class cls //'cls' is name of Class.
{
public:
cls() //this is default constructor.
{
cout<<"This is Default Constructor"<<endl;
}
cls(char *p) //this is parameterized constructor and passing
{ //here argument.
cout<<"This is "<<p<<" Constructor"<<endl;
}
cls(int roll, char sec) //this is parameterized constructor and
{ //passing here arguments.
cout<<"Roll No is: "<<roll<<endl<<"Section is: "<<sec;
}
};
void main()
{ //'obj', 'obj2' and 'obj3' is objects of Class 'cls'.
cls obj; //calling default constructor.
cls obj2("Parameterized"); //calling and passing argument to parameter.
cls obj3(120122067, 'B'); //calling and passing arguments to parameters.
getch();
}
#include<iostream.h>
#include<conio.h>
#include<string>
class cls //'cls' is name of Class.
{
public:
cls() //this is default constructor.
{
cout<<"This is Default Constructor"<<endl;
}
cls(string p) //this is parameterized constructor and passing
{ //here argument.
cout<<"This is "<<p<<" Constructor"<<endl;
}
cls(int roll, char sec) //this is parameterized constructor and
{ //passing here arguments.
cout<<"Roll No is: "<<roll<<endl<<"Section is: "<<sec;
}
};
void main()
{ //'obj', 'obj2' and 'obj3' is objects of Class 'cls'.
cls obj; //calling default constructor.
cls obj2("Parameterized"); //calling and passing argument to parameter.
cls obj3(120122067, 'B'); //calling and passing arguments to parameters.
getch();
}