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

Slides:



Advertisements
Similar presentations
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 10: Continuing with classes Constructors, using classes.
Advertisements

What have we learned so far… Preprocessor directives Introduction to C++ Variable Declaration Display Messages on Screen Get Information from User Performed.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Classes and Objects Systems Programming.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Reformatted slides from the textbook, C++ How to Program, 6/e Pearson Education, Inc. All rights reserved Chapter 3. [Lecture 01] Introduction to.
1.  A method describes the internal mechanisms that actually perform its tasks  A class is used to house (among other things) a method ◦ A class that.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Separate Compilation. A key concept in programming  Two kinds of languages, compilation (C, Pascal, …) and interpretation (Lisp, …, Matlab, Phython,
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Fall 2012 Lecture 8: File I/O; Introduction to classes.
Reformatted slides from the textbook, C++ How to Program, 6/e Pearson Education, Inc. All rights reserved Chapter 3. [Lecture 02] Introduction to.
Object Oriented Programming (OOP) Lecture No. 11.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More about.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
Chapter 3 (B) 3.5 – 3.7.  Variables declared in a function definition’s body are known as local variables and can be used only from the line of their.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 9: Continuing with classes.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2005 Pearson Education, Inc. All rights reserved. 1 Introduction to Classes and Objects.
A DVANCED P ROGRAMMING C HAPTER 4: I NTRODUCTION TO I NTRODUCTION TO C LASSES, O BJECTS, M ETHODS AND STRINGS Dr Shahriar Bijani Winter 2016.
1 Data Structures CSCI 132, Spring 2014 Lecture 2 Classes and Abstract Data Types Read Ch Read Style Guide (see course webpage)
Friend Functions. Problem u Assuming two Complex objects u How would one add two numbers? W + X Complex operator+(const Complex& w, const Complex& x);
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More about.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 13: Exam 1 Preview.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved. Note: C How to Program, Chapter 16 is a copy of C++ How to Program Chapter.
Chapter 3 Introduction to Classes, Objects and Strings C++ How to Program, 9/e ©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved. Instructor.
Introduction to Classes and Objects
Chapter 12 Classes and Abstraction
Structures and Classes
Chapter 7: Introduction to Classes and Objects
Abstract Data Types Programmer-created data types that specify
Chapter 3 Introduction to Classes, Objects and Strings
Chapter 3 Introduction to Classes and Objects
Review: Two Programming Paradigms
More about OOP and ADTs Classes
CMPE Data Structures and Algorithms in C++ February 22 Class Meeting
Dr Shahriar Bijani Winter 2017
Introduction to Classes
IFS410: Advanced Analysis and Design
More about OOP and ADTs Classes
Week 3 Object-based Programming: Classes and Objects
Introduction to Classes and Objects
Dr. Bhargavi Dept of CS CHRIST
Classes, Objects, Methods and Strings
COP 3330 Object-oriented Programming in C++
Object Oriented Programming in java
Instructor: Dr. Michael Geiger Spring 2019 Lecture 13: Exam 1 Preview
Visual Programming Lecture 4.
Introduction to Classes and Objects
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Data Structures and ADTs
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Instructor: Dr. Michael Geiger Spring 2017 Lecture 12: Exam 1 Preview
Instructor: Dr. Michael Geiger Spring 2019 Lecture 23: Exam 2 Preview
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2017
EECE.2160 ECE Application Programming
Classes and Objects Systems Programming.
Introduction to Classes and Objects
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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