\( \begin{array}{l}\text { Given the following class definition and the following } \\ \text { member function header, which is the correct way to } \\ \text { output the private data? } \\ \text { class Person } \\ \text { I public: } \\ \text { void outputPerson(ostream\& out); } \\ \text { private: } \\ \text { int age; } \\ \text { float weight; } \\ \text { int id; }\end{array} \) I; \( \begin{array}{l}\text { I/what goes here? } \\ \text { void outputPerson(ostream\& out) } \\ \text { I outputPerson(person); }\end{array} \) out \ll age << weight << id;
Real Tutor Solution
Answer
Solution
Reviewed and approved by the UpStudy tutoring team
Extra Insights
To correctly implement the `outputPerson` function, inside the function body, you'll want to use the `ostream` reference provided to format and output the private member variables. So your function implementation will look something like this:
```cpp
void outputPerson(ostream& out) {
out << "Age: " << age << ", Weight: " << weight << "kg, ID: " << id << endl;
}
```
This will give a clear and concise output of a `Person` object's attributes, complete with labels!
Now, a common mistake with such functions is forgetting to handle the output stream correctly. Ensure you're using the right formatting and don't forget to include necessary headers, like `