Write a C++ program to create a class Department which contains data members as Dept_Id, Dept_Name, H.O.D., Number_Of_staff. Write necessary member functions to i. Accept details from user for ‘n’ departments and write it in a file “Dept.txt”. ii. Display details of department from a file. iii. Count the number of objects stored in a file.
//Note: If you are not using Borland or Turbo C then add the following line after header files :
//using namespace std;
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
class dept
{
int did,staff; //did= Dept-Id ,staff=no of staff
static int c; //c=count
char dname[10],hod[10]; //dname=Dept-Name , hod=HOD
public:
static void count()
{
c=c+1;
}
void accept()
{
cout<<"Enter Dept-Id: ";
cin>>did;
cout<<"Enter Dept-Name: ";
cin>>dname;
cout<<"Enter HOD: ";
cin>>hod;
cout<<"Enter No Of Staff: ";
cin>>staff;
ofstream out;
out.open("File.txt",ios::app);
out<<"\nDept-ID : "<<did;
out<<"\nDept-Name : "<<dname;
out<<"\nHOD : "<<hod;
out<<"\nNo Of Staff : "<<staff;
out.close();
}
void display()
{
char ch;
ifstream fin; // ifstream object is used to open a file for reading purpose only.
fin.open("File.txt");
while(!fin.eof()) //eof=end of file
{
ch=fin.get();
cout<<ch;
}
fin.close();
}
static void dis()
{
cout<<"\nTotal object are : "<<c<<endl;
}
};
int dept::c;
int main()
{
int n,i,ch;
do{
cout<<"\n1.Accept Details\n2.Display\n3.Count no of objects\n4.Exit\nEnter your choice :- ";
cin>>ch;
switch(ch)
{
case 1:cout<<"Enter how many department you want to enter :- ";
cin>>n;
dept d[20];
for(i=0;i<n;i++)
{
d[i].accept();
d[i].count();
}break;
case 2:for(i=0;i<n;i++)
{
d[i].display();
}break;
case 3:d[n-1].dis(); break;
}
}while(ch!=4);
return(0);
}
0 Comments