Presentation is loading. Please wait.

Presentation is loading. Please wait.

EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019

Similar presentations


Presentation on theme: "EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019"— Presentation transcript:

1 EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Lecture 10: Abstract data types Classes

2 Data Structures: Lecture 9
Lecture outline Announcements/reminders HW 1 due Wednesday, 2/20 Problem set dealing with algorithmic complexity No late submissions allowed (if exam on 2/22) Program 2 to be posted; due TBD Exam 1: TBD Please respond to poll if you haven’t already Today’s lecture ADT intro Class intro 6/1/2019 Data Structures: Lecture 9

3 Abstract data types (ADTs)
Processing data requires Collection of data items Basic operations to be performed on those items Combination of the two: abstract data type (ADT) “Abstract” part: definition of type separated from implementation Look at storage of data without worrying about implementation Example: “store 10 values” Could use many different implementations Algorithms defined for basic operations Effectiveness of algorithm usually linked to underlying data structures 6/1/2019 Data Structures: Lecture 9

4 C-style data structures
Can be more efficient than C++ implementation Example: array vs. C++ vector May simplify implementation but add overhead in form of operations that aren’t used Key C-style structures Arrays (1-D or greater) Structures 6/1/2019 Data Structures: Lecture 9

5 Data Structures: Lecture 9
Time ADT to represent time Data to be stored: hours, minutes, AM/PM, military (24-hour equivalent of 12-hour time) Operations: set time, display time, advance time, compare times Will define ADT using C-style implementation Will re-define later using OOP implementation 6/1/2019 Data Structures: Lecture 9

6 Time structure, prototypes
struct Time { unsigned hour, minute; char AMorPM; // 'A' or 'P' unsigned milTime; // military time equivalent }; void set(Time &t, unsigned hours, unsigned minutes, char AMPM); void display(const Time &t, ostream &out); void advance(Time &t, unsigned hours, unsigned minutes); bool lessThan(const Time &t1, const Time &t2); 6/1/2019 Data Structures: Lecture 9

7 Classes, Objects, Member Functions and Data Members
Classes: user-defined types Classes represent real concepts (e.g., car) Functions describe mechanisms that perform tasks Hide complex tasks from the user Ex: driver can use gas pedal to accelerate without knowing how acceleration is performed Must define classes before using them Ex: a car must be designed and built before it can be driven Many objects can be created from the same class Object: instance of a particular type In C++, every data type comes from an object Ex: many cars can be built from same specifications 6/1/2019 Data Structures: Lecture 10

8 Structs and Classes: Similarities
Essentially the same syntax Both are used to model objects with multiple attributes (characteristics) represented as data members also called fields … or … instance or attribute variables. Thus, both are used to process non-homogeneous data sets. 6/1/2019 Data Structures: Lecture 10

9 Structs vs. Classes: Differences
No classes in C Members public by default Can be specified private Classes Both structs and classes in C++ Structs can have members declared private Class members are private by default Can be specified public 6/1/2019 Data Structures: Lecture 10

10 Advantages in C++: (structs and classes)
C++ structs and classes model objects which have: Attributes represented as data members Operations represented as functions (or methods) Leads to object oriented programming Objects are self contained "I can do it myself" mentality They do not pass a parameter to an external function If data member is private, can only be modified by member function 6/1/2019 Data Structures: Lecture 10

11 Data Structures: Lecture 10
Class Declaration Syntax class ClassName { public: Declarations of public members private: Declarations of private members }; Order of public/private doesn’t matter Members private by default  if you list private members first, you don’t need private keyword 6/1/2019 Data Structures: Lecture 10

12 Data Structures: Lecture 10
Designing a Class Public members of class accessible to everyone Most function members are public Private members of class accessible only in member functions Data members almost always private Some private function members (helper or utility functions) Class definition in .h file (i.e., Time.h) Data members Member function prototypes Friend function prototypes Function definitions in .cpp file (i.e., Time.cpp) 6/1/2019 Data Structures: Lecture 10

13 Data Structures: Lecture 10
Class implementation One key point: within .cpp file, don’t know what namespace functions belong to Function names must include class name as well Format: <class_name>::<function_name>([param list]) { <function body> } Example: void GradeBook::setCourseName(string name) { courseName = name; } 6/1/2019 Data Structures: Lecture 10

14 Data Structures: Lecture 10
Data members Local variables Variables declared in a function definition’s body Cannot be used outside of that function body Lost when function terminates Attributes Exist throughout the life of the object Represented as data members Each object maintains its own copy of data members Functions that change data members are called mutator functions (or “set” functions) Functions that return data members are called accessor functions (or “get” functions) Good programming practice: keep data private Use mutators / accessors to set / get data Allows programmer to control data accesses 6/1/2019 Data Structures: Lecture 10

15 Example: data members (GradeBook.h)
// GradeBook class interface class GradeBook { public: // function that sets the course name void setCourseName( string name ); // function that gets the course name string getCourseName(); // function that displays a welcome message void displayMessage(); private: string courseName; // course name for this GradeBook }; 6/1/2019 Data Structures: Lecture 10

16 Example: data members (GradeBook.cpp)
// GradeBook class implementation #include “GradeBook.h” // function that sets the course name void GradeBook::setCourseName( string name ) { courseName = name; } // function that gets the course name string GradeBook::getCourseName() { return courseName; // function that displays a welcome message void GradeBook::displayMessage() { cout << "Welcome to the grade book for\n" << courseName << "!" << endl; 6/1/2019 Data Structures: Lecture 10

17 Data Structures: Lecture 10
Example (cont.) int main() { string nameOfCourse; // string of characters to store the course name GradeBook myGradeBook; // create a GradeBook object named myGradeBook // display initial value of courseName cout << "Initial course name is: " << myGradeBook.getCourseName() << endl; // prompt for, input and set course name cout << "\nPlease enter the course name:" << endl; getline( cin, nameOfCourse ); // read a course name with blanks // This version of getline works with string objects myGradeBook.setCourseName( nameOfCourse ); cout << endl; myGradeBook.displayMessage(); return 0; } Initial course name is: Please enter the course name: EECE.3220 Welcome to the grade book for EECE.3220! 6/1/2019 Data Structures: Lecture 10

18 Data Structures: Lecture 9
Final notes Next time More class details Reminders: HW 1 due Wednesday, 2/20 Problem set dealing with algorithmic complexity No late submissions allowed (if exam on 2/22) Program 2 to be posted; due TBD Exam 1: TBD Please respond to poll if you haven’t already 6/1/2019 Data Structures: Lecture 9


Download ppt "EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019"

Similar presentations


Ads by Google