1 / 14

Comp 245 Data Structures

Comp 245 Data Structures. (A)bstract (D)ata (T)ypes ADT. What is an ADT?. Data abstraction is thinking in terms of WHAT you can do with a collection of data independently of HOW you store and manipulate the data. It is a collection of data together with a set of operations on that data.

gada
Download Presentation

Comp 245 Data Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Comp 245Data Structures (A)bstract (D)ata (T)ypes ADT

  2. What is an ADT? • Data abstraction is thinking in terms of WHAT you can do with a collection of data independently of HOW you store and manipulate the data. • It is a collection of data together with a set of operations on that data. • We will define ADT’s using the C++ class construct • Applications will use an ADT in their code by creating an (ADT) object from the C++ class.

  3. ADT Example – A Sphere • WHATfunctionality do we want a sphere to have? • Find out it’s radius • Set it’s radius • Compute it’s diameter • Compute it’s circumference • Compute it’s area • Compute it’s volume • Display all of it’s statistics (area, volume, etc…)

  4. ADT Example – A Sphere • WHATdata is needed to be able to provide the functionality we want? • A radius

  5. Implementing the Sphere ADTStep 1 – Defining the ADT • Define the ADT by using a C++ class • The class will contain the prototypes for the functions (methods) needed by the ADT. This is what provides the functionality we want. • The class will also contain the variable declarations needed for the data we want to be able to provide the functionality. • This class definition will placed in a header (.h) file. Programs desiring to use this ADT will simply just include (#include) this in their program.

  6. Class Definition for a Sphere ADTUsing a C++ Class class sphereClass { public: sphereClass (double R); sphereClass (); ~sphereClass (); double Radius (); void SetRadius (double R); double Diameter (); double Circumference (); double Area (); double Volume (); void DisplayStatistics (); private: double TheRadius; };

  7. Implementing the Sphere ADTStep 2 – Implementing the ADT • You will create a C++ (.cpp) file which will contain the code for all the functions which you prototyped in the class definition. • Pay special attention how you write the function header for each; this will specify that these functions can only be used with objects of this particular type.

  8. Implementation of a SphereConstructors/Destructor #include "sphere.h" #include <iostream.h> const double PI = 3.14; sphereClass::sphereClass(double R):TheRadius(R) {} sphereClass::sphereClass():TheRadius(1.0) {} sphereClass::~sphereClass() {}

  9. Implementation of a SphereFunctionality – Part I void sphereClass::SetRadius(double R) { TheRadius = R; } double sphereClass::Radius() { return TheRadius; } double sphereClass::Diameter() { return 2.0 * TheRadius; } double sphereClass::Circumference() { return PI * Diameter(); }

  10. Implementation of a SphereFunctionality – Part II double sphereClass::Area() { return 4.0 * PI * TheRadius * TheRadius; } double sphereClass::Volume() { double R = TheRadius; return (4.0 * PI * R * R * R)/3.0; } void sphereClass::DisplayStatistics() { cout << "\nRadius = " << Radius() << "\nDiameter = " << Diameter() << "\nCircumference = " << Circumference() << "\nArea = " << Area() << "\nVolume = " << Volume() << endl; }

  11. Using the Sphere ADTStep 3 – Application Program • Once an ADT has been defined and implemented, an application programmer can use it to help solve problems. • An application program will utilize an ADT by creating an object. An object is more powerful than a simple variable. An object contains both data and functions which manipulate the data in the object.

  12. Using the Sphere ADTObject-Oriented Program #include <iostream.h> #include "sphere.h" void main() { sphereClass MySphere(5.1); sphereClass UnitSphere; UnitSphere.DisplayStatistics(); MySphere.SetRadius(4.2); cout << MySphere.Diameter() << endl; }

  13. Review of Using an ADT • Define an ADT in a C++ class. Place this in it’s own header (.h) file. • Code the functionality of the ADT in it’s own implementation (.cpp) file. • Use the ADT by creating an object in an application (.cpp) file.

  14. Putting the Pieces Together • Create a Project • Win32 Console Application • Empty Project • Make sure all source files are in the same folder • Build • Compile (only .cpp files – creates .obj files) • Link (uses .obj files and C++ std library) • Completed product is an exe

More Related