Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Comp 245 Data Structures (A)bstract (D)ata (T)ypes ADT."— Presentation transcript:

1 Comp 245 Data 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 WHAT functionality do we want a sphere to have? o Find out it’s radius o Set it’s radius o Compute it’s diameter o Compute it’s circumference o Compute it’s area o Compute it’s volume o Display all of it’s statistics (area, volume, etc…)

4 ADT Example – A Sphere WHAT data is needed to be able to provide the functionality we want? o A radius

5 Implementing the Sphere ADT Step 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 ADT Using 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 ADT Step 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 Sphere Constructors/Destructor #include "sphere.h" #include const double PI = 3.14; sphereClass::sphereClass(double R):TheRadius(R) {} sphereClass::sphereClass():TheRadius(1.0) {} sphereClass::~sphereClass() {}

9 Implementation of a Sphere Functionality – 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 Sphere Functionality – 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 ADT Step 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 ADT Object-Oriented Program #include #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


Download ppt "Comp 245 Data Structures (A)bstract (D)ata (T)ypes ADT."

Similar presentations


Ads by Google