Following is the question from my assignment :
Create a class telephone_directory (name, occupation, phone, address) and an
virtual function search (), that searches the object contents in a linear fashion. Extend
the class in a specific class called doctor (qualification, clinic, visiting time) and
override the virtual function such that it searches using binary search. Also create
another class lawyer (qualification, civil/criminal, cases attended, contact_no,
office_address) that extends the telephone_directory such that it wont override the
virtual function. Search for a doctor and a lawyer by taking the option from the user.
Following is my attempt so far :
/*18. Create a class telephone_directory (name, occupation, phone, address) and an
virtual function search (), that searches the object contents in a linear fashion. Extend
the class in a specific class called doctor (qualification, clinic, visiting time) and
override the virtual function such that it searches using binary search. Also create
another class lawyer (qualification, civil/criminal, cases attended, contact_no,
office_address) that extends the telephone_directory such that it wont override the
virtual function. Search for a doctor and a lawyer by taking the option from the user.
(m)*/
#include<iostream>
#include<string>
#include<algorithm>
#include<stdlib.h>
#include<conio.h>
using namespace std;
static int size;
class telephone
{
public:
string name, address;
int phone;
telephone();
virtual void search(telephone *a);
void display();
};
class doctor : public telephone
{
public:
string qualification, visit, clinic, occupation;
doctor();
void search(doctor *a);
void display();
};
class lawyer : public telephone
{
public:
string qualification, type, cases, office, occupation;
void display();
void search(lawyer *a);
lawyer();
};
bool sortByName(const telephone &lhs, const telephone &rhs) { return lhs.name < rhs.name; }
void telephone :: display()
{
cout << "\nName : " << name << "\nAddress : " << address << "\nPhone : " << phone << endl;
}
telephone :: telephone()
{
string n[13] = {"Bill","Roy","Drake","Robbie","Elvis","John","Sara","Kate","Lauren","Julia","Nigella","Gordon","Ellen"};
string add[5] = {"Delhi", "Chennai", "Kolkata", "Mumbai", "Vellore"};
name = n[rand() % 13];
address = add[rand() % 5];
phone = 1000 + rand () % 5000;
}
void telephone :: search(telephone *a)
{
string n, add;
int tel;
int i = 0, j, com, flag;
cout << "\nEnter name\n";
cin >> n;
flag = 0;
for(j = 0; j < size; j++)
{
com = n.compare(a[j].name);
if(com == 0)
{
flag = 1;
a[j].display();
}
}
if(flag == 0)
{
cout << "\nNo matches were found\n";
}
}
doctor :: doctor()
{
occupation = "Doctor";
string q[4] = {"Intern", "Resident", "Attending", "Fellow"};
string v[4] = {"4 to 5", "5 to 6", "6 to 7", "7 to 8"};
string c[4] = {"General", "Free", "Charity", "Specialist"};
visit = v[rand() % 4];
clinic = c[rand() % 4];
qualification = q[rand() % 4];
}
void doctor :: display()
{
cout << "\nName : " << name << "\nOccupation : " << occupation << "\nAddress : " << address << "\nPhone : " << phone << "\nQualification : " << qualification << "\nClinic : " << clinic << "\nVisiting Hours : " << visit << endl;
}
void doctor :: search(doctor *a)
{
string n;
int i = 0, j, com, flag, first, last, middle;
cout << "\nEnter name\n";
cin >> n;
flag = 0;
first = 0;
last = size - 1;
middle = (first + last) / 2;
sort(a, a + size, sortByName);
while ( first <= last )
{
middle = (first + last) / 2;
if(a[middle].name < n)
first = middle + 1;
else if ( a[middle].name > n)
last = middle - 1;
else
{
flag = 1;
a[middle].display();
break;
}
}
if(flag == 0)
{
cout << "\nNo matches were found\n";
}
}
lawyer :: lawyer()
{
string q[5] = {"Associate", "Senior Associate", "Junior Partner", "Senior Partner", "Manager"};
string t[2] = {"Civil", "Criminal"};
string c[5] = {"40", "70", "90", "Fresh", "No experience"};
string o[3] = {"Vellore", "Chennai", "Delhi"};
qualification = q[rand() % 5];
type = t[rand() % 2];
cases = c[rand() % 5];
office = o[rand() % 3];
occupation = "Lawyer";
}
void lawyer :: display()
{
cout << "\nName : " << name << "\nOccupation : " << occupation << "\nAddress : " << address << "\nPhone : " << phone << "\nQualification : " << qualification << "\nType : " << type << "\nCases Attended : " << cases << "\nOffice Address : " << office << endl;
}
void lawyer :: search(lawyer *a)
{
string n;
int i = 0, j, com, flag, first, last, middle;
cout << "\nEnter name\n";
cin >> n;
flag = 0;
first = 0;
last = size - 1;
middle = (first + last) / 2;
sort(a, a + size, sortByName);
while ( first <= last )
{
middle = (first + last) / 2;
if(a[middle].name < n)
first = middle + 1;
else if ( a[middle].name > n)
last = middle - 1;
else
{
flag = 1;
a[middle].display();
break;
}
}
if(flag == 0)
{
cout << "\nNo matches were found\n";
}
}
main()
{
int i = 0, k;
doctor *doc;
lawyer *law;
cout << "\nEnter the size\n";
cin >> size;
doc = new doctor [size];
law = new lawyer [size];
do
{
cout << "\n1.) List the doctors\n2.) List the lawyers\n3.) Search for a doctor by name\n4.) Search for a lawyer by name\n5.) Exit\n";
cin >> i;
switch(i)
{
case 1:
cout << "\nDOCTORS LIST\n\n";
for(i = 0; i < size; i++)
doc[i].display();
break;
case 2:
cout << "\nLAWYERS LIST\n\n";
for(i = 0; i < size; i++)
law[i].display();
break;
case 3:
doc[0].search(doc);
break;
case 4:
law[0].search(law);
break;
case 5:
break;
}
} while(i != 5);
}
Here I am not able to do the part "such that it won't override the virtual function". Please tell me how will I be able to apply this in my program.