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 : State the term an XHTML Element State

    State the term an XHTML Element?

  • Q : State the term snooping State the term

    State the term snooping?

  • Q : Define the term Multiprogramming system

    Define the term Multiprogramming system: It is an operating system which is able to run multiple programs parallel.

  • Q : What are collections and generics What

    What are collections and generics?

  • Q : Explain the way to write the opening

    Explain the way to write the opening tag of an XHTML element.

  • Q : What is Disk drive Disk drive : It is a

    Disk drive: It is a hardware device employed to store the data. They come in numerous forms, like floppy disks, compact disks, and hard disks.

  • Q : What is Runtime stack Runtime stack :

    Runtime stack: It is a stack structure maintained by the Java Virtual Machine which records that methods are presently being executed. The most of late entered technique will be at the top of the stack and the main technique of an application will be

  • Q : Explain Global variable Global variable

    Global variable: It is a phenomenon which is more generally regarded as being a trouble in structured programming languages than in object-oriented languages. In structured programming language, like C or Pascal, a global variable is one stated outsid

  • Q : What are tools require View XHTML

    What are tools require View XHTML Documents?

  • Q : Use Polymorphism Use Polymorphism :

    Use Polymorphism: Programs, especially those converted from non-OOP languages like C, sometimes use state where they should use inheritance. For example,

    Discover Q & A

    Leading Solution Library
    Avail More Than 1454180 Solved problems, classrooms assignments, textbook's solutions, for quick Downloads
    No hassle, Instant Access
    Start Discovering

    18,76,764

    1941397
    Asked

    3,689

    Active Tutors

    1454180

    Questions
    Answered

    Start Excelling in your courses, Ask an Expert and get answers for your homework and assignments!!

    Submit Assignment

    ©TutorsGlobe All rights reserved 2022-2023.