We are now taking a look at streams and stream objects. We mix some simple, almo
ID: 3884933 • Letter: W
Question
We are now taking a look at streams and stream objects. We mix some simple, almost old fashioned items, like simple reads and writes of characters, with some newer concepts such as defining and using objects.
Let's focus on what it means to be an "object". Post your thoughts on how objects differ from simple variables. Here's some guides:
- take a shot at explaining what an object is, in your own words
- take one of the member functions of stream objects (such as get, precision, open, close, ...) and speculate on what internal values must be stored inside the object for that function to do its job
- think about how what the operators << and >> must know in order to write and read data from and to objects like cin and cout
Explanation / Answer
An object is any real world entity. This is what the generalized definition of an object. And coming to object oriented programming an object is a collection of variables, and the methods that operate on those variables. Combining these together are called objects. A simple variables are variables that just can hold the data. And if some functions want to operate on those varibles, these variables must be either passed to those functions, or the variables should be declared globally. Either ways its risky that the variables may be modified by some functions either illegally or by chance. To avoid this, the variables are objectified, which means they are tied together with only the methods that are allowed to modify/access. Where as if the outside world want ot access the object variables, they'll simple ask the public methods for the access so that they can be accessed only through those methods.
For example: int x, float price; etc are examples of simple variables.
Where as:
class Circle
{
double radius;
public:
area();
circumference();
};
In this case, the radius is a private variable, and can only be accessed by the member functions area(), and circumference().
And for example, open() is a library method which can only be accessed by the filestream objects.
ifstream fin;
fin.open("fileName.txt");
Here the fileName is opened, and can be accessed only by the filestream fin. And it is restricted only to read from the file. And no other operations can be done.
And after reading from the file, the stream can be safely closed by using fin.close();
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.