140 likes | 280 Views
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.
E N D
Comp 245Data 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. • 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.
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…)
ADT Example – A Sphere • WHATdata is needed to be able to provide the functionality we want? • A radius
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.
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; };
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.
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() {}
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(); }
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; }
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.
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; }
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.
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