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.
INSTRUCTOR: SHIH-SHINH HUANG Windows Programming Using Java Chapter3: Introduction to Classes and Objects 1.
 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.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 15: Class diagrams; class relationships.
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.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
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.
Chapter 4 Introduction to Classes, Objects, Methods and strings
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.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 11: Class diagrams; class relationships.
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.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
11/07/11Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 8 An Introduction.
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.
CSIS 123A Lecture 1 Intro To Classes Glenn Stevenson CSIS 113A MSJC.
Introduction to Classes and Objects
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
ECE Application Programming
Abstract Data Types Programmer-created data types that specify
Chapter 3 Introduction to Classes, Objects and Strings
Chapter 3 Introduction to Classes and Objects
CMSC202 Computer Science II for Majors Lecture 08 – Overloaded Constructors Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
Introduction to Classes
Function and class templates
Introduction to Classes
IFS410: Advanced Analysis and Design
Week 3 Object-based Programming: Classes and Objects
Introduction to Classes and Objects
Classes, Objects, Methods and Strings
EECE.2160 ECE Application Programming
Object Oriented Programming in java
3-4-5 Introduction.
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
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Instructor: Dr. Michael Geiger Spring 2017 Lecture 12: Exam 1 Preview
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Instructor: Dr. Michael Geiger Spring 2019 Lecture 23: Exam 2 Preview
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
EECE.2160 ECE Application Programming
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2017
Lecture 8 Object Oriented Programming (OOP)
Classes and Objects Systems Programming.
Introduction to Classes and Objects
Presentation transcript:

EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019 Lecture 11: More class details

Announcements/reminders HW 1 due Wednesday, 2/20 Problem set dealing with algorithmic complexity No submissions after 1 PM, 2/22 Will post solution around that time Program 2 to be posted; due TBD Exam 1: Monday, 2/25, 3-5 PM, Ball 214 Will be allowed one 8.5” x 11” double-sided note sheet Preview lecture Friday 2/22 Survey to be posted to deal with exam conflicts 5/23/2019 Data Structures: Lecture 11

Data Structures: Lecture 11 Today’s lecture Review Abstract data types Class basics More details on classes Class definitions Mutators/accessors Constructors Composition 5/23/2019 Data Structures: Lecture 11

Review: Abstract data types (ADTs) Processing data requires Storing data items Operations to be performed on those items Combination of the two: abstract data type (ADT) “Abstract”  no specific implementation Data storage: for example, “store 10 values” Could use array, linked list, tree, etc Operations: algorithms, not code Can analyze complexity without HW/SW specifics 5/23/2019 Data Structures: Lecture 11

Data Structures: Lecture 11 Review: Classes Classes: programmer-defined types Concrete implementation of ADT Objects: instances of a class Each class typically contains Data members: attributes for each object Each object has own copy of data members Member functions: Tasks specific to class Data/functions can be: Public: accessible anywhere Private: accessible only in member functions Members private by default Private functions also known as helper functions 5/23/2019 Data Structures: Lecture 11

Data Structures: Lecture 11 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 5/23/2019 Data Structures: Lecture 11

Data Structures: Lecture 11 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) 5/23/2019 Data Structures: Lecture 11

Data Structures: Lecture 11 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; } 5/23/2019 Data Structures: Lecture 11

Data Structures: Lecture 11 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 5/23/2019 Data Structures: Lecture 11

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 }; 5/23/2019 Data Structures: Lecture 11

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; 5/23/2019 Data Structures: Lecture 11

Data Structures: Lecture 11 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! 5/23/2019 Data Structures: Lecture 11

Data Structures: Lecture 11 Constructors Functions used to initialize an object’s data when it is created Call made implicitly when object is created Must be defined with the same name as the class Cannot return values Not even void Default constructor has no parameters The compiler will provide one when a class does not explicitly include a constructor Compiler’s default constructor only calls constructors of data members that are objects of classes 5/23/2019 Data Structures: Lecture 11

Example: constructors (GradeBook.h) // NOTE: See web for #includes // GradeBook class interface class GradeBook { public: GradeBook(); // Default constructor GradeBook(string name); // Parameterized constructor // 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 }; // end class GradeBook 5/23/2019 Data Structures: Lecture 11

Example: constructors (GradeBook.cpp) Add the following to GradeBook.cpp: // Default constructor GradeBook::GradeBook() { courseName = “”; } // Parameterized constructor GradeBook::GradeBook(string name) { courseName = name; 5/23/2019 Data Structures: Lecture 11

Data Structures: Lecture 11 Example (cont.) // function main begins program execution int main() { // create two GradeBook objects GradeBook gradeBook1( "CS101 Introduction to C++ Programming" ); GradeBook gradeBook2( "CS102 Data Structures in C++" ); // display initial value of courseName for each GradeBook cout << "gradeBook1 created for course: " << gradeBook1.getCourseName() << "\ngradeBook2 created for course: " << gradeBook2.getCourseName() << endl; return 0; // indicate successful termination } // end main gradeBook1 created for course: CS 101 Introduction to C++ Programming gradeBook2 created for course: CS 102 Data Structures in C++ Output: 5/23/2019 Data Structures: Lecture 11

