Sign in to follow this  
bentaberry

function using cout and call to function question

Recommended Posts

bentaberry    100
Hi Guys, I am practicing my OOP in C++ and I have a question about my code in one of my functions.
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

Share this post


Link to post
Share on other sites
Black Knight    769
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;
}

Share this post


Link to post
Share on other sites
Brother Bob    10344
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.

Share this post


Link to post
Share on other sites
phresnel    953
Quote:
Original post by bentaberry

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;
}


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;

Share this post


Link to post
Share on other sites
bentaberry    100
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

Share this post


Link to post
Share on other sites
Black Knight    769
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().

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this