PARAMETERIZED CONSTRUCTORS
It means constructors with the parameters. Different form the default constructors because default constructors have no parameters. You can have as many parameterized constructors that you want but it should have the different number or types of arguments. The compiler finds out which constructor to invoke by comparing the number or the type of arguments(just like we were having in function overloading).
Syntax: constructor-name(parameters)
{
body of the constructor
}
Example:
class student{
private: int roll_no;
public:
student(int r) //Parameterized constructor
{
roll_no=r;
}
.
.
};
void main()
{
clrscr();
int a=10;
student s1(a); //This will invoke the parameterized constructor of class 'student'
getch();
}
The above statement, when the object was created of class 'student' , it will pass the variable 'a' to the constructor 'student' and will initialize the roll_no variable.
No comments:
Post a Comment