定义一个friend类,包括姓名、性别、年龄、电话、邮箱、QQ、单位等属性和对这些属性的操作函数,再定义通讯录类,实现通讯录列表、查询、修改、添加、删除、保存、退出等功能
望采纳
#include <iostream>
#include <string>
#include <vector>
using namespace std;
// 定义Friend类
class Friend
{
public:
Friend(string name, string gender, int age, string phone, string email, string qq, string unit)
{
m_name = name;
m_gender = gender;
m_age = age;
m_phone = phone;
m_email = email;
m_qq = qq;
m_unit = unit;
}
void setName(string name)
{
m_name = name;
}
string getName()
{
return m_name;
}
void setGender(string gender)
{
m_gender = gender;
}
string getGender()
{
return m_gender;
}
void setAge(int age)
{
m_age = age;
}
int getAge()
{
return m_age;
}
void setPhone(string phone)
{
m_phone = phone;
}
string getPhone()
{
return m_phone;
}
void setEmail(string email)
{
m_email = email;
}
string getEmail()
{
return m_email;
}
void setQQ(string qq)
{
m_qq = qq;
}
string getQQ()
{
return m_qq;
}
void setUnit(string unit)
{
m_unit = unit;
}
string getUnit()
{
return m_unit;
}
private:
string m_name;
string m_gender;
int m_age;
string m_phone;
string m_email;
string m_qq;
string m_unit;
};
// 定义通讯录类
class AddressBook
{
public:
AddressBook()
{
m_friends = vector<Friend>();
}
void list()
{
cout << "姓名\t性别\t年龄\t电话\t邮箱\tQQ\t单位" << endl;
for (int i = 0; i < m_friends.size(); i++)
{
cout << m_friends[i].getName() << "\t" << m_friends[i].getGender() << "\t" << m_friends[i].getAge() << "\t" << m_friends[i].getPhone() << "\t" << m_friends[i].getEmail() << "\t" << m_friends[i].getQQ() << "\t" << m_friends[i].getUnit() << endl;
}
}
void query(string name)
{
for (int i = 0; i < m_friends.size(); i++)
{
if (m_friends[i].getName() == name)
{
cout << "姓名\t性别\t年龄\t电话\t邮箱\tQQ\t单位" << endl;
cout << m_friends[i].getName() << "\t" << m_friends[i].getGender() << "\t" << m_friends[i].getAge() << "\t" << m_friends[i].getPhone() << "\t" << m_friends[i].getEmail() << "\t" << m_friends[i].getQQ() << "\t" << m_friends[i].getUnit() << endl;
}
}
}
void modify(string name)
{
for (int i = 0; i < m_friends.size(); i++)
{
if (m_friends[i].getName() == name)
{
string gender;
int age;
string phone;
string email;
string qq;
string unit;
cout << "请输入新的性别:";
cin >> gender;
cout << "请输入新的年龄:";
cin >> age;
cout << "请输入新的电话:";
cin >> phone;
cout << "请输入新的邮箱:";
cin >> email;
cout << "请输入新的QQ:";
cin >> qq;
cout << "请输入新的单位:";
cin >> unit;
m_friends[i].setGender(gender);
m_friends[i].setAge(age);
m_friends[i].setPhone(phone);
m_friends[i].setEmail(email);
m_friends[i].setQQ(qq);
m_friends[i].setUnit(unit);
}
}
}
void add()
{
string name;
string gender;
int age;
string phone;
string email;
string qq;
string unit;
cout << "请输入姓名:";
cin >> name;
cout << "请输入性别:";
cin >> gender;
cout << "请输入年龄:";
cin >> age;
cout << "请输入电话:";
cin >> phone;
cout << "请输入邮箱:";
cin >> email;
cout << "请输入QQ:";
cin >> qq;
Regenerate response