Question: Write a conversion operator to allow automatic conversion of EvenNumbers to ints. Hint: Like a constructor, the return type is implicit - do not specify
Write a conversion operator to allow automatic conversion of EvenNumbers to ints. Hint: Like a constructor, the return type is implicit - do not specify one.
#include
class EvenNumber { public: EvenNumber(int n) { value = (n % 2 == 0) ? n : n - 1; //must be even }
operator int();
int getValue() const { return value; } private: int value; };
//Do not modify anything on or above the line below this //YOUR_CODE_BELOW
//YOUR_CODE
//YOUR_CODE_ABOVE //Do not modify anything on or below the line above this
int main() { EvenNumber n1(6);
int n = n1;
cout << n << endl;
return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
