in c++, if you have an array of objects of type (example \"Apartment class\") an
ID: 3621336 • Letter: I
Question
in c++, if you have an array of objects of type (example "Apartment class") and each object has a private member (example, apt_number) how would you search a specific apt_number among all the given apt_numbers in my array? I am stuck with my code.
Explanation / Answer
Within your Apartment class definition, you would need a public "getter" for the private member. Your class definition should be something like this: class Apartment { private: int apt_number; public: int getApt_Number() { return this->apt_number; } }; Then in your search function, you'd do a loop like this: Apartment * search(Apartment * myArray[], int arraySize, int toSearchFor) { for (int i = 0; i getApt_Number() == toSearchFor) { // This is the one whose apt_number is equal to the one we're searching for return myArray[i]; } } return NULL; }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.