Write a programme in C++ to write and later retrieve objects

Q. Write a programme in C++ to write and later retrieve objects to form file. 

Ans.

# include

# include < c type. h >                      // for trouper

# include < string. h >             // for strlen

# define MAX NAME 40

Class person

{

Private :

Char name [MAX NAME];

Int age;

Public :

   // this functions writes the class's data members to the file void write (of stream & of s)

{

Of. S write (name, strlen (name));

Of a << ends;

Of s write ((char *) & age, size of (age));

}

     // this functions reads the class's data members from the file.

// it returns non zero if no errors were encountered while reading

Int read (if stream & is)

{  is. Get (name, MAX NAME,0);

Name [ is. G count () ] =  0;

    Is. Ignore (1);   // ignore the NULL terminator in the file.

Is. Read (( char * ) & age, size of (age));

Return is. good ();

  }

// stream operator, << overloading

Friend function & operator << (of stream & os, person & b); 

 // stream operator, << operator overloading

Friend function & operator << (is stream & os, person & b);            

 // output file stream operator << (of stream & fos, person & b)

{

b. write (fos);     return fos;

}

// output file stream operator overloading

Friend function of stream & operator << (of stream & fos, person & b)

{

b. write (fos);   return fos;

}

// output file stream operator overloading

Friend function of stream & operator << (if stream & fos, person & b)

b. read (fos);   return fos

}

};

I stream & operator >> (i stream & fos, person & b)

{

Cout << "Name:",

Is << ws; // flush input buffer

Is. Get (b. Name, MAX NAME);

Cout << "Age :" ;

 Is >> ws >> b. Age;

Return is;

}

Of stream & operator << (o stream & os, person & b)

{

Os << b. Name << end l;

  Os << b. age << end l;

   Return os;

}

Void main ()

{

   Related Questions in Programming Languages

  • Q : What are good examples of element

    What are good examples of element attributes?

  • Q : Explain UpdatePanel control Briefly

    Briefly explain about the UpdatePanel control.

  • Q : What is Transmission Control Protocol

    Transmission Control Protocol: It is the Transmission Control Protocol (abbreviated as TCP) is a set of rules which permit reliable communication among two processes across a network.

  • Q : Chown Explain the chown, chmod and

    Explain the chown, chmod and chgrp?

  • Q : Explain Information hiding Information

    Information hiding: The practice of making sure that only as much information is exposed regarding the implementation of a class as is strictly needed. Hiding needless knowledge of implementation makes it not as much of likely that other classes will

  • Q : What is Shallow copy Shallow copy : It

    Shallow copy: It is a copy of an object in which copies of each and every object's sub-components are not as well made. For example, a shallow copy of an array of objects would outcome in two separate array objects, each having references to similar s

  • Q : Define Compilation Compilation : It is

    Compilation: It is a process of translating a programming language. This frequently comprises translating a high level programming language into a low level programming language, and the binary form of a specific instruction set. The translation is ex

  • Q : Transport for XML Web Services What is

    What is the transport for XML Web Services? How can end-to-end security be provided for this transport?

  • Q : Asynchronous and synchronous postback

    Differeniate asynchronous postback and synchronous postback?

  • Q : Explain Relative filename Relative

    Relative filename: It is a filename whose full path is associative to some point within a file system tree-frequently the present working folder (that is, directory). For example:   

©TutorsGlobe All rights reserved 2022-2023.