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

C++ Classes in Depth. Topics Designing Your Own Classes Attributes and Behaviors Writing Classes in C++ Creating and Using Objects.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 15: Class diagrams; class relationships.
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.
Copyright (c) 1998, 1999 D.L. Bailey * Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.
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.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 29: Operator overloading.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Copyright © 2012 Pearson Education, Inc. Chapter 10 Advanced Topics.
11/07/11Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 8 An Introduction.
6/24/2016Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 8 An Introduction.
CSIS 123A Lecture 1 Intro To Classes Glenn Stevenson CSIS 113A MSJC.
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
ECE Application Programming
Inheritance ITI1121 Nour El Kadri.
Abstract Data Types Programmer-created data types that specify
Default Constructors A default constructor is a constructor that takes no arguments. If you write a class with no constructor at all, C++ will write a.
A First C++ Class – a Circle
Review: Two Programming Paradigms
classes and objects review
Introduction to Classes
Class: Special Topics Copy Constructors Static members Friends this
More about OOP and ADTs Classes
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Function and class templates
Introduction to Classes
Inheritance Basics Programming with Inheritance
Instructor: Dr. Michael Geiger Spring 2017 Lecture 34: Inheritance
Inheritance Dr. Bhargavi Goswami Department of Computer Science
More about OOP and ADTs Classes
Computer Programming with JAVA
CISC/CMPE320 - Prof. McLeod
EECE.2160 ECE Application Programming
COP 3330 Object-oriented Programming in C++
Chapter 11 Inheritance and Polymorphism
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Review: C++ class represents an ADT
Instructor: Dr. Michael Geiger Spring 2019 Lecture 13: Exam 1 Preview
Dynamic allocation (continued)
Instructor: Dr. Michael Geiger Spring 2019 Lecture 4: Functions in C++
EECE.4810/EECE.5730 Operating Systems
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
EECE.2160 ECE Application Programming
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
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
Getting queues right … finally (?)
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)
EECE.2160 ECE Application Programming
EECE.3170 Microprocessor Systems Design I
EECE.2160 ECE Application Programming
Presentation transcript:

EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019 Lecture 14: Class relationships

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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