代理模式

婚介所

婚介所其实就是找对象的一个代理,请仿照我们的课堂例子“论坛权限控制代理”完成这个实际问题,其中如果年纪小于18周岁,婚介所会提示“对不起,不能早恋!”,并终止业务。

 

#include <iostream>

#include <string>

using namespace std;

 

 

class Person

{

public:

    string name;

    int age;

    Person(string n,int a)

    {

        name=n;

        age=a;

    }

};

 

class Center

{

protected:

    string name;

public:

    Center(string n){name=n;}

    virtual void findlove(Person* person)=0;

};

 

 

class Marry:public Center

{

public:

    Marry(string name):Center(name){}

    void findlove(Person* person)

    {

        if(person->age<18)

        {

            cout <<name<<":"<<person->name<<",对不起!不能早恋!" << endl;

        }

        else if(person->age<100)

        {

            cout <<name<<":"<<person->name<<",我们会为您找到合适的伴侣!" << endl;

        }

        else

        {

            cout <<name<<":"<<person->name<<",对不起!您年龄太大了!" << endl;

        }

    }

};

int main()

{

    Center* center1,* center2;

    center1=new Marry("婚介");

    center2=new Marry("婚介");

    Person* b1=new Person("1",23);

    Person* b2=new Person("2",19);

    center1->findlove(b1);

    center2->findlove(b2);

    return 0;

}

posted @ 2024-11-22 14:11  涨涨涨张  阅读(2)  评论(0编辑  收藏  举报