From 4ae56c5a42d724c3aff7f09a1de29bfd869100fa Mon Sep 17 00:00:00 2001 From: Iurii Tatishchev Date: Thu, 2 May 2024 13:03:27 -0700 Subject: [PATCH] 6.10 Lab*: Singly-Linked Lists (Park) Replace Park.hDisplay() with output stream operator --- 03-lists/LinkedList.cpp | 2 +- 03-lists/Park.cpp | 12 ++++++------ 03-lists/Park.h | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/03-lists/LinkedList.cpp b/03-lists/LinkedList.cpp index 36d3138..1e3dead 100644 --- a/03-lists/LinkedList.cpp +++ b/03-lists/LinkedList.cpp @@ -97,7 +97,7 @@ void LinkedList::displayList() const { // While pCur points to a node, traverse the list. while (pCur) { // Display the value in this node. - pCur->park.hDdisplay(); + cout << pCur->park << endl; // Move to the next node. pCur = pCur->next; diff --git a/03-lists/Park.cpp b/03-lists/Park.cpp index fb6fe3d..beab99c 100644 --- a/03-lists/Park.cpp +++ b/03-lists/Park.cpp @@ -38,11 +38,12 @@ Park::Park(string cd, string st, string nm, string dsc, int yr) { // Displays the values of a Park object member variables // on one line (horizontal display) // *********************************************************** -void Park::hDdisplay() const { - cout << code << " "; - cout << state << " "; - cout << year << " "; - cout << name << " " << endl; +ostream& operator<<(ostream &out, const Park &park) { + out << park.code << " "; + out << park.state << " "; + out << park.year << " "; + out << park.name << " "; + return out; } // *********************************************************** @@ -55,4 +56,3 @@ void Park::vDisplay() const { cout << year << endl; cout << state << endl; } - diff --git a/03-lists/Park.h b/03-lists/Park.h index 63b7477..a5ef177 100644 --- a/03-lists/Park.h +++ b/03-lists/Park.h @@ -56,7 +56,7 @@ public: int getYear() const { return year; } //other functions - void hDdisplay() const; + friend std::ostream &operator<<(std::ostream &, const Park &); void vDisplay() const; };