Coupon Accepted Successfully!


The Keyword explicit

The keyword explicit is advantageous in order to declare the constructors of a class as being explicit. When a constructor is invoked with a single argument, implicit conversion is carried out. In this operation, automatic conversion is performed. The type of argument taken is modified to an object type. To avoid such automatic-type conversion, the keyword explicit is used followed by the constructor name. The above program explains the use of the keyword explicit.


21.11 Write a program to declare explicit constructor and avoid automatic conversion.


class XYZ


int k;


explicit XYZ (int j)


void show()


cout<<“\n k=”<<k;



int main()


XYZ XY(545);

// XYZ XY=450; invalid assignment


return 0;





The Keyword mutable



Explanation: In the above program, the class XYZ is declared with a one-integer argument. The class also has a one-argument constructor followed by the keyword explicit and one member function show(). In the function main(), the statement XYZ XY (545) creates an object XY and initializes the member variable k with the number 545. The statement XYZ XY = 450 is invalid, because the constructor is explicit and does not permit automatic conversion. This statement is valid only when the class constructor is not declared as being explicit.

Test Your Skills Now!
Take a Quiz now
Reviewer Name