2
2
#include < fstream>
3
3
using namespace std ;
4
4
5
- class Product {
6
- public :
5
+ class Item {
6
+ private :
7
7
string name;
8
8
float price;
9
- int quantity;
10
-
11
- // constructor
12
- Product (string name = " " ,float price = 0 , int quantity = 0 ){
13
- this ->name = name;
14
- this ->price = price;
15
- this ->quantity = quantity;
16
- }
9
+ int qty;
10
+ public:
11
+ Item (){}
12
+ Item (string n,float p,int q);
13
+ friend ifstream & operator >>(ifstream &ifs,Item &i);
14
+ friend ofstream & operator <<(ofstream &ofs,Item &i);
15
+ friend ostream & operator <<(ostream &out,Item &i);
17
16
18
- friend ifstream & operator >>(ifstream &ifs, Product &p);
19
- friend ofstream & operator <<(ofstream &ofs, Product &p);
20
17
};
21
18
22
- ifstream & operator >>(ifstream &ifs, Product &p){
23
- ifs>>p.name >>p.price >>p.quantity ;
24
- return ifs;
19
+ Item::Item (string n,float p,int q){
20
+ name=n;
21
+ price=p;
22
+ qty=q;
25
23
}
26
24
27
- ofstream & operator <<(ofstream &ofs, Product &p){
28
- cout<<" Name : " <<p.name <<endl;
29
- cout<<" Price : $" <<p.price <<endl;
30
- cout<<" Quantity : " <<p.quantity <<endl;
25
+ ofstream & operator <<(ofstream &ofs,Item &i){
26
+ ofs<<i.name <<endl<<i.price <<endl<<i.qty <<endl;
31
27
return ofs;
32
28
}
33
29
34
- int main (){
35
- Product p1 (" Hennessey Cognac" ,399.99 ,60 );
36
- ifstream ifs (" product.txt" );
37
- ifs>>p1;
38
- ifs.close ();
30
+ ifstream & operator >>(ifstream &ifs,Item &i){
31
+ ifs>>i.name >>i.price >>i.qty ;
32
+ return ifs;
33
+ }
39
34
40
- ofstream ofs (" product.txt" );
41
- ofs<<p1;
42
- ofs.close ();
35
+ ostream & operator <<(ostream &out,Item &i){
36
+ out<<i.name <<endl<<i.price <<endl<<i.qty <<endl;
37
+ return out;
38
+ }
43
39
40
+
41
+ int main ()
42
+ {
43
+ int n;
44
+ string name;
45
+ float price;
46
+ int qty;
47
+
48
+ cout<<" Enter number of Item:" ;
49
+ cin>>n;
50
+
51
+ Item *list[n];
52
+
53
+ cout<<" Enter All Item " <<endl;
54
+ for (int i=0 ;i<n;i++){
55
+ cout<<" Enter " <<i+1 <<" Item Name , Price and Quantity" <<endl;
56
+ cin>>name;
57
+ cin>>price;
58
+ cin>>qty;
59
+
60
+ list[i]=new Item (name,price,qty);
61
+ }
62
+
63
+ ofstream fos (" product.txt" );
64
+ for (int i=0 ;i<n;i++){
65
+ fos<<*list[i];
66
+ }
67
+
68
+ Item item;
69
+ ifstream ifs (" product.txt" );
70
+ for (int i=0 ;i<n;i++){
71
+ ifs>>item;
72
+ cout<<" Item " <<i<<endl<<item<<endl;
73
+ }
44
74
return 0 ;
45
75
}
0 commit comments