--%>

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 : Generate source code for an ASP.NET Web

    What is the possibility to generate the source code for an ASP.NET Web service by a WSDL? Answer: The Wsdl.exe tool (as .NET Framework SDK) can be utilized to genera

  • Q : Define the term Marking interface

    Define the term Marking interface: It is an interface with no methods.

  • Q : Explain Singleton pattern Singleton

    Singleton pattern: It is a pattern which permits us to make sure that only a single instance of a specific class exists at any one time. Such an instance is termed as singleton. The pattern can as well be employed whenever instances would have no excl

  • Q : Automaton distributed in the class

    Write a code in a c++/java  for the automaton distributed in the class which accepts keywords(cat,bat,cab). Create an input file with these words may be two or three copies of these words scattered in a paragraph and show that your program does accept these words and gives an output to that

  • Q : What is Hostname What is Hostname : It

    What is Hostname: It is the name of a host system.

  • Q : Explain Static variable Static variable

    Static variable: It is a static variable defined within a class body. Such a variable fits in to the class as a whole, and is, thus, shared by all objects of the class. The class variable may be employed to define the default value of an instance vari

  • 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 : Problem on Vectors Create a vector

    Create a vector representing x coordinates of a measurement with 20 points between 0 and 10. Create another vector y representing fake measurements which are related to the above x values as y = 2.3 x – 1.2. Next add random (normal, Gaussian) noise to the vector

  • Q : Explain the meaning of semantic

    Explain the meaning of semantic connotations.

  • Q : Define several features of XQuery

    Define several features of XQuery?