abstract data types adt
play

Abstract Data Types (ADT) and C++ Classes 1-15-2013 Abstract Data - PowerPoint PPT Presentation

Abstract Data Types (ADT) and C++ Classes 1-15-2013 Abstract Data Types (ADT) & UML C++ Class definition & implementation constructors, accessors & modifiers overloading operators friend functions HW#1 posted


  1. Abstract Data Types (ADT) and C++ Classes 1-15-2013

  2.  Abstract Data Types (ADT) & UML  C++ Class definition & implementation  constructors, accessors & modifiers  overloading operators  friend functions HW#1 posted – due: Tuesday, 1/22 Quiz Thursday, 1/24

  3.  FAQ 5.14 What is an abstraction and why is it important? An abstraction is a simplified vew of an object in the user’s own vocabulary. In OO and C++, an abstraction is the simplest interface to an object that provides all the features and services the intended users expect.  FAQ 5.15 Should abstractions be user-centric or developer-centric? User-centric . Focus on the user’s point of view.

  4.  Computes what each employee should be paid for a day of work. Reads a file containing start and stop times for each employee. Then calculates and saves the pay amounts to another file. int runPayCalculator (const char csInputFileName[], const char csOutputFileName[]) Sample input: 510 + 24601 990 - 24601 Sample output 24601 96

  5. int runPayCalculator (const char csInputFileName[], const char csOutputFileName[]) Algorithm: call computeHours and then computeAndWritePay Data structure: The number of hours worked by each employee is stored in an array indexed by the possible employee numbers. This array is of size MAX_EMPLOYEE_NUMBER + 1, where MAX_EMPLOYEE_NUMBER is a global constant

  6. In a well-designed modular program, software components should satisfy the following two properties: 1. Each component performs one well ll-def defined ned task sk . (i.e. “cohesion”) 2. Each component is as indep ndependen ndent as possible from the others. (i.e. loosely coupled”)

  7. 1. Easier to understand; little redundant code. 2. Facilitates software reuse. 3. Easier to implement. 4. Easier to test. 5. Easier to modify.

  8.  Independence of modules is typically achieved by “information hiding” (which can be achieved by “encapsulation”).  Procedural Abstraction  Use of a function depends on its purpose (what it does) but not on its implementation (how it does it).  FAQ 5.18 What’s the value of separating interface from implementation? It’s a key to eliminating the ripple effect when a change is made.

  9.  Class designer/implementer - designs & implements a class vs.  Client programmer - uses a class for an application vs.  End-user - uses the application

  10.  Procedural Abstraction (Algorithm)  Data Abstraction (Data)

  11.  An Abstract stract Data ta Type (ADT) is a specification of a set of data and a set of operations that can be performed on the data. examples: String Circle List Dice Dictionary Song Student Telephone Directory Time Complex number

  12.  In C++, a class represents an ADT.  An “instance” of a class is a specific object which is created, and the data members are filled in with values (possibly default values). Objects are created with a specialized member function called a “constructor”.  An instance of a class is destroyed (recycled) with a specialized member function called a “destructor”

  13. Circle Type name constructor(s): Circle(int,int), Circle(float,int,int); Public float computeArea(); interface float getRadius(); void setRadius(float); // etc.

  14.  An ADT is a contract between  The interface designer and ...  The coder of a class that implements the interface  Prec econ ondition: dition: any assumption/constraint on the method data before the method begins execution  Postcond tcondition: ition: describes result of executing the method

  15.  A C++ program is a collection of functions and classes.  A class represents a set of objects that have common properties.  A class is a template for creating objects.  A class represents a type.  Type determines the set of values an object may have.  Type determines the operations that can be performed on those values.  In C++ there are two kinds of types:  Primitive or build-in types  User Defined or class types

  16.  A class consists of members  Data members – also called data fields or attributes  Member functions – also called operators, functions or methods  Data members are also sometimes called instance variables because each object (instance of a class) contains them.  Data members may be either primitive or class types.

  17. private instance variables: private float radius; private Point center; public methods: constructor(s) accessor methods (get) mutator methods (set) float computeArea() … etc.

  18. Represent a 2D “point” Data: (x , y) coordinates, integer values Methods: create a point with coordinates (0,0) create a point with coordinates (x,y), get the x coordinate of a point, get the y coordinate of a point, draw a point erase a point move a point etc.

  19.  Unified Modeling Language (UML) is a standard diagram notation for describing a class Field signatures : type and name Method signatures : name, argument Class types, result type name Field Instance values of Person

  20. Point Type name constructor(s): Point(int,int), Point(); // default (0,0) Public int getX(); interface int getY(); // etc.

  21. private instance variables: private int xCoordinate private int yCoordinate public methods: constructor(s) accessor methods (get) mutator methods (set) … etc.

  22.  Class members that are declared in the public section of a class definition are accessible to all functions (inside or outside) the class.  Class members that are declared in the private section of a class definition are accessible only to functions that are members of the class.  Generally we want the operators (member functions) visible to the users of the class.  Thus they are declared public.  Generally we want to keep the implementation details (data members) hidden from the users of the class  Thus they are declared private.

  23.  A constructor is a member function that initializes the data members of an object when the object is cr crea eate ted. d. cl class s Point int {  Note the use of public: lic: initializat ialization ion lists Point( nt(int nt i, , int j) : (more re effici cien ent than x( x(i), y( y(j) { } } assignmen nment t statement ments) s) Point() nt() : x(0) 0), y(0) 0) { } } private vate: int x; x; int y; y; }

  24.  A modifier function provides the ability to modify the value of a private data member vo void d setX tX(in int newX wX) ) { x = = newX wX; ; }  An accessor function provides the ability to read the value of a private data member, without changing it (note use of “ const ”) in int ge getX() () co const { return n x; }

  25. class Point { Client programmer can write: public: Point(int i, int j) : Point p1(10,30); x(i), y(j) { } Point p2; int i = p1.getX(); no args Point() : x(0), y(0) { } receiver message (this) int getX() const { return x; } call the method Point::getX() private: class of the int x; receiver int y; }

  26.  A member function definition (implementation) may be included in the class definition.  The compiler can insert the code for the function body where the function is called.  This is known as an inline function.  Use of inline member function is recommended only for the following:  Functions whose body is very small (one or two lines) ● Constructors ● Accessors ● Modifiers

  27. How would you compare two points, p1 and p2.  Define a method to compare their x and y coordinates.p  p1.lessThan(p2)  Overload the operator <  p1 < p2 bool Point: int::operator operator< (const nst Point int& & other) er) const st { { return eturn (x < < other.x er.x) ) || ((x == == other.x er.x) ) && (y < < other. her.y)); )); }

  28. class Point { Client programmer can write: public: Point p1; Point(int i, int j) : cin>>i; cin>>j; x(i), y(j) { } Point p2(i,j); Point() : if (p2 < p1) then cout << “ lol ”; x(0), y(0) { } int getX() const { return x; } bool operator<(const Point& other) const; private: int x; int y; } bool Point::operator< (const Point& other) const { return // you fill in... }

  29. class Point { public: Point p1(10,30); // other methods as before std::ostream& operator<<( cout << p1; std::ostream& os, const Point& p); receiver arg private: // as before message (this) } problem: ostream& operator<<( The receiver is type ostream ostream& os, const Point& p) { solution: os << //... you fill in Make this function a “friend” return os; }

  30. class Point { Point p1 = public: new Point(10,30); // other methods as before friend std::ostream& cout << p1; operator<<( std::ostream& os, solution: const Point& p); Make this function a “friend” private: // as before } Gives permission for this function to have complete ostream& operator<<( access to the data members, ostream& os, even though they are private to the class const Point& p) { os << //... you fill in return os; }

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend