Create a print function that takes an ostream as an argument and writes to that stream
Asked Answered
W

2

5

Im currently anwsering exercise questions concerning operator overloading in C++. I have a question:

Create a simple class containing an int and overload the operator+ as a member function. Also provide a print( ) member function that takes an ostream& as an argument and prints to that ostream&. Test your class to show that it works correctly.

I can create the class and write the operator+ function alright but I really dont understand the second part of the question. So far in my study of c++ I havent really come across ostream's and as such am not sure if its possible to explicitly create such a stream. I have tried using:

std::ostream o;

However this produces an error. Could someone please enlighten me on how I should create this function please?

Whimwham answered 24/8, 2011 at 16:44 Comment(2)
cout is an ostream. Are you sure you haven't come across that?Paquito
I do realize that cout is ostream, however that is as much as iv used it, however, iv never been asked to pass one as an argument which is why I was unsure how to do this.Whimwham
H
10

So far in my study of c++ I havent really come across ostream's and as such am not sure if its possible to explicitly create such a stream. I have tried using: std::ostream o;

You must have missed something, because ostreams are important. std::cout is a variable of type std::ostream by the way. Usage is more or less like this

#include <iostream> //defines "std::ostream", and creates "std::ofstream std::cout"
#include <fstream> //defines "std::ofstream" (a type of std::ostream)
std::ostream& doStuffWithStream(std::ostream &out) { //defines a function
    out << "apples!";
    return out;
}
int main() {
    std::cout << "starting!\n"; 
    doStuffWithStream(std::cout); //uses the function

    std::ofstream fileout("C:/myfile.txt"); //creates a "std::ofstream"
    doStuffWithStream(fileout); //uses the function

    return 0;
}
Hera answered 24/8, 2011 at 16:58 Comment(2)
I do understand that cout is type ostream, however as iv never had to pass ostream as a function argument I was unsure how to do it. I thought to have a reference to an ostream as an argument in a function I had to create an ostream object first, seems this is not the case. Thanks for helping to clear this up.Whimwham
A function definition doesn't require any objects to actually exist yet, it only needs to know about types. Obviously, you have to create a stream at some point to pass to the function. Here, I pass std::cout (which is automatically created when you include <iostream>), and fileout, which I create myself.Hera
C
4

You don't create an ostream, you create an ostream reference, like your exercise question said. And you do it in the parameter list of your print function, i.e.

void print(std::ostream & os);

Then you can call that function, passing cout or any other object of a class derived from ostream(ofstream, ostringstream, etc...)

Cantata answered 24/8, 2011 at 16:53 Comment(1)
Thank you for your help. Normally when passing an argument you need an object to pass which is why I was confused by this question, I thought I had to create an object of type ostream.Whimwham

© 2022 - 2024 — McMap. All rights reserved.