By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,302 Members | 1,429 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,302 IT Pros & Developers. It's quick & easy.

How to print an array using << operator:Beginner's question

P: n/a
Hi!
I want to overload << operator so that it can print an arry defined in
MyClass.My problem is that I want to print only a no of elements NOT
all the elements in the array and this no of elements depends upon
user input.The code below describes this in detail:

class MyClass
{
public:
unsigned short x;
unsigned short y;
unsigned short num_elems;
unsigned short *elem_array;

// Constructor
// Destructor

// Print out array info

friend ostream& operator << ( ostream &output, const MyClass &m
);
};

After the user has given the values x=5,y=10 and no of elements = 2 I
have 2 elements in the array.( no of elements can be more depends on
user)

ostream& operator << ( ostream &output, const MyClass &m )
{
output << "------------------------BEGIN -------------------------" <<
endl
<< endl
<< "X: " << m.x << endl
<< "Y: " << m.y << endl
<< "Num of Elements : "<<m.num_elems<< endl
<<"Elements : "
<<<<<<<<<<<<<<????How can I print Array>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

<< "------------------------- END
--------------------------" << endl;
return output;
}
Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Shailesh wrote:
Hi! <snip>
ostream& operator << ( ostream &output, const MyClass &m )
{
output << "------------------------BEGIN -------------------------" <<
endl
<< endl
<< "X: " << m.x << endl
<< "Y: " << m.y << endl
<< "Num of Elements : "<<m.num_elems<< endl
<<"Elements : "
<<<<<<<<<<<<<<????How can I print Array>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

<< "------------------------- END
--------------------------" << endl;
return output;
}

One of many ways:
struct MyClass {
string data;
};

ostream& operator<<(ostream& os, pair<int,MyClass> intmc) {
os << "Number=" << intmc.first << " MyClass=" <<
intmc.second.data << endl;
return os;
}
int main() {
MyClass mc1;

int num;
cin >> num;
mc1.data = "Hello world";
cout << pair<int,MyClass>(num,mc1);
}

--
Roy Varghese

Replace DOT in my email with a (.) before replying.

Jul 22 '05 #2

P: n/a

"Chris Theis" <Ch*************@nospam.cern.ch> wrote in message
news:bq**********@sunnews.cern.ch...

// print elements
for( int i = 0< i < m.num_elems; ++i ) {
output << elem_array[i] << endl;


This line should of course read

output << m.elem_array[i] << endl;

Sorry for that!

Chris
Jul 22 '05 #3

P: n/a

"Roy Varghese" <rv*******@usersDOTsf.net> wrote in message
news:OWYyb.279447$275.998391@attbi_s53...
Shailesh wrote:
Hi!

<snip>

ostream& operator << ( ostream &output, const MyClass &m )
{
output << "------------------------BEGIN -------------------------" <<
endl
<< endl
<< "X: " << m.x << endl
<< "Y: " << m.y << endl
<< "Num of Elements : "<<m.num_elems<< endl
<<"Elements : "
<<<<<<<<<<<<<<????How can I print Array>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

<< "------------------------- END
--------------------------" << endl;
return output;
}

One of many ways:
struct MyClass {
string data;
};

ostream& operator<<(ostream& os, pair<int,MyClass> intmc) {
os << "Number=" << intmc.first << " MyClass=" <<
intmc.second.data << endl;
return os;
}
int main() {
MyClass mc1;

int num;
cin >> num;
mc1.data = "Hello world";
cout << pair<int,MyClass>(num,mc1);
}

--


Hi Roy,

probably I overlooked something but I can't quite see how this would help
the OP to print an array.

To the OP:

If you the number of elements that you want to print is stored in num_elems
then you can just iterate over your array. For example:

ostream& operator << ( ostream &output, const MyClass &m )
{
output << "------------------------BEGIN -------------------------" <<
endl << endl
<< "X: " << m.x << endl
<< "Y: " << m.y << endl
<< "Num of Elements : "<<m.num_elems<< endl <<"Elements : " <<
endl;

// print elements
for( int i = 0< i < m.num_elems; ++i ) {
output << elem_array[i] << endl;
}

output << "------------------------- END --------------------------" <<
endl;
return output;
}

Just some other remarks - as you have a dynamically allocated array in your
class you should take care of copy ctor & the assignment operator, which you
probably did. As it's not indicated in the code snippet you provided I just
wanted to remark on this. Furthermore for safety & comfort you might
consider using a vector class instead of the array.

Regards
Chris
Jul 22 '05 #4

P: n/a
"Chris Theis" <Ch*************@nospam.cern.ch> wrote in message news:<bq**********@sunnews.cern.ch>...
"Roy Varghese" <rv*******@usersDOTsf.net> wrote in message
news:OWYyb.279447$275.998391@attbi_s53...
Shailesh wrote:
Hi! <snip>
ostream& operator << ( ostream &output, const MyClass &m )
{
output << "------------------------BEGIN -------------------------" <<
endl
<< endl
<< "X: " << m.x << endl
<< "Y: " << m.y << endl
<< "Num of Elements : "<<m.num_elems<< endl
<<"Elements : "
<<<<<<<<<<<<<<????How can I print Array>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

<< "------------------------- END
--------------------------" << endl;
return output;
}

One of many ways:
struct MyClass {
string data;
};

ostream& operator<<(ostream& os, pair<int,MyClass> intmc) {
os << "Number=" << intmc.first << " MyClass=" <<
intmc.second.data << endl;
return os;
}
int main() {
MyClass mc1;

int num;
cin >> num;
mc1.data = "Hello world";
cout << pair<int,MyClass>(num,mc1);
}

--


Hi Roy,

probably I overlooked something but I can't quite see how this would help
the OP to print an array.

To the OP:

If you the number of elements that you want to print is stored in num_elems
then you can just iterate over your array. For example:

ostream& operator << ( ostream &output, const MyClass &m )
{
output << "------------------------BEGIN -------------------------" <<
endl << endl
<< "X: " << m.x << endl
<< "Y: " << m.y << endl
<< "Num of Elements : "<<m.num_elems<< endl <<"Elements : " <<
endl;

// print elements
for( int i = 0< i < m.num_elems; ++i ) {
output << elem_array[i] << endl;
}

output << "------------------------- END --------------------------" <<
endl;
return output;
}

Just some other remarks - as you have a dynamically allocated array in your
class you should take care of copy ctor & the assignment operator, which you
probably did. As it's not indicated in the code snippet you provided I just
wanted to remark on this. Furthermore for safety & comfort you might
consider using a vector class instead of the array.

Regards
Chris

Thanks Chris and Roy for all your suggestions.I was looking for what
Chris had suggested.
Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.