cout and cin

hi guys I'm just wondering unlike streams that are objects such as ifstream and ofstream how come we don't have to create an object of cin or cout to use them

to use ofstream or ifstream we have to create objects from them first then we can use them

thanks
std::cin and std::cout are objects of type std::istream and std::ostream. You can't create an object of type std::cin or std::cout.
thanks goldenchicken for the reply =)

but how come you can't create objects from them as opposed to classes from fstream like ofstream and ifstream?
You can't, because std::cin and std::cout are already objects. Creating an object from type std::cin or std::cout would be the same as doing this:
1
2
int an_integer;
an_integer another_integer; //Won't compile! Using variable name as type! 

std::cin is of type std::istream, and std::cout is of type std::ostream. This means you can create your own streams like this:
1
2
3
4
std::filebuf file;
file.open("File.txt", std::ios::out);
std::ostream file_out(&file);
file_out << "This is output to a file\n";


This creates a filebuffer and an object of type std::ostream, which is the same type as std::cout.
ohh ok cout and cin are already objects created belonging to istream and ostream

that makes sense =)

thanks
Topic archived. No new replies allowed.