Programming - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

Programming

Description:

In information hiding, the user should not be allowed to access the data members ... Data members private (information hiding) Public arithmetic member functions ... – PowerPoint PPT presentation

Number of Views:18
Avg rating:3.0/5.0
Slides: 23
Provided by: andrew184
Category:

less

Transcript and Presenter's Notes

Title: Programming


1
Programming
  • Abstract Data Types

2
Review 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
3
Review 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
4
Abstract 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).

5
Rational Review
  • Multiplication
  • Division
  • Rational number
  • Ratio of two integers a/b
  • Numerator over the denominator
  • Standard operations
  • Addition
  • Subtraction

6
Rational 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

7
Rational 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

8
Class 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)
9
main()
  • 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()

10
Rational Overview
  • class Rational
  • public
  • // for everybody (e.g. main())
  • private
  • // for Rational member functions only

11
Rational 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()

12
Rational Private
  • private
  • // inspectors
  • int Numerator() const
  • int Denominator() const
  • // mutators
  • void SetNumerator(int numer)
  • void SetDenominator(int denom)
  • // data members
  • int NumeratorValue
  • int DenominatorValue

13
const
  • const Rational OneHalf(1,2)
  • OneHalf.Display() // no problem
  • OneHalf.Get() // illegal OneHalf is a const

14
Default Constructor
  • // default constructor
  • RationalRational()
  • SetNumerator(0)
  • SetDenominator(1)
  • Example
  • Rational r // r 0/1

15
Specific Constructor
  • // specific constructor
  • RationalRational(int numer, int denom)
  • SetNumerator(numer)
  • SetDenominator(denom)
  • Example
  • Rational t(2,3) // t 2/3

16
Inspectors
  • 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?
17
Mutators
  • 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?
18
Mutators
  • 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)

19
Facilitators
  • 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)

20
Facilitators
  • 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)

21
I/O Facilitators
  • void RationalDisplay() const
  • cout ltlt Numerator() ltlt '/' ltlt Denominator()
  • Example
  • t.Display()

22
I/O Facilitators
  • void RationalGet()
  • int numer
  • int denom
  • char slash
  • cin gtgt numer gtgt slash gtgt denom
  • SetNumerator(numer)
  • SetDenominator(denom)
  • Example
  • t.Get()
Write a Comment
User Comments (0)
About PowerShow.com