



















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
These are the Lecture Slides of Advanced Programming and its key important points are: Abstract Data Type, Fraction Example, Public Interface, Implementation, Header File, Adt Operations, Classes of Operations, Constructors, Accessors, Destructors, Modifiers, Fraction Variable
Typology: Slides
1 / 27
This page cannot be seen from the preview
Don't miss anything!
Object-oriented languages provide some of this functionality within the language. In C you must do it yourself.
These Operations are designed to be used in an expression. They never modify their arguments. They always return type Fraction.
f = Fraction::negateFract(f) // returns -f s = Fraction::addFract(f1,f2) // returns f1 + f d = Fraction::subFract(f1,f2) // returns f1 – f p = Fraction::mulFract(f1,f2) // returns f1 * f q = Fraction::divFract(f1,f2) // returns f1 / f i = Fraction::compareFract(f1,f2) // f1 < f2 returns -1, // f1 = f2 returns 0, // f1 > f2 returns 1
that have some internal representation. The representation shall be shared among all the operations. Each operation is responsible for maintaining your design rules regarding the representation of Fraction information.
typedef enum{ POS, NEG } SignType;
class Fraction { private: // data members int numerator; /* numerator and denominator are declared as / int denominator; / int to simplify the algorithms, but they / SignType sign; / will always be stored >= 0 */ public: // member functions would follow private: // utility functions would follow };
All operations must preserve these rules.
a c ad + bc + = b d bd
Sum: (^) then, reduce
a c ac * = b d bd
Product: (^) then, reduce
Subtraction involves adding the opposite, and Division involves multiplying by the reciprocal.
Euclid’s Algorithm: if x < y, then swap x and y While y is not zero remainder = x mod y x = y y = remainder
When you’re done, x is the GCD.
/******** Constructors, Destructors, and Clone **********/ Fraction Fraction(); / returns 0/1 / Fraction Fraction( int n, int d ); / returns n/d / // void ~Fraction( Fraction f ); Implementation not needed // Fraction(const Fraction &); Implementation not needed
static Fraction negateFract( const Fraction &f1); static Fraction addFract( const Fraction &f1, const Fraction &f2 ); static Fraction subFract( const Fraction &f1, const Fraction &f2 ); static Fraction mulFract( const Fraction &f1, const Fraction &f2 ); static Fraction divFract( const Fraction &f1, const Fraction &f2 ); static int compareFract( const Fraction &f1, const Fraction &f2 ); /* returns -1 if f1 < f 0 if f1 == f +1 if f1 > f */
static int getFract(
istream &infile, Fraction &f);
/* returns 1 if a valid fraction is read
0 if an invalid fraction is read EOF if end of file is detected
*/
static void putFract(
ostream &outfile, const Fraction &f );