chap08 - Chapter 8 Chapter Implementing abstract data types Key Concepts Key data abstraction abstract data type(ADT rule of minimality class minimality

chap08 - Chapter 8 Chapter Implementing abstract data types...

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

Chapter 8 : Chapter 8 : Implementing abstract data types
Image of page 1
Key Concepts: Key Concepts: data abstraction abstract data type (ADT) rule of minimality class minimality principle default constructor copy constructor member assignment inspectors mutators facilitators destructor const member functions auxiliary functions operator overloading overloading “<<” and “>>” reference return pseudorandom number
Image of page 2
Our Goal Well-defined representations that allow objects to be created and used in an intuitive manner User should not have to bother with unnecessary details Example programming a microwave to make popcorn should not require a physics course
Image of page 3
Golden Rule Use information hiding and encapsulation to support integrity of data Put implementation details in a separate module Implementation details complicate the class declarations Data members are private so that use of the interface is required Makes clients generally immune to implementation changes
Image of page 4
Another Golden Rule Keep it simple – class minimality rule Implement a behavior as a nonmember function when possible Only add a behavior if it is necessary
Image of page 5
Abstract Data Type Well-defined and complete data abstraction using the information-hiding principle ADT allows its objects to be created and used in an intuitive manner Not all programmer-defined types are ADT
Image of page 6
Rational Number Review Rational number Ratio of two integers: a/b Numerator over the denominator Standard operations Basic arithmetic operations bd ac d c b a = × Multiplication bd bc ad d c b a + = + Addition bd bc ad d c b a = Subtraction bc ad d c b a = ÷ Division
Image of page 7
ADT: Rational Number Consider Rational a(1,2); // a = 1/2 Rational b(2,3); // b = 2/3 cout << a << " + " << b << " = " << a + b; Rational s; // s = 0/1 Rational t; // t = 0/1 cin >> s >> t; cout << s << " * " << t << " = " << s * t; Observation Natural look that is analogous to fundamental-type arithmetic objects
Image of page 8
Rational Attributes A numerator and denominator Implies in part a class representation with two private int data members NumeratorValue and DenominatorValue
Image of page 9
Rational Public Behaviors Rational arithmetic Addition, subtraction, multiplication, and division Rational relational Equality and less than comparisons Practice rule of class minimality
Image of page 10
Rational Public Behaviors Construction Default construction Initialize rational number to 0/1 by default Specific construction Allow client to specify numerator and denominator Copy construction Provided automatically Assignment operation Provided automatically Insertion and extraction
Image of page 11
Non-Public Behaviors Inspection and mutation of data members Clients deal with a Rational object!
Image of page 12
Auxiliary Behaviors Operations (necessarily public) Member functions for arithmetic, relational, insertion, and extraction operations Class definition provides a functional form that auxiliary operators use Auxiliary operators for arithmetic, relational, insertion, and extraction operations Provides the natural form we expect Provides commutativity consistency For C++ reasons ( 1 + r ) and ( r + 1 ) would not be treated the same if addition was a member operation Why use auxiliary operators, not member operators?
Image of page 13
Image of page 14

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