Before several hours i posted this question, i got good answer by tipaye and closed the topic but turned out that i still wasnt able to find the solution. So here it goes again
But for easier reading ill post orginal question(exercise from book) here again
Modify the ... program to make the input stream an explicit parameter. rather than simply using cin. Also give Token_stream constructor an istream& parameter so that when we figure out how to make our own streams (attached to files) we can use the ... program for those. Hint : Dont try to copy an istream
#include <iostream>
class Point{
public:
int x;
int y;
Point() : x(0), y(0) {}
};
class Token_stream{
Point point;
// ...
public:
Point get_input();
// ...
};
Point Token_stream::get_input(){
std::cout << "Enter 2 ints ";
std::cin >> point.x >> point.y;
return point;
}
int main(){
Token_stream ts;
Point p;
p= ts.get_input();
std::cout << "x = " << p.x << std::endl
<< "y = " << p.y << std::endl;
}
This is what tipaye suggested me to do and tnx him for that
Hints:
Add a member of type std::istream reference to Token_stream
Write a constructor that takes an istream reference
Initialise member in constructor
In get_input(), use member instead of std::cin
#include <iostream>
class Point{
public:
int x;
int y;
Point() : x(0), y(0) {}
};
class Token_stream{
Point point;
std::istream& stream;// member of std::istream&
public:
Point get_input();
Token_stream(std::istream& x) : point(), stream(x){} //constructor i made
};
Point Token_stream::get_input(){
std::cout << "Enter 2 ints ";
stream >> point.x >> point.y; // in get_input use member instead of std::cin
return point;
}
int main(){
std::istream input(); // ??
Token_stream ts(input()); // ??
Point p;
p= ts.get_input();
std::cout << "x = " << p.x << std::endl
<< "y = " << p.y << std::endl;
}
I think that everything is all right except for passing an argument for Token_stream ts(??) when i define it in main(). Dont really understand what to give it as argument. Any help appreciated.
Ohh dint see u posted before so tnx a lot :) With this example i just tryed to recreate calculator programs part in the book what i should be changing and create istream as Token_stream parameter. So no worries about cout's, they are just for fun when i check out if this is actually working :)