the background: reading wonderful book, briefly mentioned conversion constructors.
though understand fundamentals of conversion constructors, bit lost, when in practice problems section asked:
write conversion constractor converts ratnum
realnum
code received prior problem:
class ratnum { int num, denom; public: ratnum(int numenator = 2, int denominator = 3) : num(numenator), denom(denominator) {} void show() { cout << static_cast<double>(num) / denom << endl; } double result() { return num / denom; } }; class realnum { int number; public: realnum(double numr = 0) : number (numr) {} };
i dealt conversion constructors such:
ratnum 1 = 10; //or ratnum 2 = {10, 2}
but never saw can convert class class type. you, please, how such conversion constructor should work (maybe example)?
is defining function result
of ratnum
class , writing:
realnum check = one.result();
is meant here?
i assume meant there overload of constructor takes input of other class, example
realnum(ratnum const& other) : number{other.result()} {}
and vice-versa construct ratnum
realnum
, although less trivial there irrational numbers, infinitely repeating numbers, etc.
you use version of constructor like
ratnum a{2, 4}; realnum b{a}; // used "conversion" constructor
Comments
Post a Comment