简体   繁体   中英

In C++, how is cin and cout objects in relation to its class iostream?

I am taking Udacity beginner course for android app, and it says that class is something that has fields(variables) & methods, which can be used to instantiate an object. Basically, class is a blueprint for an object and an object is an instance of a class.

For example, if I create a 'TextView' object in the xml file with which I can assign a text, textSize, textColor, etc., this object is an instance of 'TextView' class, which has been already defined in Java with what fields and methods it can be instantiated with. Therefore, with TextView object I'm just creating an instance of 'TextView' class following the fields and methods of it.

Now here comes my confusion.

In C++, 'cin' and 'cout' are known as objects of class 'iostream'. But how is it possible for cin and cout to be already considered an object of class, when I have not even instantiated them in my source code?

For example, in Java, once I create the TextView in my xml source code, it will be the object of the class 'TextView'. But how is it possible for cin and cout to act as an objects of the 'iostream' class before even I instantiated it using the fields and methods of the 'iostream' class? If cin and cout are objects of 'iostream' class, does that mean it is the instantiation of 'iostream' class?

Also, a lot of definitions I looked up on google made it sound like 'iostream' is what contains cin and cout objects, but isn't an object an instance of class rather than object belonging to class in hierarchy sense? Sure, objects are what's created based off of class, but objects are not something that belong to class as if object is a smaller container living in the bigger container called class. So if cin and cout are objects of iostream, shouldn't they have all the fields and methods defined by iostream?

I am having a hard time trying to connect the dots here.

In C++, 'cin' and 'cout' are known as objects of class iostream .

That is not correct.

Here's what the standard says about their types:

namespace std {
   extern istream cin;
   extern ostream cout;
   extern ostream cerr;
   extern ostream clog;

   ...
}

But how is it possible for cin and cout to be already considered an object of class, when I have not even instantiated them in my source code?

They are declared in the standard header file iostream . They are defined in the implementation of the standard libraries. You don't need to define them in your source code.

So if cin and cout are objects of iostream, shouldn't they have all the fields and methods defined by iostream?

std::cin an object of type std::istream and std::cout is an object of type std::ostream .

You can call any public member function of std::istream on std::cin .
You can call any public member function of std::ostream on std::cout .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM