CISC 3110
Advanced Programming Techniques
Lecture #7
A Minimal Rational Module
Programs
- Write a minimal module for rational numbers, and a test driver for the class.
- State:
numerator and denominator
- Functions
print — prints out the numerator and a denominator if not 1.
mult — Multiples two Rational returning the result in a third object.
Concepts
const
- call-by-reference
f(T) vs f(T &) vs f(const T &)
- testing, test drivers, test stubs, test suites
- Initialization issues: validation of initial values, forcing intial values
- What happens if I don't specify an initial value for my
Rational variable?
Rules for Parameter Transmission
- If a value is being passed back via the parameter, it should be passed by-reference (
T &)
- Parameters passed using either of the other two modes — by-value or by-const-reference — will not effect a change; with by-value, all changes are
restricted to the local variable (which sis destroyed upon returning from the function), and for by-const-reference, the compiler will prohibit any changes to
the parameter within the function
- If the parameter is a predefined type (
int, long, double, bool, char, float), the parameter should be passed by=value (T).
- There is no reason to pass by-const-reference since the predefined types are small in size (whether it 'hurts' to pass by-const-reference is a matter of discussion
beyond the scope of our class)
- If the parameter is an object (i.e., a variable whose type is a structure or class), it should be passed by-const-reference
- As a general rule, we're not interested in knowing the internal fields of a class (we aren't at that stage yet, but we will quickly get there) and thus
we don't know its size. Passing large objects can take time, while passing a reference is essentiall the same as passing an integer.