質問編集履歴

1

C++のサンプルコードを追加しました

2019/04/20 04:44

投稿

takdcloose
takdcloose

スコア10

test CHANGED
File without changes
test CHANGED
@@ -130,4 +130,94 @@
130
130
 
131
131
 
132
132
 
133
+
134
+
135
+ //C++のサンプルコード
136
+
137
+
138
+
139
+ #include <iostream>
140
+
141
+ #include <cstring>
142
+
143
+ using namespace std;
144
+
145
+
146
+
147
+ class inventory {
148
+
149
+ char item[40];
150
+
151
+ int onhand;
152
+
153
+ double cost;
154
+
155
+ public :
156
+
157
+ inventory(char *i, int o,double c){
158
+
159
+ strcpy(item,i);
160
+
161
+ onhand = o;
162
+
163
+ cost = c;
164
+
165
+ }
166
+
167
+ friend ostream &operator <<(ostream &stream, inventory ob);
168
+
169
+ friend istream &operator >>(istream &stream, inventory &ob);
170
+
171
+ };
172
+
173
+
174
+
175
+ ostream &operator<<(ostream &stream, inventory ob){
176
+
177
+ stream << ob.item << " :在庫数: " << ob.onhand;
178
+
179
+ stream << " 費用は: " << ob.cost << endl;
180
+
181
+ return stream;
182
+
183
+ }
184
+
185
+
186
+
187
+ istream &operator>>(istream &stream, inventory &ob){
188
+
189
+ cout << "品名を入力: ";
190
+
191
+ stream >> ob.item;
192
+
193
+ cout << "在庫数を入力: ";
194
+
195
+ stream >> ob.onhand;
196
+
197
+ cout << "原価を入力: ";
198
+
199
+ stream >> ob.cost;
200
+
201
+ return stream;
202
+
203
+ }
204
+
205
+
206
+
207
+ int main(){
208
+
209
+ inventory ob("金槌", 4, 12.55);
210
+
211
+
212
+
213
+ cin >> ob;
214
+
215
+ cout << ob;
216
+
217
+
218
+
219
+ return 0;
220
+
221
+ }
222
+
133
223
  ```