Title: Programming
1Programming
2Review class
- Name one reason why we need the class construct.
- Why do we need "inspector functions" to read the
values and "mutator functions" to modify values?
Abstract (user-defined) data types
Private data members
3Review class
- Is there a way to avoid all these hassles, for
instance - void main()
- Temperature temp
- cout ltlt temp.degree ltlt ' ' ltlt temp.scale ltlt
endl // no inspectors - temp.scale 10 // no mutators
- temp.degree 'c'
-
- If yes to above, why we need all the hassles of
inspector and mutator functions?
Public data members!
Information hiding
4Abstract Data Type
- An Abstract Data Type is a class with some
special restrictions. - These restrictions can make programming easier.
- One of these restrictions is called information
hiding, and it helps avoid common problems (e.g.,
a denominator of zero should not be allowed in
rational numbers). - In information hiding, the user should not be
allowed to access the data members directly (they
should be private). - An Abstract Data Type is used in Object-Oriented
Programming (COMP151).
5Rational Review
- Rational number
- Ratio of two integers a/b
- Numerator over the denominator
- Standard operations
- Addition
- Subtraction
6Rational Representation
- Represent a numerator and denominator
with two int data members - NumeratorValue and DenominatorValue
- Data members private (information hiding)
- Public arithmetic member functions
- Rational addition, subtraction, multiplication
and division - Public relational member functions
- Equality and less than comparisons
7Rational Representation
- Public functions
- Construction
- Default construction
- Rational r
- Specific numerator and denominator construction
- Rational r(3, 4)
- Copy construction (provided automatically)
- Rational r(t) Rational r t
- Assignment (provided automatically)
- r t
- Inputting and displaying object
- Private functions
- Inspecting and mutating (changing) data members
- Users should not inspect or change num/denom
directly
8Class Rational
Public interface Add(), Subtract(),
Multiply(),Divide(), Equal(),
LessThan(), Display(), Get()
Data members NumeratorValue,
DenominatorValue
Private Numerator(), Denominator(),
SetNumerator(), SetDenominator(),
Rational a(1,2)
Rational b(2,3)
b Values
a Values
NumeratorValue(1)
NumeratorValue(2)
DenominatorValue(2)
DenominatorValue(3)
9main()
- void main()
- Rational r
- Rational s
- cout ltlt "Enter two rationals(a/b) "
- r.Get()
- s.Get()
- Rational t(r) // t r
- Rational sum r.Add(s) // sum r s
- Rational product r.Multiply(s) // product r
s - r.Display()
- cout ltlt " "
- s.Display()
- cout ltlt " "
- sum.Display() cout ltlt endl
- r.Display()
- cout ltlt " "
- s.Display()
10Rational Overview
- class Rational
- public
- // for everybody (e.g. main())
- private
- // for Rational member functions only
-
11Rational Public
- public
- // default constructor
- Rational()
- // explicit-value constructor
- Rational(int numer, int denom 1)
- // arithmetic facilitators
- Rational Add(const Rational r) const
- Rational Multiply(const Rational r) const
- // i/o facilitators
- void Display() const
- void Get()
12Rational Private
- private
- // inspectors
- int Numerator() const
- int Denominator() const
- // mutators
- void SetNumerator(int numer)
- void SetDenominator(int denom)
- // data members
- int NumeratorValue
- int DenominatorValue
13const
- const Rational OneHalf(1,2)
- OneHalf.Display() // no problem
- OneHalf.Get() // illegal OneHalf is a const
14Default Constructor
- // default constructor
- RationalRational()
- SetNumerator(0)
- SetDenominator(1)
-
- Example
- Rational r // r 0/1
15Specific Constructor
- // specific constructor
- RationalRational(int numer, int denom)
- SetNumerator(numer)
- SetDenominator(denom)
-
- Example
- Rational t(2,3) // t 2/3
-
16Inspectors
- int RationalNumerator() const
- return NumeratorValue
-
- int RationalDenominator() const
- return DenominatorValue
-
- Where is the following legal?
- int a Numerator()
- Answer In a member function only, because
Numerator() is private.
Why the const?
17Mutators
- void RationalSetNumerator(int numer)
- NumeratorValue numer
-
- Where is the following legal?
- SetNumerator(1)
- Answer In a member function only, because
SetNumerator() is private.
Why no const?
18Mutators
- void RationalSetDenominator(int denom)
- if (denom ! 0)
- DenominatorValue denom
- else
- cout ltlt "Illegal denominator " ltlt denom
- ltlt "using 1" ltlt endl
- DenominatorValue 1
-
-
- Example
- SetDenominator(5)
19Facilitators
- Rational RationalAdd(const Rational r) const
- int a Numerator()
- int b Denominator()
- int c r.Numerator()
- int d r.Denominator()
- Rational result(ad bc, bd)
- return result
-
- Example
- Rational r t.Add(u)
20Facilitators
- Rational RationalMultiply(const Rational r)
const - int a Numerator()
- int b Denominator()
- int c r.Numerator()
- int d r.Denominator()
- Rational result(ac, bd)
- return result
-
- Example
- Rational r t.Multiply(u)
21I/O Facilitators
- void RationalDisplay() const
- cout ltlt Numerator() ltlt '/' ltlt Denominator()
-
- Example
- t.Display()
22I/O Facilitators
- void RationalGet()
- int numer
- int denom
- char slash
- cin gtgt numer gtgt slash gtgt denom
- SetNumerator(numer)
- SetDenominator(denom)
-
- Example
- t.Get()