Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Class Relationships in C++ CS 123/CS 231. 2 Class Diagram Notation Revisited zMultiplicity zAssociation and Navigability zComposition zAggregation.

Similar presentations


Presentation on theme: "1 Class Relationships in C++ CS 123/CS 231. 2 Class Diagram Notation Revisited zMultiplicity zAssociation and Navigability zComposition zAggregation."— Presentation transcript:

1 1 Class Relationships in C++ CS 123/CS 231

2 2 Class Diagram Notation Revisited zMultiplicity zAssociation and Navigability zComposition zAggregation

3 3 Mandatory Relationships Car 1 engine Engine class Car { private: Engine engine; … } Department 1 head Employee class Department { private: Employee *head; … Department(); // no default public: Department(Employee& h) { head = &h; } … }

4 4 Optional Relationship Patron 0..1 borrower Book class Book { private: Patron *borrower; … public: Book() { borrower = NULL; } void setBorrower(Patron& p) { borrower = &p; } void returnBook() { borrower = NULL; … } … }

5 5 Multiple and Mandatory Car 4 wheels Wheel // Option 2: Array of pointers class Car { private: Wheel *wheels[4]; // wheel objects may be created externally... Car(); public: Car(Wheel& w1, Wheel& w2, … ) … // wheel references required in constructor } // Option 1: Array of objects // use for composition relationships class Car { private: Wheel wheels[4];... }

6 6 Multiple but not Mandatory Patron 0..3 borrowed Book class Patron { private: Book *borrowed[3]; int numBooks; // counter … public: Patron() { numBooks = 0; } void borrow(Book& b) { if (numBooks < 3) borrowed[numBooks++] = &b; … } … }

7 7 “One-to-Many” Relationships zCollection required but size is not fixed zDynamically allocate the array ÕUse a pointer and new to create the array ÕUse pointer-to-pointers-to-objects if you want to handle references; use pointer-to-objects if you want to create the objects within the class (composition). zOr, use a container data structure Õe.g., a list structure that maintains a collection of pointers (or objects)

8 8 “One-to-Many” Examples Invoice * lines Order Line Catalog * booklist Book Teacher * advisees Student 1 advisor

9 9 Using a Variable-sized Array class Catalog { private: Book *booklist; int capacity; int listSize; … public: Catalog(int startSize) { booklist = new Book[startSize]; // note that Book objects are created here  capacity = startSize; listSize = 0; } void addBook(Book& b) { if (listSize < capacity) booklist[listSize++] = b; // question: what happens here? else // resize array and then add } … } Catalog addBook() * booklist Book

10 10 Implementing Collections through a Data Structure class Catalog { private: LinkedList booklist; … public: void addBook(Book& b) { booklist.insert(b); } … } Note: Although it is not incorrect to include the LinkedList class (and other details) in a class diagram, it is often not necessary because it is more of an implementation detail on how a collection is carried out. Catalog * Book Catalog 1 Linked List BookNode versus 1 next

11 11 Associations and Navigability zGiven two classes A and B that are associated zNavigability is the ability to access associated objects from a given class zCan provide a way to get to the B objects from A, to the A objects from B, or both ÕNote: we provided both directions for the Patron- Book example, but we did not have to ÕDecision often depends on the use cases zOr, create an association class

12 12 Common Implementations for Associations zFor 1-to-1 relationships, one-way navigability is often sufficient ÕExample: Employee and Spouse zFor 1-to-Many relationships, allow navigation at least from the “Many” participant ÕExample: Faculty-Advisor and Student zFor Many-to-Many relationships, provide two directions, or create an association class *Note that there are efficiency and update considerations/tradeoffs

13 13 Association Class StudentCourse StudentCourse ** Enrollment grade Enrollment grade ** What about navigability?

14 14 Navigability and Association Classes class Enrollment { private: Student *student; Course *course; double grade; … } class Student { private: … Enrollment **list; // collection of enrollment records // for the student (may be useful) … } class Course { private: … Enrollment **list; // collection of enrollment records // for the course (useful?) … }

15 15 Navigability and Associative Maps zIn collection classes for associations, it is also helpful to use maps (key-object pairs) instead of a list of just the objects ÕTo facilitate searching for objects in an association based on a search key zFor example, in the collection class for Enrollment, we could have maps that use student-id and/or course-number as keys ÕCarrying out the operation “list all courses and grades that student 222-11-0000 has taken” is now possible without the list member in student

16 16 Composition zRecall that the essence of composition is that the part is created and destroyed with the whole zIt is thus “natural” (at least in C++) to use object members to implement a composition relationship zBut it is possible to use pointers to part-objects and let the constructors and the destructor take care of the consistent creation and destruction of the parts

17 17 Aggregation zStanding recommendation: use pointers to represent the parts and ensure that object construction of the whole requires the user to specify the parts ÕHide the default constructor and have a user-defined constructor whose parameters are references to the parts ÕParts can be created externally, but need to be included when creating the whole zProblem: It is still possible (within the class) to have a whole without the part

18 18 Problem with Mandatory Parts and Object Pointers Car 1 engine Engine class Car { private: Engine *engine; Car(); public: Car(Engine& e); … } Problem: Programmer can still perform engine = NULL; within this class

19 19 Option: Using Members as &-References (aliases) class Car { private: Engine &engine; Car(); public: Car(Engine& e): engine(e) { … } … // can update engine here // but it can’t be “nullified” } Solution: By using a &-reference as a member, engine has to refer to an actual Engine object


Download ppt "1 Class Relationships in C++ CS 123/CS 231. 2 Class Diagram Notation Revisited zMultiplicity zAssociation and Navigability zComposition zAggregation."

Similar presentations


Ads by Google