Would you like to react to this message? Create an account in a few clicks or log in to continue.

Semi-advanced expo. of cout/cin/cerr/clog

2 posters

Go down

Semi-advanced expo. of cout/cin/cerr/clog Empty Semi-advanced expo. of cout/cin/cerr/clog

Post  legolizard Tue Oct 04, 2011 10:28 pm

Okay, so you problably know a little about C++ right? Good. I bet your very first program looked kind-of like this :

Code:
#include <iostream>

using namespace std;

int main(){

cout << "Hello World!"<<endl;
return 0;
}

Well today I am going to explain a crucial line within this simple program, the cout statement and the like(cin, cerr, clog).

Code:
int x;

Now, assuming you know even a little about C++, you should know what this line of code is and what it does when the compiler reaches it; it creates a variable named 'x' of type integer. Cout is an object of type ostream. Cin works much the same however it is an object of type istream. Cerr and clog are also objects of the ostream class.

To demonstrate that all of the above things are objects type the following code within your programs main() :

Code:
cout.operator<<("LAWL!");

Note the .(dot) operator, which is used exclusively with objects. After compiling and running this code however we do not get the same string literal as we have previously entered and rather get some address. This is because when you call this :

Code:
cout<<"LAWL!";
You overload this :
Code:
cout.operator<<("LAWL!");
Thus resulting in the difference.

So in short cout, cin, cerr, and clog are all objects defined within the iostream header file.


If you have any questions I would be more than happy to answer them. : )


Last edited by legolizard on Thu Dec 22, 2011 8:47 pm; edited 6 times in total (Reason for editing : Forum was messing up my post. :///)
legolizard
legolizard

Posts : 137
Join date : 2011-08-01
Location : On planet Char.

Back to top Go down

Semi-advanced expo. of cout/cin/cerr/clog Empty Re: Semi-advanced expo. of cout/cin/cerr/clog

Post  David B Tue Oct 04, 2011 10:37 pm

Thanks! You did a really nice job! Eventually, I am going to try compiling some of the code contained within the tutorial!
David B
David B
Administrator
Administrator

Posts : 618
Join date : 2011-07-20
Location : The Twilight Zone!

https://programmingforums.forumotion.com/

Back to top Go down

Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum