Test the automatic conversion that occurs when a constructor is not modified by explicit: - C++ Class

C++ examples for Class:Constructor

Description

Test the automatic conversion that occurs when a constructor is not modified by explicit:

Demo Code

#include <iostream>
using namespace std;
class myclass {/*w w  w.  jav  a 2 s .  co m*/
   int val;
   public:
   // The following constructor is NOT explicit.
   myclass(int x) { val = x; }
   int getval() { return val; }
};
int main()
{
   myclass ob(4); // OK
   cout << "val in ob: " << ob.getval() << endl;
   // the implicit conversion from int to myclass.
   myclass ob2 = 19;
   cout << "val in ob2: " << ob2.getval() << endl;
   return 0;
}

Result


Related Tutorials