Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 25-10-2008
Member
 
Join Date: May 2008
Posts: 3
Class Object in C++

Since the class objects are the central elements of C++ programming, so the language support features for writing to and reading from the disk files object directly. The binary input and output functions read() and write() are designed to do the given task.

Code:
#include< fstream.h>
#include< iomanip.h>
#include< conio.h>
class detail
{
char name[10];
int code;
float cost;
public:
void readdata(void);
void writedata(void);
};
void detail:: readdata(void)//read from keyboard
{
cout<<"\nEnter name: ";
cin>>name;
cout<<"\nEnter code: ";
cin>>code;
cout<<"\nEnter cost: ";
cin>>cost;
}
void detail::writedata(void)//formatted display on the screen
{
cout<< setiosflags(ios::left)<< setw(10)<< name
<< setiosflags(ios::right)<< setw(10)<< code
<< setprecision(2)<< setw(10)<< cost
<< endl;
}
main()
{
clrscr();
detail item[3];//Declare array of3 objects
fstream file; //Input and Output file
file.open("details.dat",ios::in,ios::out);
cout<<" Enter Details For Three Items \n";
for(inti=0;i<3;i++)
{
item[i].readdata();
file.write((char*) &item[i],sizeof(item[i]));
}
file.seekg(0);//reset to start
cout<< "\nOUTPUT\n\n";
for(i=0;i<3;i++)
{
file.read((char*) &item[i], sizeof(item[i]));
item[i].writedata();
}
file.close();
}
The output of the program :

Enter name:xyz
Enter code:121
Enter cost:500
Enter name:mmm
Enter code:222
Enter cost:600
Enter name:nnn
Enter code:333
Enter cost:400


OUTPUT


xyz 121 500
mmm 222 600
nnn 333 400
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Class Object in C++"
Thread Thread Starter Forum Replies Last Post
Getting HP AIO Device object server register class object failed message on my system Donoho Hardware Peripherals 6 06-06-2011 01:34 AM
Instantiate object of a class New ID Software Development 5 08-03-2010 10:03 AM
Get Object Class Name At Runtime In Java Sheenas Software Development 5 22-02-2010 09:13 PM
How many ways to create an object for class Shanbaag Software Development 3 25-04-2009 10:08 AM
What is the Difference between Class and Object? RupaliP Software Development 5 28-02-2009 07:03 PM


All times are GMT +5.5. The time now is 05:06 AM.