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

S

Shailesh

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;
}
 
R

Roy Varghese

Shailesh said:
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);
}
 
C

Chris Theis

Roy Varghese said:
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 << 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
 
C

Chris Theis

Chris Theis said:
// print elements
for( int i = 0< i < m.num_elems; ++i ) {
output << elem_array << endl;


This line should of course read

output << m.elem_array << endl;

Sorry for that!

Chris
 
S

Shailesh

Chris Theis said:
Roy Varghese said:
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 << 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top