c++编程设计并实现一个C++联系人类:contact,该类有name,tel和postcode三个私有属性,为该类设计若干方法,包括构造函数、存取三个私有属性的函数、显示联系人信息的函数等。(2)设计并实现一个通讯录类:addressBook,该类包含一个存放所有联系人的容器(可以用实验5中的CArray类实现,也可以用STL的容器实现)。(3)为addressBook设计若干方法,包括显示所有联系人、增加联系人、删除联系人、插入联系人、查找联系人等函数,以及将所有联系人保存到文件和从文件中读取联系人的函数。(4)设计主函数,显示操作选择菜单,完成通讯录的显示、增加、查找、删除和保存等功能。(5)建立一个C++工程,包含需要的.h和.cpp文件
时间: 2023-05-14 15:04:43 浏览: 184
以下是一个可能的实现:
```c
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
class Contact {
private:
string name;
string tel;
string postcode;
public:
Contact(string name, string tel, string postcode) {
this->name = name;
this->tel = tel;
this->postcode = postcode;
}
string getName() {
return name;
}
string getTel() {
return tel;
}
string getPostcode() {
return postcode;
}
void setName(string name) {
this->name = name;
}
void setTel(string tel) {
this->tel = tel;
}
void setPostcode(string postcode) {
this->postcode = postcode;
}
void display() {
cout << "Name: " << name << endl;
cout << "Tel: " << tel << endl;
cout << "Postcode: " << postcode << endl;
}
};
class AddressBook {
private:
vector<Contact*> contacts;
public:
void addContact(Contact* contact) {
contacts.push_back(contact);
}
void removeContact(int index) {
contacts.erase(contacts.begin() + index);
}
void insertContact(int index, Contact* contact) {
contacts.insert(contacts.begin() + index, contact);
}
Contact* findContact(string name) {
for (int i = 0; i < contacts.size(); i++) {
if (contacts[i]->getName() == name) {
return contacts[i];
}
}
return NULL;
}
void displayAll() {
for (int i = 0; i < contacts.size(); i++) {
contacts[i]->display();
cout << endl;
}
}
void saveToFile(string filename) {
ofstream outfile(filename);
for (int i = 0; i < contacts.size(); i++) {
outfile << contacts[i]->getName() << "," << contacts[i]->getTel() << "," << contacts[i]->getPostcode() << endl;
}
outfile.close();
}
void loadFromFile(string filename) {
ifstream infile(filename);
string line;
while (getline(infile, line)) {
int pos1 = line.find(",");
int pos2 = line.find(",", pos1 + 1);
string name = line.substr(0, pos1);
string tel = line.substr(pos1 + 1, pos2 - pos1 - 1);
string postcode = line.substr(pos2 + 1);
Contact* contact = new Contact(name, tel, postcode);
addContact(contact);
}
infile.close();
}
};
int main() {
AddressBook addressBook;
while (true) {
cout << "1. Display all contacts" << endl;
cout << "2. Add a contact" << endl;
cout << "3. Find a contact" << endl;
cout << "4. Remove a contact" << endl;
cout << "5. Save to file" << endl;
cout << "6. Load from file" << endl;
cout << "0. Exit" << endl;
int choice;
cin >> choice;
if (choice == 0) {
break;
} else if (choice == 1) {
addressBook.displayAll();
} else if (choice == 2) {
string name, tel, postcode;
cout << "Name: ";
cin >> name;
cout << "Tel: ";
cin >> tel;
cout << "Postcode: ";
cin >> postcode;
Contact* contact = new Contact(name, tel, postcode);
addressBook.addContact(contact);
} else if (choice == 3) {
string name;
cout << "Name: ";
cin >> name;
Contact* contact = addressBook.findContact(name);
if (contact == NULL) {
cout << "Contact not found." << endl;
} else {
contact->display();
}
} else if (choice == 4) {
int index;
cout << "Index: ";
cin >> index;
addressBook.removeContact(index);
} else if (choice == 5) {
string filename;
cout << "Filename: ";
cin >> filename;
addressBook.saveToFile(filename);
} else if (choice == 6) {
string filename;
cout << "Filename: ";
cin >> filename;
addressBook.loadFromFile(filename);
} else {
cout << "Invalid choice." << endl;
}
}
return 0;
}
```
这个程序实现了一个简单的通讯录,包括联系人类 Contact 和通讯录类 AddressBook。Contact 有三个私有属性 name、tel 和 postcode,以及构造函数、存取三个私有属性的函数和显示联系人信息的函数。AddressBook 包含一个存放所有联系人的容器 contacts,以及增加、删除、插入、查找、显示所有联系人、保存到文件和从文件中读取联系人等函数。主函数显示操作选择菜单,根据用户的选择调用相应的函数完成通讯录的显示、增加、查找、删除和保存等功能。
阅读全文