Output account info to a file using an inserter. : Inserter « Overload « C++

C++
1. Bitset
2. Class
3. Console
4. Data Structure
5. Data Type
6. Deque
7. Development
8. File
9. Function
10. Generic
11. Language
12. List
13. Map Multimap
14. Overload
15. Pointer
16. Queue Stack
17. Set Multiset
18. STL Algorithms Binary search
19. STL Algorithms Heap
20. STL Algorithms Helper
21. STL Algorithms Iterator
22. STL Algorithms Merge
23. STL Algorithms Min Max
24. STL Algorithms Modifying sequence operations
25. STL Algorithms Non modifying sequence operations
26. STL Algorithms Sorting
27. STL Basics
28. String
29. Valarray
30. Vector
Microsoft Office Word 2007 Tutorial
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
C++ » Overload » InserterScreenshots 
Output account info to a file using an inserter.
Output account info to a file using an inserter.


#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;

class account {
  int custnum;
  char name[80];
  double balance;
public:
  account(int c, char *n, double b
  {
    custnum = c;
    strcpy(name, n);
    balance = b;
  }
  friend ostream &operator<<(ostream &stream, account ob);
};

ostream &operator<<(ostream &stream, account ob
{
  stream << ob.custnum << ' ';
  stream << ob.name << ' ' << ob.balance;
  stream << '\n';

  return stream;
}

int main()
{
  account  Rex(1011"Joe Yin"12323.34);
  ofstream out("accounts", ios::out | ios::binary);

  if(!out) {
    cout << "Cannot open output file.\n";
    return 1;
  }

  out << Rex;

  out.close();

  return 0;
}



           
       
Related examples in the same category
1. Create a non-friend inserter.Create a non-friend inserter.
2. Use a friend inserter for objects of type MyClass.Use a friend inserter for objects of type MyClass.
3. This program draws right trianglesThis program draws right triangles
4. PhoneNumber inserter functionPhoneNumber inserter function
5. An inserter can be used to output data in any formAn inserter can be used to output data in any form
6. Use friend function to operator: <<, >>Use friend function to operator: <<, >>
ww__w__.__j__av___a_2s._com__ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.