My instructor at my engineering institute has given the following assignment question:
Create a database of scientists and labourers and maintain the same by making use of inheritance.
We haven't been taught the database connectivity with program execution, so that's not to be used. I wrote the following code:
#include<iostream.h>
class employee
{
char name[20];
long id;
int age;
char gender;
char address[30];
char phone[3][10];
public:
employee();
virtual void accept();
virtual void display();
};
employee::employee()
{
name[0]='\0';
id=0;
age=0;
gender='\0';
address[0]='\0';
for(int i=0;i<3;i++)
phone[i][0]='\0';
}
void employee::display()
{
cout<<endl<<"name:"<<name;
cout<<endl<<"id:"<<id;
cout<<endl<<"age:"<<age;
cout<<endl<<"gender:"<<gender;
cout<<endl<<"address:"<<address;
cout<<endl<<"phone number:";
for(int i=0;i<3;i++)
{
if(phone[i]=='\0')
{
break;
}
cout<<endl<<"phone "<<i+1<<" : "<<phone[i];
}
}
void employee::accept()
{
int phone_count;
cout<<endl<<"enter the name:";
cin>>name;
cout<<endl<<"enter the id:";
cin>>id;
cout<<endl<<"enter the age:";
cin>>age;
cout<<endl<<"enter the gender:";
cin>>gender;
cout<<endl<<"enter the address:";
cin.getline(address,29);
cout<<endl<<"enter the number of phone number:";
cin>>phone_count;
for(int i=0;i<phone_count;i++)
{
cout<<endl<<"phone "<<i<<" : ";
cin>>phone[i];
}
}
//-----------------------------------------------------------------
class scientist:public employee
{
char qual[30];
char field[20];
int exp;
public:
scientist()
{
qual[0]='\0';
field[0]='\0';
exp=0;
}
void accept();
void display();
};
void scientist::accept()
{
employee::accept();
cout<<endl<<"enter the qualification:";
cin.getline(qual,29);
cout<<endl<<"enter the field:";
cin.getline(field,19);
cout<<endl<<"enter the experience:";
cin>>exp;
}
void scientist::display()
{
employee::display();
cout<<endl<<"Qualification : "<<qual;
cout<<endl<<"Field : "<<qual;
cout<<endl<<"Experience : "<<qual;
}
//-----------------------------------------------------------------
class labourer:public employee
{
char job[10];
int hours;
double pay;
public:
labourer()
{
job[0]='\0';
hours=0;
pay=0;
}
void accept();
void display();
};
void labourer::accept()
{
employee::accept();
cout<<endl<<"enter the job:";
cin.getline(job,9);
cout<<endl<<"enter the hours:";
cin>>hours;
cout<<endl<<"enter the pay:";
cin>>pay;
}
void labourer::display()
{
employee::display();
cout<<endl<<"Job : "<<job;
cout<<endl<<"Hours : "<<hours;
cout<<endl<<"Pay : "<<pay;
}
//-----------------------------------------------------------------
int main()
{
employee *p;
cout<<endl<<"enter 1 for scinetisi , 2 for labourer :";
int i;
cin>>i;
if(i==1)
{
p=new scientist;
}
else if(i==2)
{
p=new labourer;
}
p->accept();
p->display();
cout<<endl;
return 0;
}
In my code I have used inheritance and it stores and displays details of scientist and labourer, but still it isn't a database as it stores only one record that too of either of the two entities. Any suggestions to my problem?