I have a constructor Kruskal(constunsigned& , istream &is = cin ) to accept integer from standard input to construct the Kruskal object. Because ifstream is derived from istream, can use an ifstream object to read files?
yes, you can reference an istream object, such as the default input stream cin, or a ifstream object initialised to point to a data file, they will work just the same.
However, if I were you, for expandability, I would make Kruskal::Kruskal call a read(std::istream&) member function, which you can then define to read in whatever you like rather than just an int. Then you can add new data members to your class and have the option of initializing them from a data file using Kruskal::read(std::istream&) without having to change your interface.
Thanks, matsom and dangrr888.
Yeah, i tried, I works fine!
I know, though, " 1 item per post ", but i find a tiny problem:
When i use an ifstream object to read a file, why can't it read into the last integer if the integer is the last word of the file without any character appending , no blank space , no newline, no anything?