what does this mean?

what does this mean in more basic c++ terms? it is a function in a header file:

friend ostream& operator << (ostream& os, const item* itm);
The function ostream& operator << (ostream& os, const item* itm) overloads the operator "<<" so it can take as a parameter an pointer to a class named item.
Take a look at the following example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

class simpleClass{
   int num;
public:
   simpleClass(int x): num(x){}//Constructor for the class
   friend ostream& operator << (ostream &os, simpleClass obj); //use a friend function to overload the "<<" operator
};
ostream& operator << (ostream &os, simpleClass obj){//Function for the overloaded operator
   os << obj.num; //put into an output stream the data from the class
   return os; //return the output stream
}

int main(){
   simpleClass object(5);//create a new object of the class
   cout << object; //preview the object directly with cout through the overloaded operator "<<"
}


When you use cout << object instead of calling the default "<<" operator the program calls the overloaded which takes as arguments a reference to an output stream (cout is an output stream) and an object of the type simpleClass.
In the overloaded function it puts the "num" from the simpleClass object into the stream and then returns the stream. That way your cout stream now contains the "num" from your class. So you don't have to create a new function for viewing the data of the class and also you can use cout << object something that is very easy to remember instead of calling a function.

Hope this helps
so how would i accomplish the same thing say without the key words friend or operator?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

class simpleClass{
   int num;
public:
   simpleClass(int x): num(x){}
   void view();
};
void simpleClass::view(){
   cout << num << '\n';
}
int main(){
   simpleClass object(5);
   object.view();
}


In this example it seems easier just to use a function. But you have to remember that output streams can be for files also. So you can use the same function for the previewing and to output the information from the class into a file... And in general wherever you can use an output stream.
so in the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#ifndef ASSIGN3_H_
#define ASSIGN3_H_
#include <string>

using namespace std;

class item 
{
      public:
              string denomination;
              int date;
              string condition;
              float cost;
              float value;

              item();
              //default constructor
              
              item(string& itemDenomination, int itemDate, string& itemCondition, float itemCost, float itemValue);
              //constructor
              //the object is initialized according to the parameters.
              //postcondition: itemDenomination = denomination; 
              //               itemDate = date; 
              //               itemCondition = condition; 
              //               itemCost = cost; 
              //               itemValue = value;
              
              friend ostream& operator << (ostream& os, const item* itm);
};

class coin : public item 
{

              string obverse;
              string converse;
              char metal;
              int percentage;

      public:
             coin();
             //default constructor
             
             coin(string& coinDenomination, int coinDate, string& coinCondition, float coinCost, float coinValue, string& coinObverse, string& coinConverse, char coinMetal, int coinPercentage);
             //constructor
             //function to set the coin information
             //the coin information is set according to the parameters
             //Postcondition: obverse = coinObverse;
             //               converse = coinConverse;
             //               metal = coinMetal;
             //               percentage = coinPercentage;
             
             friend ostream& operator << (ostream& os, const coin* cn);
             //
};


class paper : public item 
{

              bool newstyle;
              char type;
      
      public:
             paper();
             //default constructor
             
             paper(string& paperDenomination, int paperDate, string& paperCondition, float paperCost, float paperValue, bool paperNewstyle, char paperType);
             //constructor
             //function to set the paper information
             //the paper information is set according to the parameters
             //postcondition: newstyle = paperNewstyle;
             //               type = paperType; 
             
             friend ostream& operator << (ostream& os, const paper* ppr);
             //
};
#endif

#include <iostream>
//#include "assign3new.h"

item::item() 
{
	denomination = "none";
	date = 1900;
	condition = "unknown";
	cost = 0.0;
	value = 0.0;
}

item::item(string& itemDenomination, int itemDate, string& itemCondition, const float itemCost, float itemValue) 
{
           denomination = itemDenomination;
           date = itemDate;
           condition = itemCondition;
           cost = itemCost;
           value = itemValue;
}

ostream& operator <<(ostream& os, item* itm) 
{
	os << itm->denomination << endl;
	os << itm->date << endl;
	os << itm->condition << endl;
	os << itm->cost << ' ' << itm->value << endl;
	
	return os;
}



coin::coin() 
{
	string obverse = "none";
	string converse = "none";
	metal = 'U';
	percentage = 0;
}

