C++通讯录管理系统:类、继承与多态实战
C++通讯录管理系统:类、继承与多态实战
你想用C++编写一个功能齐全的通讯录管理系统吗?这篇博客将带你一步步实现!本项目不仅涵盖新建、分组、查找等基本功能,还深入讲解了C++面向对象编程的核心概念,包括:
- 类的定义、数据成员、成员函数* 构造函数和析构函数* 类的继承与派生* 类的多态性* 运算符重载* 数据类型和数据结构
代码实现cpp#include #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 = ''; }
// 获取联系人姓名 string getName() const { return name; }
// 设置姓名拼音 void setPinyin(string p) { pinyin = p; }
// 设置昵称 void setNickname(string n) { nickname = n; }
// 设置手机号码 void setPhoneNumber(string p) { phoneNumber = p; }
// 显示联系人信息 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; }
// 按拼音排序 bool operator<(const Contact& other) const { return pinyin < other.pinyin; }};
// 亲属联系人class RelativeContact : public Contact {public: RelativeContact(string n) : Contact(n) {}
void display() override { cout << '---亲属联系人---' << endl; Contact::display(); }};
// 同事联系人class ColleagueContact : public Contact {public: ColleagueContact(string n) : Contact(n) {}
void display() override { cout << '---同事联系人---' << endl; Contact::display(); }};
// 同学联系人class ClassmateContact : public Contact {public: ClassmateContact(string n) : Contact(n) {}
void display() override { cout << '---同学联系人---' << endl; Contact::display(); }};
// 教师联系人class TeacherContact : public Contact {public: TeacherContact(string n) : Contact(n) {}
void display() override { 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 < *b; // 使用运算符重载 }); }
// 按姓氏首字母查找 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;}
代码解析
Contact类: 作为所有联系人类型的基类,包含姓名、拼音、昵称、地址等通用信息。2. 派生类:RelativeContact、ColleagueContact等类继承自Contact,代表不同类型的联系人。3. 多态: 通过虚函数display(),实现根据联系人类型显示不同信息。4.AddressBook类: 管理所有联系人,提供添加、显示、排序、查找等功能。5. 运算符重载: 重载小于运算符<,方便按拼音排序。
总结
本篇博客介绍了如何使用C++编写一个功能完善的通讯录管理系统,并详细讲解了代码实现。希望通过学习本项目,你能更深入地理解C++面向对象编程,并能够将这些知识应用到实际项目中。
原文地址: https://www.cveoy.top/t/topic/Tgf 著作权归作者所有。请勿转载和采集!