void human::displayInfo()
{
cout<< "Name : " << getName() <<endl;
cout<< "Hair Colour : " << getHairColour() <<endl;
cout<< "Eye Colour : " << getEyeColour() <<endl;
cout<< "Skin Colour : " << getSkinColour() <<endl;
cout<< "Walk Speed : " << getWalkSpeed() <<endl;
cout<< "Run Speed : " << getRunSpeed() <<endl;
cout<< "Total Health : " << getHealth() <<endl;
}
The question is: Is it correct for me to use a call to a function like that in a cout. For example get hair colour returns a string so kinda makes sense to me that it would work in cout but getWalkSpeed() returns an integer yet the program runs fine and shows me the integer as text in the cout.
Have I coded that correctly, or am i relying on the compiler to figure that out and its not good code?
Kind Regards
David
function using cout and call to function question
Hi Guys,
I am practicing my OOP in C++ and I have a question about my code in one of my functions.
Quote:Original post by Black Knight
It works for basic data types because operator << is overloaded for them.
See here
Thanks for that info Black knight.
Kind Regards
David
In your case you can have something like this :
#include <iostream>#include <string>using std::cout;class Human{public: Human() { } // data members methods...};std::ostream& operator<< (std::ostream& out,Human& h ){ cout<< "Name : " << h.getName() <<endl; cout<< "Hair Colour : " << h.getHairColour() <<endl; cout<< "Eye Colour : " << h.getEyeColour() <<endl; cout<< "Skin Colour : " << h.getSkinColour() <<endl; cout<< "Walk Speed : " << h.getWalkSpeed() <<endl; cout<< "Run Speed : " << h.getRunSpeed() <<endl; cout<< "Total Health : " << h.getHealth() <<endl; return out;}int main(){ Human myHuman; std::cout << myHuman; return 0;}
Yup, thats exactly how I have my class setup only with an overloaded constructor.
Kind Regards
David
Kind Regards
David
Quote:Original post by Black Knight
In your case you can have something like this :
*** Source Snippet Removed ***
You shall stream to out, not cout, in operator <<. Otherwise you'll always stream to the standard output, no matter what actual stream object you pass your Human object to.
Quote:Original post by bentaberryvoid human::displayInfo(){ cout<< "Name : " << getName() <<endl; cout<< "Hair Colour : " << getHairColour() <<endl; cout<< "Eye Colour : " << getEyeColour() <<endl; cout<< "Skin Colour : " << getSkinColour() <<endl; cout<< "Walk Speed : " << getWalkSpeed() <<endl; cout<< "Run Speed : " << getRunSpeed() <<endl; cout<< "Total Health : " << getHealth() <<endl;}
You shouldn't flush after each line (endl is a combined "\n" with a flush), this unecessarily costs you performance. Better:
cout<< "Name : " << getName() <<'\n'; cout<< "Hair Colour : " << getHairColour() <<'\n'; cout<< "Eye Colour : " << getEyeColour() <<'\n'; cout<< "Skin Colour : " << getSkinColour() <<'\n'; cout<< "Walk Speed : " << getWalkSpeed() <<'\n'; cout<< "Run Speed : " << getRunSpeed() <<'\n'; cout<< "Total Health : " << getHealth() <<endl;
Quote:Original post by phresnel
You shouldn't flush after each line (endl is a combined "\n" with a flush), this unecessarily costs you performance. Better:cout<< "Name : " << getName() <<'\n'; cout<< "Hair Colour : " << getHairColour() <<'\n'; cout<< "Eye Colour : " << getEyeColour() <<'\n'; cout<< "Skin Colour : " << getSkinColour() <<'\n'; cout<< "Walk Speed : " << getWalkSpeed() <<'\n'; cout<< "Run Speed : " << getRunSpeed() <<'\n'; cout<< "Total Health : " << getHealth() <<endl;
What is a flush?
Kind Regards
David
Output is not directly written to the screen instead it is buffered and then written to the screen when a flush happens.So instead of flushing everything each line.You put everything in the buffer and then flush them with either endl our cout.flush().
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement