--%>

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 : Define Single line comment Single line

    Single line comment: A comment is in the form:     // this line will be avoided by the compiler.

  • Q : Explain Increment operator Increment

    Increment operator: The operator (++) which adds one to its operand. It consists of two forms: pre-increment (++x) and post-increment (x++). In its pre-increment form, the outcome of the expression is the value of its argument subsequent to the increm

  • Q : Explain Look-and-feel Look-and-feel :

    Look-and-feel: The visual impression and interaction style given by a user interface. This is mainly the responsibility of the window manager (that is, in collaboration with the fundamental operating system) running on a specific computer. This refers

  • Q : Difference between collection and arrays

    Write the difference between collection and arrays?

  • Q : Difference between JavaScript and AJAX

    Write basic difference between JavaScript and AJAX ?

  • Q : Generating uniform random numbers using

    In .NET, write a simple web service in C# to generate uniform random numbers. Use the class System.Random to generate the random numbers. What part of the code is specifically associated with web services? How could you deploy the web service?

  • Q : Define Multiple Inheritance Multiple

    Multiple Inheritance: In C++, multiple-inheritance occurs when a class inherits from more than one parent. For example: 524_mult</span></p>
                                        </div>
                                        <!-- /comment-box -->
                                    </li>
   
   </td>
	</tr><tr>
		<td>
       
      <li>
                                        <div class=

    Q : What is File Transfer Protocol or FTP

    File Transfer Protocol: The File Transfer Protocol (abbreviated as FTP) states a standard set of rules which make it probable to transfer a file from one file system to the other.

  • Q : Define the term Inheritance Inheritance

    Inheritance: It is a feature of object-oriented programming languages in which a sub-type inherits methods and variables from its super-type. The Inheritance is most generally employed as a synonym for class inheritance {class!inheritance}, however in