// Wyo C++ // Ch. 14 Demo #8 // purpose - to illustrate a template class and a template function #include <iostream.h> template <class Whatever> class Fraction { public: Fraction(); Fraction(const Whatever, const Whatever); void setNum(const Whatever); void setDen(const Whatever); Whatever getNum(); Whatever getDen(); double toDecimal(); void display(); private: Whatever myNum; Whatever myDen; }; template <class Whatever> void displayReciprocal(Fraction <Whatever> myFraction); int main() { Fraction<int> fraction1; Fraction <double> fraction2(2.5, 4.1); fraction1.setNum(3); fraction1.setDen(5); fraction1.display(); cout << endl; fraction2.display(); cout << endl; displayReciprocal(fraction1); cout << endl; return 0; } template <class Whatever> void displayReciprocal(Fraction <Whatever> myFraction) { cout << myFraction.getDen() << '/' << myFraction.getNum() << endl; } template <class Whatever> Fraction<Whatever>::Fraction():myNum(0), myDen(0) {} template <class Whatever> Fraction<Whatever>::Fraction(const Whatever num, const Whatever den) { myNum = num; myDen = den; } template <class Whatever> void Fraction<Whatever>::setNum(const Whatever num) { myNum = num; } template <class Whatever> void Fraction<Whatever>::setDen(const Whatever den) { myDen = den; } template <class Whatever> Whatever Fraction<Whatever>::getNum() { return myNum; } template <class Whatever> Whatever Fraction<Whatever>::getDen() { return myDen; } template <class Whatever> <Whatever> Fraction<Whatever>::toDecimal() { return myNum / myDen; } template <class Whatever> void Fraction<Whatever>::display() { cout << myNum << '/' << myDen; }