阅读下列说明和C++代码,填补代码中的空缺,将解答填入答题纸的对应栏内。
【说明】
以下C++代码实现一个简单客户关系管理系统(CRM)中通过工厂(Customerfactor y)对象来创建客户(Customer)对象的功能。客户分为创建成功的客户(realCustom er)和空客户(NullCustomer)。空客户对象是当不满足特定条件时创建或获取的对象。类间关系如图6-1所示。
 2017

【C++代码】
#include<iostream>
#include<string>
using namespace std;
class Customer {
protected:
string name;
public:
    _(1)_bool isNil(=0;
    ( 2 )string getName()=0;
class RealCustomer(_3)
{
public:
    realCustomer(string name) {this->name=name;}
    bool isNil() { return false;}
    string getName() { return name;}
}

class NullCustomer( 4){
public:
    bool isNil() {
        return true;
    }
    string getName()
    {
        return "Not Availabl e in Customer Database";
    }
};


class Customerfactory {
public:
    string names [3] = {"rob", "Joe","Julie");
public:
    Customer*getCustomer(string name){
    for (int i=0;i<3;i++)
    {
        if (names[i].(5 ).) 
        {
            return new realCustomer(name);
        }
        return(_6_);
    }
};

class CRM{
public:
void getCustomer()
{
    Customerfactory* (7);
    Customer*customer1=cf->getCustom er("Rob");
    Customer*customer2=cf->getCustom er("Bob");
    Customer*customer3=cf->getCustom er("Julie");
    Customer*customer4=cf->getCustom er("Laura");
    cout<<"Customers"<<endl;
    cout<<Customer1->getName()<<endl; delete customer1;
    cout<<Customer2->getName()<<endl; delete customer2;
    cout<<Customer3->getName()<<endl; delete customer3;
    cout<<Customer4->getName()<<endl; delete customer4;
    delete cf;
}

int main( )
{
    CRM*crs=new CRM();
    crs->getCustomer();
    delete crs;
    return 0;
}

/*程序输出为:
Customers
rob
Not Available in Customer Database
Julie
Not Available in Customer Database
*/


参考答案:
1) virtual
2) virtual
3) :public Customer
4) :public Customer

5) compare(name)==0//备注,考察string类的compare方法,相等返回0

6) new Null Customer()//null类,不是null,因为要打印输出Not Available in Customer Database

(7) cf=New CustomeFactory()

所属知识点:

C++程序设计>C++程序设计

 

解析

本题考查使用C++代码实现实际问题。在C++中,动态绑定是通过虚函数来实现的。此题中用到了虚函数,所以要在成员函数原型前加一个关键字virtual。

类RealCustomer和类NullCustomer是类Cus tomer的派生类,因此3、4空都填public Cu stomer。

进行对比数据库中的名compare(name)==0

第6空与前面语句是相反的,一个是返回new RealCustomer(name),那么此处应填: new Null Customer()

第7空,用工厂创建对象,cf=New CustomeFactory()

 

 

Logo

火山引擎开发者社区是火山引擎打造的AI技术生态平台,聚焦Agent与大模型开发,提供豆包系列模型(图像/视频/视觉)、智能分析与会话工具,并配套评测集、动手实验室及行业案例库。社区通过技术沙龙、挑战赛等活动促进开发者成长,新用户可领50万Tokens权益,助力构建智能应用。

更多推荐