Abstract Data Types Lecture 23 Section 7.1 Robb T. Koether Hampden-Sydney College Wed, Oct 24, 2012 Robb T. Koether (Hampden-Sydney College) Abstract Data Types Wed, Oct 24, 2012 1 / 19
1 Abstract Data Types 2 The Rectangle Class The Member Functions The Data Members 3 Assignment Robb T. Koether (Hampden-Sydney College) Abstract Data Types Wed, Oct 24, 2012 2 / 19
Outline 1 Abstract Data Types 2 The Rectangle Class The Member Functions The Data Members 3 Assignment Robb T. Koether (Hampden-Sydney College) Abstract Data Types Wed, Oct 24, 2012 3 / 19
Abstract Data Types An abstract data type (ADT) is a data type for which only its interface is described. The ADT is described through the prototypes of its public member functions. The ADT does not include the implementation details. The client programmer does not need to concern himself with the implementation details. Robb T. Koether (Hampden-Sydney College) Abstract Data Types Wed, Oct 24, 2012 4 / 19
Example of an ADT Consider a Rectangle class. The client programmer is concerned only with manipulating rectangles. He is not concerned with how they are represented internally. However, it is important that the results of his manipulations conform to his expectations, based on his abstract notion of a rectangle. Robb T. Koether (Hampden-Sydney College) Abstract Data Types Wed, Oct 24, 2012 5 / 19
Outline 1 Abstract Data Types 2 The Rectangle Class The Member Functions The Data Members 3 Assignment Robb T. Koether (Hampden-Sydney College) Abstract Data Types Wed, Oct 24, 2012 6 / 19
Outline 1 Abstract Data Types 2 The Rectangle Class The Member Functions The Data Members 3 Assignment Robb T. Koether (Hampden-Sydney College) Abstract Data Types Wed, Oct 24, 2012 7 / 19
A Rectangle Class The client uses only the public member functions. Create a rectangle. Destroy a rectangle. Copy a rectangle. Draw a rectangle. Move a rectangle to a new location. Change the length of a rectangle. Find the area of a rectangle. And so on. Robb T. Koether (Hampden-Sydney College) Abstract Data Types Wed, Oct 24, 2012 8 / 19
Rectangle Interface Rectangle Constructors // Constructors Rectangle(); Rectangle(const Rectangle& r); Rectangle(double len, double wid); Rectangle(double len, double wid, const Point& cen); The above is a possible interface for the Rectangle class. The class continues on the following slides. Robb T. Koether (Hampden-Sydney College) Abstract Data Types Wed, Oct 24, 2012 9 / 19
Rectangle Interface Rectangle Inspectors and Mutators // Inspectors double getlength() const; double getwidth() const; Point getcenter() const; // Mutators void setlength(double len); void setwidth(double wid); void setcenter(const Point& cen); Robb T. Koether (Hampden-Sydney College) Abstract Data Types Wed, Oct 24, 2012 10 / 19
Rectangle Interface Rectangle Facilitators // Facilitators void input(istream& in); void output(ostream& out) const; // Operators istream& operator>>(istream& in, Rectangle& r); ostream& operator<<(ostream& out, const Rectangle& r); Robb T. Koether (Hampden-Sydney College) Abstract Data Types Wed, Oct 24, 2012 11 / 19
Rectangle Interface Other Rectangle Functions // Other member functions void draw(ostream& out) const; double perimeter() const; double area() const; Robb T. Koether (Hampden-Sydney College) Abstract Data Types Wed, Oct 24, 2012 12 / 19
Outline 1 Abstract Data Types 2 The Rectangle Class The Member Functions The Data Members 3 Assignment Robb T. Koether (Hampden-Sydney College) Abstract Data Types Wed, Oct 24, 2012 13 / 19
Rectangle Class Implementation Rectangle Data Members // Data members private: double left; // Left x double right; // Right x double top; // Top y double bottom; // Bottom y The implementation of the Rectangle class refers to the choice of data members and how they are used to implement the member functions. The above is a possible implementation. Robb T. Koether (Hampden-Sydney College) Abstract Data Types Wed, Oct 24, 2012 14 / 19
Rectangle Class Implementation Rectangle Data Members // Data members private: double left; double bottom; double length; double width; // Left x // Bottom y // Length // Width Another possible implementation is the above. Robb T. Koether (Hampden-Sydney College) Abstract Data Types Wed, Oct 24, 2012 15 / 19
Rectangle Class Implementation Rectangle Data Members // Data members private: Point bottomleft; Point topright; // Bottom left corner // Top right corner Yet another possible implementation. Robb T. Koether (Hampden-Sydney College) Abstract Data Types Wed, Oct 24, 2012 16 / 19
Rectangle Class Implementation The client would not care which implementation was used because he would not be able to tell the difference. Robb T. Koether (Hampden-Sydney College) Abstract Data Types Wed, Oct 24, 2012 17 / 19
Outline 1 Abstract Data Types 2 The Rectangle Class The Member Functions The Data Members 3 Assignment Robb T. Koether (Hampden-Sydney College) Abstract Data Types Wed, Oct 24, 2012 18 / 19
Assignment Assignment Read Section 7.1. Robb T. Koether (Hampden-Sydney College) Abstract Data Types Wed, Oct 24, 2012 19 / 19