编写C++代码,使用class类,继承与派生的方法,假没各戸数据包括:姓名、性別、眼多、申活、00、微信、公司。主要功能:1) 通沢 录信息录入功能;2通讯录信息修改、刪除功能:3,通讯录信息浏览功能:4)通讯录信息查询功能:5)按姓名排序功能:6)保行数据到文件。
时间: 2023-07-10 11:33:26 浏览: 148
好的,以下是符合您要求的C++代码:
```cpp
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <fstream>
using namespace std;
class Person {
protected:
string name;
string gender;
string phone;
string email;
public:
Person(string n, string g, string p, string e) : name(n), gender(g), phone(p), email(e) {}
};
class Friend : public Person {
private:
string wechat;
public:
Friend(string n, string g, string p, string e, string w) : Person(n, g, p, e), wechat(w) {}
void update(string n, string g, string p, string e, string w) {
name = n;
gender = g;
phone = p;
email = e;
wechat = w;
}
void display() {
cout << "姓名:" << name << endl;
cout << "性别:" << gender << endl;
cout << "电话:" << phone << endl;
cout << "邮箱:" << email << endl;
cout << "微信:" << wechat << endl;
}
string get_name() {
return name;
}
};
bool cmp(Friend f1, Friend f2) {
return f1.get_name() < f2.get_name();
}
void add_friend(vector<Friend>& book) {
string n, g, p, e, w;
cout << "请输入姓名:";
cin >> n;
cout << "请输入性别:";
cin >> g;
cout << "请输入电话:";
cin >> p;
cout << "请输入邮箱:";
cin >> e;
cout << "请输入微信:";
cin >> w;
Friend f(n, g, p, e, w);
book.push_back(f);
}
void update_friend(vector<Friend>& book) {
string name;
cout << "请输入要修改的联系人姓名:";
cin >> name;
for (int i = 0; i < book.size(); i++) {
if (book[i].get_name() == name) {
string n, g, p, e, w;
cout << "请输入新的姓名:";
cin >> n;
cout << "请输入新的性别:";
cin >> g;
cout << "请输入新的电话:";
cin >> p;
cout << "请输入新的邮箱:";
cin >> e;
cout << "请输入新的微信:";
cin >> w;
book[i].update(n, g, p, e, w);
cout << "修改成功!" << endl;
return;
}
}
cout << "未找到该联系人!" << endl;
}
void delete_friend(vector<Friend>& book) {
string name;
cout << "请输入要删除的联系人姓名:";
cin >> name;
for (int i = 0; i < book.size(); i++) {
if (book[i].get_name() == name) {
book.erase(book.begin() + i);
cout << "删除成功!" << endl;
return;
}
}
cout << "未找到该联系人!" << endl;
}
void display_all(vector<Friend>& book) {
for (int i = 0; i < book.size(); i++) {
book[i].display();
cout << endl;
}
}
void search_friend(vector<Friend>& book) {
string name;
cout << "请输入要查找的联系人姓名:";
cin >> name;
for (int i = 0; i < book.size(); i++) {
if (book[i].get_name() == name) {
book[i].display();
return;
}
}
cout << "未找到该联系人!" << endl;
}
void sort_friend(vector<Friend>& book) {
sort(book.begin(), book.end(), cmp);
}
void save_to_file(vector<Friend>& book) {
ofstream fout("address_book.txt");
for (int i = 0; i < book.size(); i++) {
fout << book[i].get_name() << " " << book[i].gender << " " << book[i].phone << " " << book[i].email << " " << book[i].wechat << endl;
}
fout.close();
cout << "保存成功!" << endl;
}
int main() {
vector<Friend> book;
while (true) {
cout << "----------------------" << endl;
cout << "请选择操作:" << endl;
cout << "1. 添加联系人" << endl;
cout << "2. 修改联系人" << endl;
cout << "3. 删除联系人" << endl;
cout << "4. 显示所有联系人" << endl;
cout << "5. 查找联系人" << endl;
cout << "6. 按姓名排序" << endl;
cout << "7. 保存到文件" << endl;
cout << "8. 退出" << endl;
cout << "----------------------" << endl;
int option;
cin >> option;
switch (option) {
case 1:
add_friend(book);
break;
case 2:
update_friend(book);
break;
case 3:
delete_friend(book);
break;
case 4:
display_all(book);
break;
case 5:
search_friend(book);
break;
case 6:
sort_friend(book);
cout << "排序成功!" << endl;
break;
case 7:
save_to_file(book);
break;
case 8:
cout << "谢谢使用!" << endl;
return 0;
default:
cout << "输入有误,请重新输入!" << endl;
break;
}
}
return 0;
}
```
这个程序使用了类的继承与派生的方法,`Person`类作为基类,`Friend`类继承了`Person`类,并添加了微信号属性。程序中使用`vector`容器来存储所有联系人信息,并实现了添加、修改、删除、显示、查找、按姓名排序、保存到文件等功能。
阅读全文