Semi-advanced expo. of cout/cin/cerr/clog
2 posters
:: C Languages :: C++ :: General Discussion
Page 1 of 1
Semi-advanced expo. of cout/cin/cerr/clog
Okay, so you problably know a little about C++ right? Good. I bet your very first program looked kind-of like this :
Well today I am going to explain a crucial line within this simple program, the cout statement and the like(cin, cerr, clog).
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() :
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 :
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. : )
- 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!";
- Code:
cout.operator<<("LAWL!");
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- Posts : 137
Join date : 2011-08-01
Location : On planet Char.
Re: Semi-advanced expo. of cout/cin/cerr/clog
Thanks! You did a really nice job! Eventually, I am going to try compiling some of the code contained within the tutorial!
:: C Languages :: C++ :: General Discussion
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|