PowerPoint PresentationAbstract Data Type ExampleDefining an ADTADT OperationsFraction ADT SpecificationFraction ADT (Cont)Fraction ADT (cont)Fraction ADT OperationsFraction ADT I/ODesignFraction ADT Design: DataFraction ADT VariablesFraction ADT AlgorithmsSlide 14Fraction ADT Design IssuesADT SpecificationAccessorsModifiersOperationsInput/OutputAllowing for multiple #includesSample Client - DeclarationsSample Client – Creating FractionsSample Client – Manipulating FractionsSample Client – Displaying OutputSample Client – Clean UpSample ExecutionAcknowledgementsDale RobertsDepartment of Computer and Information Science,Department of Computer and Information Science,School of Science, IUPUISchool of Science, IUPUICSCI 265Fraction Abstract Data TypeFraction Abstract Data TypeDale Roberts, LecturerComputer Science, IUPUIE-mail: [email protected] RobertsAbstract Data Type ExampleAbstract Data Type ExampleAn An Abstract Data TypeAbstract Data Type is a data type that is is a data type that is defined by the programmer, not the language.defined by the programmer, not the language.Like any data type, it is a set of values with Like any data type, it is a set of values with operations that act on those values.operations that act on those values.Working with ADTs involves three different Working with ADTs involves three different components:components:1.1.The The public interfacepublic interface, or specification, defines , or specification, defines the ADT and how it is used.the ADT and how it is used.2.2.The The implementationimplementation, implements the ADT in , implements the ADT in code.code.3.3.The The clientclient uses the ADT to perform a task. uses the ADT to perform a task.Dale RobertsDefining an ADTDefining an ADTWithin C++, you define an ADTs public interface Within C++, you define an ADTs public interface with a header file.with a header file.#include “fraction.h”#include “fraction.h”main()main(){{ Fraction x, y; Fraction x, y; … …}}User-defined ADTs are typically User-defined ADTs are typically capitalized to distinguish them from capitalized to distinguish them from intrinsic data types.intrinsic data types.Dale RobertsADT OperationsADT OperationsSeveral classes of operations are required in order to Several classes of operations are required in order to effectively used ADT. effectively used ADT. 1.1.Constructors – used to create instances of the data Constructors – used to create instances of the data type.type.2.2.Destructors – used to destroy an instance of the data Destructors – used to destroy an instance of the data typetype3.3.Accessors – used to access attributes of the data type. Accessors – used to access attributes of the data type. (Such as “get” functions)(Such as “get” functions)4.4.Modifiers – used to modify attributes of the data type. Modifiers – used to modify attributes of the data type. (Such as “set” functions)(Such as “set” functions)Object-oriented languages provide some of this functionality within the Object-oriented languages provide some of this functionality within the language. In C you must do it yourself.language. In C you must do it yourself.Dale RobertsFraction ADT SpecificationFraction ADT SpecificationThe specification for the Fraction ADT requires a The specification for the Fraction ADT requires a discussion of what operations are required.discussion of what operations are required.Constructors:Constructors: Fraction() creates a new variable of type Fraction Fraction() creates a new variable of type Fraction with the default value of 0/1.with the default value of 0/1. Fraction(int n, int d) creates a new variable of Fraction(int n, int d) creates a new variable of type Fraction with the default value of n/d.type Fraction with the default value of n/d.Note that you need to handle all combinations of Note that you need to handle all combinations of positive and negative for n and d in order to positive and negative for n and d in order to determine the sign of the fraction.determine the sign of the fraction.Dale RobertsFraction ADT (Cont)Fraction ADT (Cont)Destructors:Destructors: ~fraction(f) – frees Fraction variable f.~fraction(f) – frees Fraction variable f.Copy:Copy: Fraction g = f; – creates a new Fraction g, whose values Fraction g = f; – creates a new Fraction g, whose values are copied from f. C++ provides a default copy are copied from f. C++ provides a default copy constructor, or you may write your own: Fraction(const constructor, or you may write your own: Fraction(const Fraction &)Fraction &)Accessors:Accessors: i = f.getNumerator();i = f.getNumerator(); i = f.getDenominator();i = f.getDenominator(); c = f.getSign();c = f.getSign();You don’t need to provide you own copy constructor if you don’t use new.Dale RobertsFraction ADT (cont)Fraction ADT (cont)Modifiers:Modifiers: f.reduceFract() – reduces a fraction to lowest f.reduceFract() – reduces a fraction to lowest terms. Notice that this is a modifier, and cannot terms. Notice that this is a modifier, and cannot be used in an expression.be used in an expression.setNumerator(int);setNumerator(int);setDenominator(int);setDenominator(int);setSign(char);setSign(char);Dale RobertsFraction ADT OperationsFraction ADT OperationsThese Operations are designed to be used in an These Operations are designed to be used in an expression. They never modify their arguments. They expression. They never modify their arguments. They always return type Fraction.always return type Fraction.f = Fraction::negateFract(f) // returns -ff = Fraction::negateFract(f) // returns -fs = Fraction::addFract(f1,f2) // returns f1 + f2s = Fraction::addFract(f1,f2) // returns f1 + f2d = Fraction::subFract(f1,f2) // returns f1 – f2d = Fraction::subFract(f1,f2) // returns f1 – f2p = Fraction::mulFract(f1,f2) // returns f1 * f2p = Fraction::mulFract(f1,f2) // returns f1 * f2q = Fraction::divFract(f1,f2) // returns f1 / f2q = Fraction::divFract(f1,f2) // returns f1 / f2i = Fraction::compareFract(f1,f2) i = Fraction::compareFract(f1,f2) // f1 < f2 returns -1, // f1 < f2 returns -1, // f1 = f2 returns 0, // f1 = f2 returns 0, // f1 > f2 returns 1// f1 > f2 returns 1Dale RobertsFraction ADT I/OFraction ADT I/OInput/Output:Input/Output:Fraction Fraction::getFract(istream, f) – return 1 if Fraction Fraction::getFract(istream, f) – return 1 if OK, 0 if invalid, EOF if end of file.OK, 0 if invalid, EOF if
View Full Document