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 11: More class details

2 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

3 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

4 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

5 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

6 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

7 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

8 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

9 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

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

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

12 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

13 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

14 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

15 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

16 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

17 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

18 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

19 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

20 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

21 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

22 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

23 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

24 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

25 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

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

27 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

28 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

29 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

30 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

31 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


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

Similar presentations


Ads by Google