Pages

Write program to store data in file object oriented approch.

Tuesday, 25 June 2013

#include <iostream>
#include <string.h>
#include <fstream>
using namespace::std;
class student
{
      string name;
      int age;
      public:
      student()     //constructor
      {
              
      }
      ~student()   //distructor
      {
               
      }
      void setData(string n, int a)     //setter function
      {
           name=n;
           age=a;
      }
      void save()// dada save in file
      {
           ofstream outfile;  // create object of of class
           outfile.open("abs.txt");  //open file inwhich data will be stored
           outfile<<"Your name is "<<name<<"\nYour age is "<<age<<endl;  // data is storing in file.
           cout<<"saved\n";
      }
      void Dispaly()                   //getter function
      {
           cout<<"Your name is "<<name<<"\nYour age is "<<age<<endl;
      }
};
int main()
{
       student a; // object of class student
       string name;
       int age;
       cout<<"Enter your name: ";
       getline(cin, name); //get complete line from user.
       cout<<"Enter your age: ";
       cin>>age;
       a.setData(name, age); // calling setter function
       a.save();
       a.Dispaly(); // calling getter function
       system("pause"); // hold screen untill user enter any character from screen
}
 

Search Box

Most Reading

Contact Form

Name

Email *

Message *