C++通过映射并使用中的方法从基类实现派生类

C++ implementing derived class from base class by map and using methods from

本文关键字:方法 基类 实现 派生 映射 C++      更新时间:2023-10-16

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

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容器通过使用add方法将键和值插入到任何子类(如("cool", new phone_entry("NAME",000000))中,该方法有两个参数,一个用于键,另一个必须与其他类连接,所以我认为使用条目obj将完成在成员中使用指针的工作,因为我想使用多态性。我也不知道如何使用getContencts,它知道从哪个子类调用。例如,他问主界面会是这样的:

ContactBook contacts(“My contacts”);
contacts.add(“Johny”, new PhoneEntry(“John Smith”, 100200300));
contacts.add(“Lisa”, new EmailEntry(“Lisa Wood”, “lisa@gmail.com”));
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 lisa@gmail.com (pos. 2)
try {
cout << contacts[“Andy”].getContents() << endl;
//result:
//Andrew Fox: Warsaw, Green St. 7 (pos. 3)

你能帮我弄清楚如何实施它们,得到他想要的东西,以及如何&将在main函数中处理该添加方法和new。

您不太清楚实际问题是什么–所以我认为你很难获得整个通讯录所需的输出。。。

首先,还有其他一些问题:

contact_book继承entry是没有意义的——这本书包含条目,它不是一个。似乎您只继承了能够重写displaying函数,从而能够为entry类使用已经定义的operator<<。但这是一个不好的继承理由。相反,您应该为您的通讯录提供单独的operator<<过载:

std::ostream& operator(std::ostream& s, contact_book const& cb); // coming to later

然后习惯于在适当的地方重用代码;我假设getContents应该输出将通过operator<<写入std::cout的相同字符串。好的,那么让我们从中获利

std::string entry::getContents()
{
std::ostringstream s;
s << *this;
return s.str();
}

你不需要再让它虚拟化了(也不应该)。

我个人会使用displaying函数的默认值(不过,这不是一个很好的名称,你应该更喜欢命令式的"display",也许更好:"printToStream"或只是"printTo"):

void entry::printTo(std::ostream& s)
{
// output members to s 
}

然后您的继承类可以重用它:

void phone_entry::printTo(std::ostream& s)
{
entry::printTo(s); // use parent's version first to print common data
// now append phone number to s... 
}

不过,用户应该使用operator<<而不是printTo函数。保护它(甚至是私有的)可能是个好主意。

最后返回到输出联系人簿:

std::ostream& operator(std::ostream& s, contact_book const& cb)
{
// you want to output every entry? then do so:
for(auto& e : cb.contacts)
{
s << *e.second << std::endl;
//     ^^^^^^^ map entries are of type std::pair<key_type const, mapped_type>
//   ^ you stored pointers, operator<< accepts reference
}
}

同样,您将重新使用已经编写的代码。

最后一点:您混合了经典初始化(括号)和统一初始化(大括号),一次甚至在一行中(email_contact):

entry(des), email{email_entry}

我个人认为UI通常有一些有效的推理,不过它在C++中的实现方式已经被打破了。还有很多其他问题,我最喜欢的是:

std::vector<int> v0{7};   // gives you a vector with 7 elements, all 0!!!
// with classic initialization:
std::vector<int> v1(7);   // now clear, you WANT such a vector!
std::vector<int> v2({7}); // now clear, you want initializer list

你现在可以按照我的推理行事(其他人选择使用UI是为了从中获利),但无论你做什么决定——请在整个文件/项目中始终如一地!