Download presentation
Presentation is loading. Please wait.
Published byKimmo Jokinen Modified over 5 years ago
1
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Lecture 14: Class relationships
2
Announcements/reminders
Program 2 to be posted; due TBD Will implement dictionary Each entry contains word, part of speech, definition Load contents of dictionary from file Implement string-based commands to interact Friday, 3/8: deadline for P1 resubmissions Exams to be returned next week 5/24/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/24/2019 Data Structures: Lecture 11
4
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/24/2019 Data Structures: Lecture 11
5
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/24/2019 Data Structures: Lecture 11
6
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/24/2019 Data Structures: Lecture 11
7
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/24/2019 Data Structures: Lecture 11
8
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/24/2019 Data Structures: Lecture 11
9
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/24/2019 Data Structures: Lecture 11
10
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/24/2019 Data Structures: Lecture 11
11
Data Structures: Lecture 11
Composition example Write code for: Point Rectangle::getOrigin(); void Rectangle::setOrigin(Point p); 5/24/2019 Data Structures: Lecture 11
12
Cleaning up Wednesday’s mess (1/?) …
On Wednesday, I showed you these solutions … Point Rectangle::getOrigin() { return origin; } void Rectangle::setOrigin(Point p) { origin.setX(p.getX()); origin.setY(p.getY()); … which prompted a good question: why can we copy origin as a return value, but can’t copy an argument to origin? 5/24/2019 Data Structures: Lecture 11
13
Cleaning up Wednesday’s mess (2/?) …
The short answer: I was wrong In this case, you absolutely could write setOrigin() as follows: void Rectangle::setOrigin(Point p) { origin = p; } So, why did I show you the other version? 5/24/2019 Data Structures: Lecture 11
14
Cleaning up Wednesday’s mess (3/?) …
The long answer: 2 parts To directly access Point data members in a Rectangle function, you must use Point functions Say we wanted to setOrigin() to take 2 args: void Rectangle::setOrigin(double x, double y) { origin.xCoord = x; // Won’t work origin.setX(x); origin.setY(y); } I forgot: by default, copying one object to another is a defined operation In some cases, default behavior can be overridden In other cases, default behavior won’t work Tried to avoid too much detail to avoid confusion … … and made the lecture MORE confusing 5/24/2019 Data Structures: Lecture 11
15
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/24/2019 Data Structures: Lecture 11
16
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/24/2019 Data Structures: Lecture 11
17
Data Structures: Lecture 11
Example solution Rectangle::Rectangle(double h, double w, double x, double y) : height(h), width(w), origin(x,y) {} 5/24/2019 Data Structures: Lecture 11
18
Data Structures: Lecture 11
Final notes Next time Dynamic allocation Linked lists (Hopefully) Program 2 intro Reminders: Program 2 to be posted; due TBD Will implement dictionary Each entry contains word, part of speech, definition Load contents of dictionary from file Implement string-based commands to interact Exams to be returned next week 5/24/2019 Data Structures: Lecture 11
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.