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 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 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 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 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 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 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 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 Rational Attributes A numerator and denominator Implies in part a class representation with two private int data members NumeratorValue and DenominatorValue Rational Public Behaviors Rational arithmetic Addition, subtraction, multiplication, and division Rational relational Equality and less than comparisons Practice rule of class minimality 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 Non-Public Behaviors Inspection and mutation of data members Clients deal with a Rational object! 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?  #### You've reached the end of your free preview.

Want to read all 49 pages?

• • •  