coin::coin(string& coinDenomination, int coinDate, string& coinCondition, float coinCost, float coinValue, string& coinObverse, string& coinConverse, char coinMetal, int coinPercentage) 
                   :item(coinDenomination, coinDate, coinCondition, coinCost, coinValue)
{
           obverse = coinObverse;
           converse = coinConverse;
           metal = coinMetal;
           percentage = coinPercentage;
}

ostream& operator <<(ostream& os, const coin* cn) 
{
	os << "coin: " << endl;
	os << (item*)cn;
	os << cn->obverse << endl;
	os << cn->converse << endl;
	os << cn->metal << endl;
	os << cn->percentage << endl;
	
	return os;
}

paper::paper() 
{
	newstyle = false;
	type = 'U';
}

paper::paper(string& paperDenomination, int paperDate, string& paperCondition, float paperCost, float paperValue, bool paperNewstyle, char paperType) 
                     :item(paperDenomination, paperDate, paperCondition, paperCost,paperValue)
{
            newstyle = paperNewstyle;
            type = paperType;      
}

ostream& operator <<(ostream& os, const paper* ppr) 
{
	os << "paper:" << endl;
	os << (item*)ppr;
	os << ppr->type << endl;
	os << ppr->newstyle << endl;
	
	return os;
}

#include <iostream>
#include <fstream>
#include <string>
//#include "assign3new.h"

using namespace std;

int menu();

void add_item(char type);

int main() 
{
	int argc;
	char argv;
    
    for (;;) 
    {
		switch (menu()) 
        {
		       case 0:
			        return 0;
               case 1:
			        add_item('c');
			        break;
               case 2:
			        add_item('p');
			        break;
		            default:
	           cout << "error" << endl;
		}
	}
    return 0;
}

int menu() 
{
	for (;;) 
    {
		cout << "0   Exit" << endl << "1   Create an instance of coin" << endl << "2   Create an instance of paper money" << endl;
		
		int choice;
		
		cin >> choice;
		cin.clear();
		cin.ignore(250, '\n');
		
		if ((choice < 0) || (choice > 2)) 
        {
			cout << "try again" << endl;
			continue;
		}
		return choice;
	}
}

void add_item(char type) 
{
	item * new_item;
	string denomination;
	int date;
	string condition;
	float cost;
	float value;
	
    cout << "Enter denomination: ";
	cin >> denomination;
	cout << "Enter date: ";
	cin >> date;
	cout << "Enter condition: ";
	cin >> condition;
	cout << "Enter cost: ";
	cin >> cost;
	cout << "Enter value: ";
	cin >> value;
	
	if (type == 'c') 
    {
		string obverse;
		string converse;
		char metal;
		int percentage;
		
		cout << "Enter obverse: ";
		cin >> obverse;
		cout << "Enter converse: ";
		cin >> converse;
		cout << "Enter metal: ";
		cin >> metal;
		cout << "Enter percentage: ";
		cin >> percentage;
		
		new_item = new coin(denomination, date, condition, cost, value, obverse, converse, metal, percentage);
	} 
    
    else if (type == 'p') 
    {
		bool newstyle;
		char _type;
		
        cout << "Enter newstyle: ";
		cin >> newstyle;
		cout << "Enter type: ";
		cin >> _type;
		
		new_item = new paper(denomination, date, condition, cost, value, newstyle, _type);
	}
	
    ofstream report("report.txt");
	
    if (report.is_open()) 
    {
		if (type == 'c') 
        {
			report << (coin*)new_item;
		} 
        else if (type == 'p') 
        {
			report << (paper*)new_item;
		}
		
        report.close();
	} 
    else
    {
		cout << "Unable to open file report.txt";
    }
	
    delete new_item;
	cin.clear();
	cin.ignore(250, '\n');
}


how would i implement something like that?
hmmm... As I told you it makes your life much easier when you use output file as an output stream. And in this program it definetly use the overloaded operator for the ofstream...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
ofstream report("report.txt");
	
    if (report.is_open()) 
    {
		if (type == 'c') 
        {
			report << (coin*)new_item;
		} 
        else if (type == 'p') 
        {
			report << (paper*)new_item;
		}
		
        report.close();
	} 
    else
    {
		cout << "Unable to open file report.txt";
    }
Topic archived. No new replies allowed.