以下是我的任务中的问题:
创建一个类telephone_directory(姓名,职业,电话,地址)和一个 虚函数search(),以线性方式搜索对象内容。延伸 特定班级中的一个叫做医生(资格,诊所,访问时间)和 覆盖虚函数,使其使用二进制搜索进行搜索。也创造 另一名律师(资格,民事/刑事,案件,contact_no, office_address)扩展了telephone_directory,使其不会覆盖 虚函数。通过从用户那里获得选项来搜索医生和律师。
以下是我到目前为止的尝试:
/*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);
}
在这里,我无法完成部分&#34;因此它不会覆盖虚拟功能&#34;。请告诉我如何在我的程序中应用它。
答案 0 :(得分:0)
有几个问题:
1. You have to make the search method virtual in Telephone class
virtual void search(telephone* a);
then implement it to search in linear fashion.
2. Override this method in doctor
virtual void search(telephone* a) override; // note the parameter type, it is telephone*
then implement it to search in binary.
3. Don't override the method in lawyer class, so that it uses the default implementation of telephone class.
覆盖/动态绑定的强大之处在于,在运行时,将根据对象类型调用正确的search()方法。如果是电话*或律师*,将调用默认实现(因为laywer不会覆盖它)。如果是医生*,将在医生课上实施。
答案 1 :(得分:0)
您的基类telephone
类没有虚拟搜索功能。你的声明是
void search(telephone *a);
但是, 应该
virtual void search(telephone *a);
这告诉编译器当你从这个类派生另一个类时,你想要用新的一个覆盖电话的搜索功能,这个功能在你的下面重新实现(在你的情况下使用二进制搜索)。衍生类。
因此...
1)在电话类声明之前添加虚拟
2)创建标准搜索功能实现
3)为医生类
中的search()类创建另一个实现(二进制搜索)完成!