C ++通过map和使用方法从基类实现派生类

问题描述 投票:-1回答:1

我面临的问题是我要创建抽象类和4个子类。抽象类= Entry,其他子类在代码中给出。

class entry {
    friend ostream &operator<<(ostream &os, const entry &obj);

private:
    static constexpr char *def_info = "Undefind";
protected:
    string desc;
    int ID;
    static int counter;
public:
    entry() : entry(def_info) {}

    entry(string input_s);

    void setDesc(string);

    string getDesc() const;

    int getID() const;

    virtual string getContents() const = 0;

    virtual void displaying(ostream &os) const = 0;

    virtual ~entry() = default;
};

int entry::counter = 0;

entry::entry(string input_s) :
        desc{input_s} {
    ++counter;
    ID = counter;
}

ostream &operator<<(ostream &os, const entry &obj) {
    os << obj.getContents()<<endl;
    obj.displaying(os);
    return os;
}

void entry::setDesc(string input) {
    desc = input;
}

string entry::getDesc() const {
    return desc;
}

int entry::getID() const {
    return ID;
}

//PhoneEntry extending the Entry with a phone number
class phone_entry :virtual  public entry {
private:
    static constexpr char *text = "Undefind";
    static constexpr int def_no = 48000000000;
protected:
    int phone_number;
public:
    phone_entry(string input_s = text, int input = def_no) :
            entry(input_s), phone_number(input) {}

    virtual string getContents() const override {
        ostringstream output;
        output << "ID: " << ID << "\nD: " << desc << "\nPhone number : " << phone_number;
        return output.str();
    }

    virtual ~phone_entry() = default;
};

//EmailEntry extending the Entry with an e-mail addres
class email_entry : virtual public entry {
private:
    static constexpr char *def_info = "Undefind";

protected:
    string email;
public:
    email_entry(string des = def_info, string email_entry = def_info) :
            entry(des), email{email_entry} {}

    virtual string getContents() const override {
        ostringstream output;
        output << "ID: " << ID << "\nD: " << desc << "\nEmail : " << email;
        return output.str();
    }

    virtual ~email_entry() = default;
};
//AddressEntry extending the Entry with an address containing a city, a street and a house number

class address_entry : virtual public entry {
private:
    static constexpr char *def_info = "Undefind";
    static constexpr int def_no = 0;
protected:
    string city;
    string street;
    int house_number;

public:

    address_entry(string des = def_info, string c = def_info, string s = def_info, int hn = def_no) :
            entry{des}, city{c}, street{s}, house_number{hn} {}

    virtual string getContents() const override {
        ostringstream output;
        output << "ID: " << ID << "\nD: " << desc << "\nCity: " << city << "\nStreet: " << street << "\nHouse number: "
               << house_number;

        return output.str();
    }

    virtual ~address_entry() = default;
};

class contact_book : virtual public entry {
    static constexpr char *def_info = "Undefind";
private:
    map<string,entry *> contacts;
    string nick_name;
public:
    class error_mesg : public logic_error {
    public:
        error_mesg(const string message = "NULL") :
                logic_error(message) {}
    };

    contact_book(string e = "undefind") :
            entry{def_info},nick_name{e} {}

    void add(string a , entry &content){
        contacts.insert(make_pair(a,&content));
    }


    virtual void displaying(ostream &os) const override {
        os << "TESTING";
    }

};

我想使用contact_book类并使用map容器将键和值插入任何子类,如("cool", new phone_entry("NAME",000000),使用add方法,它有两个参数,一个用于键,另一个必须连接到其他类,所以我认为使用条目obj将完成作业,我在成员中使用指针,因为我想使用多态性。我也不知道如何使用getContencts,它知道从哪个子类调用。例如,他问主要看起来像这样:

    ContactBook contacts(“My contacts”);
contacts.add(“Johny”, new PhoneEntry(“John Smith”, 100200300));
contacts.add(“Lisa”, new EmailEntry(“Lisa Wood”, “[email protected]”));
contacts.add(“Andy”, new AddressEntry(“Andrew Fox”, “Warsaw”, “Green St.”, 7));
cout << contacts;
//result (ordered):
//Andrew Fox: Warsaw, Green St. 7 (pos. 3)
//John Smith: phone 100200300 (pos. 1)
//Lisa Wood: e-mail [email protected] (pos. 2)
try {
 cout << contacts[“Andy”].getContents() << endl;
 //result:
 //Andrew Fox: Warsaw, Green St. 7 (pos. 3)

你能帮我解决一下如何实现它们并得到他想要的东西,以及如何和将会在主要功能上添加方法和新功能。

c++ oop polymorphism abstract-class implementation
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.