home *** CD-ROM | disk | FTP | other *** search
- // \EXAMPLES\EX0804.CPP
- // main program to demonstrate members that are objects
- //---------------------------------------------------------
- // files in this example:
- // %F,15,EX0804.H%EX0804.H definition of Fraction class
- // EX0804.CPP main program
- //---------------------------------------------------------
- #include <iostream.h> // stream I/O
- #include <process.h> // for abort()
-
- #include "EX0804.H" // def'n of Fraction class
-
- //---------------------------------------------------------
- // the Pfracton class has a member that is a fraction
- //---------------------------------------------------------
- class Pfraction{
- int whole;
- Fraction part;
- public:
- Pfraction(int n, int d);
- ~Pfraction() {};
- friend void print(const Pfraction& pf);
- };
-
- //---------------------------------------------------------
- // constructor with initializer list
- // this initializer list assumes d != 0
- //---------------------------------------------------------
- Pfraction::Pfraction(int n=0, int d=1) : whole(n/d), part(n%d, d)
- { if ( whole < 0 ) part = - part;
- }
-
- //---------------------------------------------------------
- //---------------------------------------------------------
- void main()
- { // user input separate numerator and denominator
- int n, d;
- cout << "Enter int numerator and denominator: ";
- cin >> n >> d;
- if ( d == 0 )
- { cout << " 0 denominator not allowed: ";
- abort();
- }
- // construct and print the proper fraction
- Pfraction k(n,d);
- cout << endl << "The proper fraction is: ";
- print(k);
- cout << endl;
- }
-
- //---------------------------------------------------------
- // friend of the Pfraction class
- //---------------------------------------------------------
-
- void print (const Pfraction& pf)
- { cout << pf.whole << '_';
- print(pf.part);
- }
-
- void print (const Fraction& f)
- { cout << f.num << '/' << f.denom;
- }
- //---------------------------------------------------------
-