help me analyzing the code
Nov 1, 2016 at 11:00pm UTC
So I was studying overloading operator and case study in the book has this code. Help me analyzing the code. (Ex. what's going to happen on line #)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
ostream &operator <<( ostream &output, const Array &a )
{
int i;
// output private ptr-based array
for ( i = 0; i < a.size; ++i )
{
output << setw(12) << a.ptr[i];
if ( ( i + 1 ) % 4 == 0 ) // 4 numbers per row of output
output << endl;
}
if ( i % 4 != 0 ) // end last line of output
output << endl;
return output; // enables cout << x << y;
}
Nov 1, 2016 at 11:17pm UTC
http://www.cplusplus.com/reference/ostream/ostream/operator<</
The point of overloading is that you have one function (in this case the << operator) that can be used with arguments from different types.
As for this specific code: this code shows how an Array can be added to a stream. But you should never program this, as it is part of the standard library.
Last edited on Nov 1, 2016 at 11:23pm UTC
Nov 2, 2016 at 12:25am UTC
But you should never program this, as it is part of the standard library.
Please explain.
That "Array" is some custom type, and the "4 numbers per row of output" is definitely not a standard.
@
penguinprogrammer : quite ok, but what makes you think that the "ptr" is array of numbers and
private ?
What about line 8?
Topic archived. No new replies allowed.