class Rational Rational NumeratorValue DenominatorValue Public Add Subtract

Class rational rational numeratorvalue

This preview shows page 14 - 22 out of 49 pages.

class Rational Rational NumeratorValue : DenominatorValue : Public: Add() Subtract() Multiply() Divide() Equal() LessThan() Insert() Extract() Protected: GetNumerator() GetDenominator() SetNumerator() SetDenominator() Object: a NumeratorValue : 1 DenominatorValue : 2 Object: b NumeratorValue : 2 DenominatorValue : 3 1/2 2/3
Image of page 14
Library Components Rational.h Class definitions and library function prototypes Rational.cpp Implementation source code – member and auxiliary function definitions Auxiliary functions are assisting global functions that provide expected but non-member capabilities Rational.obj Translated version of Rational.cpp (linkable) Rational.lib Library version of Rational.obj that is more readily linkable Rational.dll Executable file of rational library for dynamic linked library (DLL)
Image of page 15
A program using Rational – MyProgram.cpp #include <iostream> using namespace std; #include "rational.h" int main() { Rational r; Rational s; cout << "Enter two rationals(a/b): "; cin >> r >> s; Rational Sum = r + s; cout << r << " + " << s << " = " << Sum; return 0; } Making use of the Rational class. The header file provides access to the class definition and to auxiliary function prototypes. The header file does not provide member and auxiliary definitions
Image of page 16
Producing MyProgram.exe Preprocessor combines the definitions and prototypes in iostream and rational headers along with MyProgram.cpp to produce a compilation unit Compiler must be told where to look for Rational.h Compiler translates the unit and produces MyProgram.obj Compiler recognizes that MyProgram.obj does not contain actual definitions of Rational constructor, +, >>, and << Linker is used to combine definitions from the Rational library file with MyProgram.obj to produce MyProgram.exe Compiler must be told where to find the Rational library file
Image of page 17
Producing MyProgram.exe MyProgram.cpp PROCESS preprocessor directives to produce a translation unit CHECK translation unit for legal syntax and compile it into object file MyProgram.obj LINK object file with standard library files and rational library file to produce executable unit MyProgram.exe
Image of page 18
Rational Header File Overview File layout Class definition and library prototypes nested within preprocessor statements Ensures one inclusion per translation unit Class definition precedes library prototypes #ifndef RATIONAL_H #define RATIONAL_H class Rational { // … }; // library prototypes … #endif //RATIONAL_H
Image of page 19
Class Rational Overview // from rational.h class Rational { public : // for everybody including clients protected : // for Rational member functions and for // member functions from classes derived // from rational private : // for Rational member functions };
Image of page 20
Rational Public Section public : // default constructor Rational(); // specific constructor Rational(int numer, int denom = 1); // arithmetic facilitators Rational Add( const Rational &r) const ; Rational Subtract( const Rational &r) const ; Rational Multiply( const Rational &r) const ; Rational Divide( const Rational &r) const ; // relational facilitators bool Equal( const Rational &r) const ; bool LessThan( const Rational &r) const ; // stream facilitators void Insert(ostream &sout) const ; void Extract(istream &sin);
Image of page 21
Image of page 22

You've reached the end of your free preview.

Want to read all 49 pages?

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes