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

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)

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

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

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

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

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
};

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);

#### You've reached the end of your free preview.

Want to read all 49 pages?

- Winter '03
- Pro.Lei
- Object-Oriented Programming, C++ Programming, Rational class, Rational member, Rational Overview