C++ 通讯录管理系统:使用类和继承实现

这篇文章将使用 C++ 实现一个简单的通讯录管理系统。我们将利用面向对象编程的概念,如类、继承和多态来设计和构建这个系统。

代码解析cpp#include #include #include #include

using namespace std;

// 基类:联系人class Contact {protected: string name; string pinyin; string nickname; string address; string birthday; string lunarBirthday; string relationship; string note; string company; string position; string phoneNumber; string occupation;

public: Contact(string n) : name(n) { pinyin = ''; nickname = ''; address = ''; birthday = ''; lunarBirthday = ''; relationship = ''; note = ''; company = ''; position = ''; phoneNumber = ''; occupation = ''; }

virtual void display() {        cout << '姓名:' << name << endl;        cout << '姓名拼音:' << pinyin << endl;        cout << '即时信息:' << nickname << endl;        cout << '地址:' << address << endl;        cout << '生日:' << birthday << endl;        cout << '农历生日:' << lunarBirthday << endl;        cout << '关系:' << relationship << endl;        cout << '备注:' << note << endl;        cout << '公司:' << company << endl;        cout << '职位:' << position << endl;        cout << '手机号码:' << phoneNumber << endl;        cout << '工作:' << occupation << endl;    }

string getName() const {        return name;    }

void setPinyin(string py) {        pinyin = py;    }

void setNickname(string nn) {        nickname = nn;    }

void setAddress(string addr) {        address = addr;    }

void setBirthday(string bday) {        birthday = bday;    }

void setLunarBirthday(string lbday) {        lunarBirthday = lbday;    }

void setRelationship(string rel) {        relationship = rel;    }

void setNote(string nt) {        note = nt;    }

void setCompany(string cmp) {        company = cmp;    }

void setPosition(string pos) {        position = pos;    }

void setPhoneNumber(string phone) {        phoneNumber = phone;    }

void setOccupation(string occ) {        occupation = occ;    }

bool operator<(const Contact& other) const {        return pinyin < other.pinyin;    }};

// 派生类:亲属联系人class RelativeContact : public Contact {public: RelativeContact(string n) : Contact(n) {}

void display() {        cout << '---亲属联系人---' << endl;        Contact::display();    }};

// 派生类:同事联系人class ColleagueContact : public Contact {public: ColleagueContact(string n) : Contact(n) {}

void display() {        cout << '---同事联系人---' << endl;        Contact::display();    }};

// 派生类:同学联系人class ClassmateContact : public Contact {public: ClassmateContact(string n) : Contact(n) {}

void display() {        cout << '---同学联系人---' << endl;        Contact::display();    }};

// 派生类:教师联系人class TeacherContact : public Contact {public: TeacherContact(string n) : Contact(n) {}

void display() {        cout << '---教师联系人---' << endl;        Contact::display();    }};

// 通讯录类class AddressBook {private: vector<Contact*> contacts;public: AddressBook() {}

~AddressBook() {        for (auto contact : contacts) {            delete contact;        }    }

void addContact(Contact* contact) {        contacts.push_back(contact);    }

void displayContacts() {        cout << '---通讯录---' << endl;        for (auto contact : contacts) {            contact->display();            cout << '--------------------------------------' << endl;        }    }

void sortByPinyin() {        sort(contacts.begin(), contacts.end(), [](Contact* a, Contact* b) {            return a->getName() < b->getName();        });    }

void searchByInitial(char initial) {        cout << '---以 ' << initial << ' 开头的联系人---' << endl;        for (auto contact : contacts) {            if (toupper(contact->getName()[0]) == toupper(initial)) {                contact->display();                cout << '--------------------------------------' << endl;            }        }    }};

int main() { AddressBook addressBook;

Contact* relative1 = new RelativeContact('张三');    relative1->setPinyin('Zhang San');    relative1->setNickname('三哥');    relative1->setPhoneNumber('1234567890');    addressBook.addContact(relative1);

Contact* colleague1 = new ColleagueContact('李四');    colleague1->setPinyin('Li Si');    colleague1->setNickname('四哥');    colleague1->setPhoneNumber('0987654321');    addressBook.addContact(colleague1);

Contact* classmate1 = new ClassmateContact('王五');    classmate1->setPinyin('Wang Wu');    classmate1->setNickname('五哥');    classmate1->setPhoneNumber('1111111111');    addressBook.addContact(classmate1);

Contact* teacher1 = new TeacherContact('赵六');    teacher1->setPinyin('Zhao Liu');    teacher1->setNickname('六哥');    teacher1->setPhoneNumber('2222222222');    addressBook.addContact(teacher1);

addressBook.sortByPinyin();    addressBook.displayContacts();

addressBook.searchByInitial('Z');

return 0;}

程序功能

这个 C++ 通讯录管理系统具备以下功能:

  1. 添加联系人: 可以添加不同类型的联系人,例如亲戚、同事、同学和老师。2. 显示联系人: 可以显示所有联系人的详细信息。3. 按拼音排序: 可以按照姓氏拼音对联系人进行排序。4. 按首字母搜索: 可以根据姓氏的首字母搜索联系人。

总结

这个简单的 C++ 通讯录管理系统展示了如何使用类和继承创建可维护和可扩展的代码。你可以根据自己的需要,扩展该程序的功能,例如添加修改联系人、删除联系人等功能,使其更加完善。


原文地址: https://www.cveoy.top/t/topic/Tiu 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录