Abstract Data Types CS 247 Module 2 Scott
Abstract Data Types CS 247 Module 2 Scott Chen
Assigned Readings Ø Eckel. Vol. 1 • Chapter 7 – Function overloading & Default Argument • Chapter 12 – Operator overloading • This will help you understand the key concepts used in implementing ADT
Agenda for Module 2 Ø What is ADT? Ø Standard Features of ADT • • • Legal Values Public Accessors and Mutators Function Overloading Default Arguments Operator Overloading Nonmember Functions Friends Type Conversion Private Data Representations Helper Functions Override / Final
Section 1 What is ADT?
What is ADT? Ø Everything is an object in OOD • Non-primitive data types must be implemented as an object • What are the C++ primitive data types? § § Integer Floating Point Numbers Characters Boolean • What are the examples of non-primitive data types? § String § Fraction Numbers § Linked List
What is ADT? Ø Non-primitive data types must be implemented as an object • Define a Abstract Data Type (ADT) Class § An object that can be used just like a primitive data type by the client programmers § Compiler treat ADT (almost) like a primitive data type, thus prevents the client code from compiling if the client uses the ADT incorrectly. § Automate the error check through compiler § Turn semantic code into syntactic error § Requires you (the upstream programmer) to define the full set of behaviors for this ADT
What is ADT? Ø Non-primitive data types must be implemented as an object • Define a Abstract Data Type (ADT) Class § An object that can be used just like a primitive data type by the client § Requires you to define the full set of behaviors for this ADT • Example: § Primitive Data Type – integer – has the following behaviors § int my. Int 1 = 3; int my. Int 2 = 4; my. Int 1 = my. Int 1 + my. Int 2; cout << my. Int 1; § Abstract Data Type – Rational – should also have the following behaviours for processing fraction numbers § Rational my. Rat 1 = Rational(3, 4); //set the initial value of 3/4 Rational my. Rat 2 = Rational(4, 5); //set the initial value of 4/5 my. Rat 1 = my. Rat 1 + my. Rat 2; cout << my. Rat 1;
What is ADT? Ø When is ADT necessary? • Whenever application-specific data structure are needed § Example 1 – Matrix Calculator § Example 2 – Special Dataset in Database § Example 3 – Game environment-specific attributes Ø Benefit of ADT? • • Encapsulate data manipulation procedures Minimize client data manipulation mistakes Turns semantic error into syntactic error (for using the ADT) Simplifies long-term software development
What is ADT? Ø How to create a decent ADT? • Heed the “Standard Features of ADT” § § § § § Legal Values Public Accessors and Mutators Function Overloading Default Arguments Operator Overloading Nonmember Functions and Friends Type Conversion Private Data Representations Helper Functions Override / Final All “Best Design Practices” will be marked with BDP: • Through this module, we will use a Rational ADT (handling fractions) as our example
What is ADT? Ø Through this module, we will use a Rational ADT (handling fractions) as our example • So that by the end of our development cycle we can do this in the client program: #include <iostream> using namespace std; int main () { Rational r, s; //ADT variables created like primitive type cout << "Enter rational number (a/b): "; cin >> r; //ADT variable acquired through cin like primitive type cout << "Enter rational number (a/b): "; cin >> s; //Arithmetic cout << r << cout << return 0; } & " " " logic operations and cout with ADT variables like primitive type + " << s << " = " << r+s << endl; * " << s << " = " << r*s << endl; == " << s << " is " << (r==s) << endl;
Section 2 Standard Features of ADT Part I Legal Values, Public Accessors & Mutators, Function Overloading, Default Arguments
Starting a New ADT Ø Rational ADT • Define Rational class • Define the public interface • Define the private data members § BDP: All data variables should always be defined in private scope § BDP: All private data members should be named with special characters to minimize naming conflict (more to come)
Feature 1 – Legal Values Ø All data types must enforce “Legal Values” • Initialization § BDP: Mandating legal values in the constructor § Method 1 – Set Default Value in the default constructor § Method 2 – Throw Exception in case of illegal values § Method 3 – Enforce Explicit Initialization § Method 4 (later) – Include default input parameters for constructors • Mutator / Setter (Later) § BDP: Mandating legal values when new values are provided via mutator functions (setters)
Feature 2 – Public Accessor and Mutator Ø Accessor (Getter) • Member functions allowing clients to read private values without exposing the private field § BDP: Use clear naming conventions for accessor functions - i. e. get. Denom(), get. Numer() § BDP: All accessor functions should be const whenever possible § BDP: Pass input parameters by const reference whenever possible Ø Mutator (Setter) • Member functions allowing clients to update private values without exposing the private field § BDP: Use clear naming conventions for mutator functions - i. e. set. Denom(), set. Numer() § BDP: Pass input parameters by const reference whenever possible § BDP: Mandating legal values when new values are provided
Feature 3 – Function Overloading Ø Allowing clients to call the same function with different variations in argument signatures • • BDP: Deploy function overloading whenever member functions are semantically related BDP: Constructors are (almost) always overloaded for different initialization purposes Ø Watch Out! • • Overloaded functions MUST have different argument signatures Cannot overload functions with only different return types
Feature 3 a – Default Arguments Ø Alternatively, member functions can also deploy default values to its arguments • BDP: Whenever possible given signature types, combine function variants (without introducing coding limitations) Ø Watch Out! • • • Default arguments MUST be done ONLY in the function declaration / prototyping When client-provided values are less than the parameter count, only the trailing parameters get default values Once one default argument is used in a function call, all subsequent arguments in the call must be defaults
Section 3 Standard Features of ADT Part II Operator Overloading, Friends, Nonmember Functions, Type Conversion
Feature 4 – Operator Overloading (Part I) Ø 4 a. Arithmetic Operators for ADT • • Enabling ADT objects to be used as primitive data types through arithmetic operators (+, -, *, /, etc) Design decisions to consider § § Argument types Return types const Pass-by-value v. s. Pass-by-reference § BDP: Use conventional operator signatures (e. g. operator== returns a bool) Ø Watch Out! • • Cannot create new operations (e. g. operator**) Cannot change the number of arguments
Feature 5 – Nonmember Functions Ø ADT-specific functions residing outside of the class definition • Nonmember functions do not have direct access to private data members • Some functions have to be nonmember (e. g. operator>>) • BDP: Functions designed to read the values in ADT without mutating them should be kept nonmember Ø Critical to ADT design • Enhance data encapsulation and private attribute protection
Feature 4 – Operator Overloading (Part 2) Ø 4 b. IO Stream Operators for ADT • Enabling ADT objects to be used as primitive data types through IO stream operators (<<, >>) § BDP: Must be nonmember function! § BDP: First parameter must be the reference to the stream object § BDP: Return value must be the modified stream to ensure chained stream operation
Feature 6 - Friends Ø Allowing nonmember functions to receive access to the private fields • Easier to track than family • • BDP: Mandating legal values if private fields are to be modified by the friended nonmember functions BDP: Be stringent in assigning friends to nonmember functions. Abusing it will result it OOD meltdown.
Feature 7 – ADT Type Conversion Ø ADT by default permits implicit conversion Ø Watch Out! • The compiler looks for one of the following constructors to perform implicit type conversion: 1. Single-Argument Constructors 2. Multi-Argument Constructors with Default Values • Such implicit type conversion can be prohibited through keyword explicit § Forcing client to explicit type-cast through the designated constructor to ensure strict type match § BDP: Consider prohibiting implicit conversion when compiler / client can misinterpret the conversion outcome from the code
Section 4 Standard Features of ADT Part III Private Data Representations, Helper Functions, Override & Final
Feature 8 – Private Data Representation Ø BDP: All data members should be private, ALWAYS • • Ensure consistent data representation through ADT evolution Deploy accessors and mutators to ensure legal values are maintained at all times Derived classes can manipulate data via public or protected methods Nonmember functions can be granted with access by being friended • BDP: Named private data members with special characters to minimize possibilities of naming conflict
Feature 9 – Helper Functions Ø Common functions used among multiple class-related functions • • BDP: Helper functions should be kept as private methods or within a namespace BDP: Helper functions should be named such that it does not pollute the global namespace
Feature 10 – Function Override and Final Ø BDP: Ask – “Should client programmers derive their own ADTs from mine? ” • Answer: “Yes” § Add keyword virtual to the functions in your ADT – permits child ADT to override the implementation of these functions § Add keyword final to the functions in your ADT – prohibits child ADT to override these functions § The client programmer can use the keyword override to override the base implementations § Question for you: What happens if the child ADT overrides a non-virtual function in the parent ADT? • Answer: “No” § Simply add keyword final to the class definition – prohibiting any form of ADT derivation
Section 5 Summary
Summary of Attribute-based ADT Design Ø Range of legal values • • Default initial value? Construct object from client-provided values? explicit constructors or allow implicit type conversion? Throw exceptions if constructors or mutators is given an illegal value Ø Attributes (virtual data members) • Accessors and mutators - Consistent naming schemes Ø Nonmember functions • • Overloaded functions / Default arguments Overloaded operators Ø Final • Allowing derived classes for clients?
- Slides: 28