472,805 Members | 1,050 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,805 software developers and data experts.

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

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
4 2471
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

"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

"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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Tony Johansson | last post by:
Hello Experts! I have one class template here called Handle and one concrete class called Point. At the bottom is the main program and the class template definitions for Handle. This program...
4
by: Mark Hanley | last post by:
I have found similar problems to mine on this and other newsgroups but I still haven't been able to solve my problem... I have two tables 'Pupil' and 'SEN' which are related on a field called...
1
by: herrcho | last post by:
#include <stdio.h> int multi; int main() { printf("\nmulti = %u",multi); printf("\nmulti = %u",multi); printf("\n&multi = %u\n",&multi); return 0;
11
by: Shawn Odekirk | last post by:
Some code I have inherited contains a macro like the following: #define setState(state, newstate) \ (state >= newstate) ? \ (fprintf(stderr, "Illegal...
2
by: Harry | last post by:
Hi all, I am writing a logger program which can take any datatype. namespace recordLog { enum Debug_Level {low, midium, high}; class L { std::ofstream os; Debug_Level cdl; const...
1
by: hunter hou | last post by:
Hello,Please look at the following code(from C++ in a nutshell) and my questions.Thanks,***Hunter... typedef void (*strproc)(const char*); void print(const char* str) { std::cout << "const...
6
by: Kbalz | last post by:
Trying to run a simple query on my dataset the SQL statement looks like this: select * from person where id in (@p) When using more than one id in the "in statement", I get...
4
by: subramanian100in | last post by:
In the book, C++ Coding Standards book by Hereb Sutter and Andrei Alexandrescu, in Item 40 on pages 86-87 viz, "Avoid providing implicit conversions", the authors have advised the use of named...
2
by: swethak | last post by:
Hi, i am a beginner in c++. i wrote the program like this #using<mscorlib.dll> #using<System.Windows.Forms.dll> // Class derived from Forms
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.