233k views
0 votes
Given the following class definitions and assume all member functions provided have

been properly implemented.
class Address {
private:
string city, state, zipcode;
public:
Address();
string getZip() const;
};
};
class Postal Service {
private:
vector catalog;
public:
void add(const Address&);
Postal Service();
In the following space, implement a member function for the Postal Service class that takes a
string parameter, representing a zipcode. The function is to remove from the catalog an Address
object with matching zipcode. If no matching zipcode is found, the function simply does not
change the catalog. You may assume no duplicate zipcode in the catalog.

1 Answer

4 votes

Final answer:

To remove an Address object from the Postal Service's catalog, iterate through the vector using an iterator, check each Address object's zipcode, and remove the matching object using vector's erase method.

Step-by-step explanation:

You are looking to implement a member function in the Postal Service class that removes an Address object with a matching zipcode from a catalog. To achieve this, the function should iterate over the catalog of Address objects, checking each object's zipcode against the provided parameter. If a match is found, the Address object should be removed from the vector.

The following is a possible implementation of this function:

void PostalService::removeByZipcode(const std::string& targetZipcode) {
for (auto it = catalog.begin(); it != catalog.end(); ++it) {
if (it->getZip() == targetZipcode) {
catalog.erase(it);
return;
}
}
}

Notice that we iterate through the catalog using an iterator. We use the getZip member function of each Address object to compare with the target zip code. Once a matching zipcode is found, we use the erase method of vector to remove the element, and then we use return to exit the function.

User Vijay S B
by
9.3k points