Data Structures: Lecture 11 Overloaded functions Two constructors are examples of overloaded functions Functions with same name but different parameters Compiler determines which version to call Difference(s) can include Number of parameters Type of parameters Different return type alone not sufficient to overload Overloading works with any function For example, program could contain these prototypes: int f(); int f(int a, int b); Main function might contain these function calls: int x = f(); // Calls first version int y = f(1, 2); // Calls second version 5/23/2019 Data Structures: Lecture 11

Examples: using classes Using GradeBook, which statements would be valid in a main() program written in the same file? #include <string> using std::string; class GradeBook { public: GradeBook( ); GradeBook( string name ); void setCourseName(string name); string getCourseName(); void displayMessage(); private: string name; }; // end class GradeBook GradeBook g1(3220); GradeBook g2; setCourseName(g2); g2.name = “EECE.3220”; string s = g2.getCourseName(); g2.displayMessage; 5/23/2019 Data Structures: Lecture 11

Examples: using classes GradeBook g1(3220); Invalid—constructor takes string as argument Valid alternative: GradeBook g1(“3220”); GradeBook g2; Valid—creates new GradeBook object using default constructor setCourseName(g2); Invalid—improper way to call member function Valid alternative: g2.setCourseName(“EECE.3220”); 5/23/2019 Data Structures: Lecture 11

Examples: using classes (cont.) g2.name = “EECE.3220”; Invalid—name is private data Must use public “set” function to assign value string s = g2.getCourseName(); Valid—correct syntax for calling member function, and type for s matches return type for getCourseName(); g2.displayMessage; Invalid—displayMessage is a function and therefore needs parentheses after the function name: g2.displayMessage(); 5/23/2019 Data Structures: Lecture 11

Data Structures: Lecture 11 Class relationships Typically have multiple objects in program Different types may interact with one another Basic interactions: association One class “uses” another in some way Example (from text): ATM “executes” a Withdrawal Classes as data members: “has a” Two such relationships: aggregation and composition Aggregation: “parent” contains pointer to “child” Composition: “parent” contains object of “child” type Like nested structures 5/23/2019 Data Structures: Lecture 11

Data Structures: Lecture 11 Composition example A rectangle is a shape that has a: point of origin width height Can implement this concept by defining a class named Rectangle Methods might include: Accessing width/height/origin Setting width/height/origin Calculating area .h files on next two slides Most function definitions self-explanatory 5/23/2019 Data Structures: Lecture 11

Data Structures: Lecture 11 Point.h class Point { public: Point(); // Default constructor Point(double X, double Y); // Parameterized constructor void setX(double newX); // Set X coordinate void setY(double newY); // Set Y coordinate double getX(); // Returns X coordinate double getY(); // Returns Y coordinate void printPoint(ostream &out); // Output Point as // (xCoord, yCoord) private: double xCoord; // X coordinate double yCoord; // Y coordinate }; 5/23/2019 Data Structures: Lecture 11

Data Structures: Lecture 11 Rectangle.h class Rectangle { public: Rectangle(); // Default constructor Rectangle(double h, double w, // Parameterized const. double x, double y); double getHeight(); // Return height double getWidth(); // Return width Point getOrigin(); // Return origin void setHeight(double h); // Change height void setWidth(double w); // Change width void setOrigin(Point p); // Change origin double area(); // Return area of rectangle private: double width; double height; Point origin; // Lower left corner }; 5/23/2019 Data Structures: Lecture 11

Example code: setOrigin() void Rectangle::setOrigin(double x, double y) { origin.xCoord = x; // Won’t work origin.setY(y); } Slightly different version than in .h file Takes two doubles, not Point Example shows two different ways of accessing elements of Point Directly changing private data still won’t work Must use set functions 5/23/2019 Data Structures: Lecture 11

Data Structures: Lecture 11 Composition example Write code for: Point Rectangle::getOrigin(); void Rectangle::setOrigin(Point p); 5/23/2019 Data Structures: Lecture 11

Data Structures: Lecture 11 Example solutions Point Rectangle::getOrigin() { return origin; } void Rectangle::setOrigin(Point p) { origin.setX(p.getX()); origin.setY(p.getY()); 5/23/2019 Data Structures: Lecture 11

Data Structures: Lecture 11 Initialization lists How would we write Rectangle constructor(s)? Could use Point set functions Ideally, we’d like to call Point constructor as well Create new Point every time we create Rectangle object Use an initialization list Explicitly calls constructors for member data Requires parameterized constructor to be defined Can be used for predefined types as well Example: Rectangle::Rectangle() : height(1), width(1), origin(0,0) {} 5/23/2019 Data Structures: Lecture 11

Initialization list example Write the parameterized constructor for the Rectangle class for which the prototype is: Rectangle(double h, double w, double x, double y); 5/23/2019 Data Structures: Lecture 11

Data Structures: Lecture 11 Example solution Rectangle::Rectangle(double h, double w, double x, double y) : height(h), width(w), origin(x,y) {} 5/23/2019 Data Structures: Lecture 11

Data Structures: Lecture 11 Final notes Next time Exam 1 Preview (F 2/22) Reminders: HW 1 due Wednesday, 2/20 Problem set dealing with algorithmic complexity No submissions after 1 PM, 2/22 Will post solution around that time Program 2 to be posted; due TBD Exam 1: Monday, 2/25, 3-5 PM, Ball 214 Will be allowed one 8.5” x 11” double-sided note sheet 5/23/2019 Data Structures: Lecture 11