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 : Problem on COBOL source code errors

    There are many errors in the following COBOL source code. Identify the errors and rewrite the program so that it contains no errors:IDENTIFICATION DIVISION   (Full stop required)PROGRAM ID. ERRORS-EXAMPLE.DATA DIVISION    (Fu

  • Q : Define Keyword Keyword : They are

    Keyword: They are reserve word containing special meaning of the language and cannot be employed as identifier.

  • Q : Describe Method result Method result :

    Method result: The value returned from a method through a return statement. The kind of expression in the return statement should match the return type declared in method header.

  • Q : What is Stack overflow Stack overflow :

    Stack overflow: Stack overflow takes place whenever too many items are pushed onto a stack with a finite or limited capacity.

  • Q : Explain the term Latest XHTML

    Explain the term Latest XHTML Specification?

  • Q : Define Livelock Livelock : It is a

    Livelock: It is a situation in which a thread waits to be notified of a condition however, on waking, finds that the other thread has inverted the condition another time. The primary thread is forced to wait again. Whenever this occurs for an indefini

  • Q : What is Application programming

    Application programming interface (API): It is a set of definitions that you can make use of it in writing programs. In the perspective of Java, these are the classes, packages, and interfaces which can be utilized to build complex ap

  • Q : What is Member What is Member : Members

    What is Member: Members of a class are methods, fields and nested classes.

  • Q : Explain Operator Explain Operator with

    Explain Operator with their types and examples?

  • Q : What is Syntax error Syntax error: It

    Syntax error: It is an error detected by the compiler throughout its parsing of a program. The syntax errors generally result from mis-ordering symbols in statements and expressions. Missing curly semicolons and brackets are general illustrations of s

©TutorsGlobe All rights reserved 2022-2023.