本文实例为大家分享了C++类实现通讯录功能的具体代码,供大家参考,具体内容如下
软件使用的是Microsoft Visual Studio
编写通讯录之前,先思考一下要实现什么功能,大概的结构,要创建几个类等等。
首先,是思考要实现什么功能。
一般的通讯录有添加,删除,修改,查找,显示等功能,一般联系人的信息包括:姓名,性别,年龄,电话号码,家庭地址。
我们首先新建一个类,用来初始化姓名,年龄,性别,电话号码,家庭地址,这几个变量
- #pragma once
- #include<iostream>
- #include<string>
- //#include "MailList.hpp"
- using namespace std;
- class MailList
- {
- public:
- ?? ?void setName(string);//给变量赋值函数
- ?? ?void setAge(string);
- ?? ?void setSex(string);
- ?? ?void setTel(string);
- ?? ?void setAddress(string);
- ?? ?string getName();//返回变量值函数
- ?? ?string getAge();
- ?? ?string getSex();
- ?? ?string getTel();
- ?? ?string getAddress();
- ?
- private:
- ?? ?string name;//私有函数成员,定义变量
- ?? ?string age;
- ?? ?string sex;
- ?? ?string tel;
- ?? ?string address;
- };
- ?
- ?
- void MailList::setName(string name)
- {
- ?? ?this->name=name;
- }
- void MailList::setAge(string age)
- {
- ?? ?this->age=age;
- }
- void MailList::setSex(string sex)
- {
- ?? ?this->sex=sex;
- }
- void MailList::setTel(string tel)
- {
- ?? ?this->tel=tel;
- }
- void MailList::setAddress(string address)
- {
- ?? ?this->address=address;
- }
- string MailList::getName()
- {
- ?? ?return this->name;
- }
- string MailList::getAge()
- {
- ?? ?return this->age;
- }
- string MailList::getSex()
- {
- ?? ?return this->sex;
- }
- string MailList::getTel()
- {
- ?? ?return this->tel;
- }
- string MailList::getAddress()
- {
- ?? ?return this->address;
- }
这里也可以使用构造函数初始化函数成员,构造函数函数名与类名一样。
然后,创建一个通讯录管理类,先把总的结构搭建起来
- class MailListManager//通讯录管理类
- {
- public:
- ?? ?MailListManager();//构造函数
- ?? ?void initList();//初始化通讯录功能,在通讯录里记录为空时使用
- ?? ?void insertList();//添加联系人功能,在通讯录里有记录时使用
- ?? ?void showList();//显示联系人功能,显示通讯录中所有联系人的记录
- ?? ?void deleteList();//删除联系人功能
- ?? ?void selectList();//查找联系人功能
- ?? ?void updateList();//修改联系人功能
- ?? ?void dropList();//清空通讯录功能
- ?? ?void save();//保存到文件,文件的写入
- ?? ?void loading();//加载,读出文件
- ?? ?string pw();//密码加密(我没能实现)
- ?? ?int exiet(string);//检查联系人是否存在
- ?
- private:
- ?
- ?? ?MailList mail[Max];//数组,MailList类型,这属于实例化对象
- ?? ?int len;//计数器
- ?
- };
- MailListManager::MailListManager()//构造函数就是用来初始化函数成员的,未初始化的函数成员不可用。这里初始化一下计数器
- {
- ?? ?len = 0;
- }
- int MailListManager::exiet(string name)//定义检查函数,检查联系人是否存在,以姓名的匹配为条件
- {
- ?
- }
- void MailListManager::loading()//定义加载函数
- {
- ?
- }
- void MailListManager::save()//定义保存函数
- {
- ?
- }
- void MailListManager::initList()//定义初始化函数
- {
- ?
- }
- void MailListManager::insertList()//定义添加函数
- {
- ?
- }
- void MailListManager::showList()//定义显示函数
- {
- ?
- }
- void MailListManager::updateList()//定义修改函数
- {
- ?
- }
- void MailListManager::deleteList()//定义删除函数
- {
- ?
- }
- void MailListManager::selectList()//定义查找函数
- {
- ?
- }
- void MailListManager::dropList()//定义清空函数
- {
- ?
- }
总结构搭建好后,再开始编写里面的定义内容。
- //这里声明部分就不显示了,直接看定义
- ?
- void MailListManager::loading()//加载函数的定义
- {
- ?? ?len = 0;//计数器len,每次调用加载函数的时候都要重新初始化为0,这样做是防止之后添加联系人时重复加载导致保存多次。
- ?? ?string name, sex, age, tel, address;//局部变量,每次使用都要声明一下的
- ?? ?ifstream in;//实例化文件类“fstream”对象
- ?? ?in.open("maillist/mail.txt");//打开文件
- ?? ?if (!in)//如果文件未打开
- ?? ?{
- ?
- ?? ??? ?cout << "--文件打开失败--" << endl;
- ?? ??? ?system("pause");
- ?? ?}
- ?? ?else
- ?? ?{
- ?? ??? ?while (!in.eof())//如果未达到文件末尾
- ?? ??? ?{
- ?? ??? ??? ?in >> name >> age >> sex >> tel >> address;
- ?? ??? ??? ?if (in.fail())break;//ifstream类的作用是从文件中读出数据到控制台上,但没有显示出来,这就相当于再次赋值给数组,就是给之前声明的MailList类的数组,所以这里使用数组mail[*].***保存数据,因为不知道到底有多少数据,所以用死循环来控制,跳出条件是达到文件末尾就跳出,这样就可以保证将文件中的内容全部存到数组里
- ?? ??? ??? ?mail[len].setName(name);
- ?? ??? ??? ?mail[len].setAge(age);
- ?? ??? ??? ?mail[len].setSex(sex);
- ?? ??? ??? ?mail[len].setTel(tel);
- ?? ??? ??? ?mail[len].setAddress(address);
- ?? ??? ??? ?len++;
- ?? ??? ?}
- ?? ?}
- ?? ?in.close();
- }
- int MailListManager::exiet(string name)//检验联系人是否存在
- {
- ?? ?for (int i = 0; i < len; i++)//每当调用检验联系人的函数之前,一定要加载一下,让数据全部保存到控制台的数组中,且要从控制台输入一个名字传给检验联系人函数,让传入的名字与数组中的数据逐一对比,从而检验联系人是否存在
- ?? ?{
- ?? ??? ?if (mail[i].getName() == name)
- ?? ??? ?{
- ?? ??? ??? ?return i;//如果存在,返回数组下标
- ?? ??? ?}
- ?? ?}
- ?? ?return -1;//不存在,返回-1
- }
- void MailListManager::save()//保存文件函数
- {
- ?? ?ofstream out;//ofstream类的作用是把控制台上的数据写入文件
- ?? ?out.open("maillist/mail.txt");//打开文件
- ?? ?if (!out)//如果文件未打开
- ?? ?{
- ?? ??? ?cout << "--文件打开失败--" << endl;
- ?? ?}
- ?? ?else
- ?? ?{
- ?? ??? ?for (int i = 0; i < len; i++)//这里的计数器len的值来自之后定义的函数中,len的值取决于谁调用的保存函数
- ?? ??? ?{
- ?? ??? ??? ?out << mail[i].getName() << " " << mail[i].getAge() << " " << mail[i].getSex() << " " << mail[i].getTel() << " " << mail[i].getAddress() << endl;
- ?? ??? ?}
- ?? ?}
- ?
- ?
- ?? ?out.close();
- }
- ?
- ?
- void MailListManager::initList()//初始化联系人。之前我先写了添加,修改,删除,在解决一些问题的时候发现添加功能调用加载函数与删除修改功能调用加载函数有冲突,导致重复显示,所以在老师的帮助下加入了这个初始化联系人功能,当然有更好的方法,只是我暂时还不会用(TvT)
- {
- ?? ?string name, age, sex, tel, address;
- ?? ?cout << "请输入联系人的信息(在姓名后输入stop停止输入):" << endl;
- ?? ?while (true)
- ?? ?{
- ?? ??? ?cout << "姓名:";
- ?? ??? ?cin >> name;
- ?? ??? ?if (name == "stop") break;
- ?? ??? ?else
- ?? ??? ?{
- ?? ??? ??? ?mail[len].setName(name);//简单的调用函数,不想讲了
- ?? ??? ??? ?cout << "年龄:";
- ?? ??? ??? ?cin >> age;
- ?? ??? ??? ?mail[len].setAge(age);
- ?? ??? ??? ?cout << "性别:";
- ?? ??? ??? ?cin >> sex;
- ?? ??? ??? ?mail[len].setSex(sex);
- ?? ??? ??? ?cout << "电话号码:";
- ?? ??? ??? ?cin >> tel;
- ?? ??? ??? ?mail[len].setTel(tel);
- ?? ??? ??? ?cout << "家庭地址:";
- ?? ??? ??? ?cin >> address;
- ?? ??? ??? ?mail[len].setAddress(address);
- ?? ??? ??? ?len++;//这个len最终的值会给到save.....emmm..说“给”好像不太准确,找不到合适的词了,反正代码顺着往下执,len加到最后不会再变了,save直接用它。
- ?? ??? ?}
- ?? ?}
- ?? ?save();
- ?
- }
- void MailListManager::insertList()//添加联系人函数
- {
- ?? ?this->loading();//加载一下初始化联系人的数据
- ?? ?string name, age, sex, tel, address;//局部变量要重新声明
- ?? ?cout << "请输入插入联系人的数量:" << endl;
- ?? ?int count = 0;//再来个计数器,控制每次想要添加的联系人的数量
- ?? ?cin >> count;//让用户来指定每次添加多少人
- ?? ?for (int i = 0; i < count; i++)
- ?? ?{
- ?? ??? ?cout << "姓名:";
- ?? ??? ?cin >> name;
- ?? ??? ?mail[i].setName(name);//函数调用过程,还是说一下吧。mail[]数组的类型是MailList,然后它是MailListManager这个类的函数成员,通过它作为桥梁来调用MailListManager类成员函数
- ?? ??? ?cout << "年龄:";
- ?? ??? ?cin >> age;
- ?? ??? ?mail[i].setAge(age);
- ?? ??? ?cout << "性别:";
- ?? ??? ?cin >> sex;
- ?? ??? ?mail[i].setSex(sex);
- ?? ??? ?cout << "电话号码:";
- ?? ??? ?cin >> tel;
- ?? ??? ?mail[i].setTel(tel);
- ?? ??? ?cout << "家庭地址:";
- ?? ??? ?cin >> address;
- ?? ??? ?mail[i].setAddress(address);
- ?? ?}
- ?? ?ofstream out;//添加有单独的保存文件定义,因为只有添加功能需要使用文件追加
- ?? ?out.open("maillist/mail.txt", ios::app);
- ?? ?if (!out)
- ?? ?{
- ?? ??? ?cout << "--文件打开失败--" << endl;
- ?? ?}
- ?? ?else
- ?? ?{
- ?? ??? ?for (int i = 0; i < count; i++)
- ?? ??? ?{
- ?? ??? ??? ?out << mail[i].getName() << " " << mail[i].getAge() << " " << mail[i].getSex() << " " << mail[i].getTel() << " " << mail[i].getAddress() << endl;
- ?? ??? ?}
- ?? ??? ?out.close();
- ?? ?}
- }
- void MailListManager::showList()//显示联系人
- {
- ?
- ?? ?loading();
- ?? ?MailList temp;
- ?? ?for (int j=0;j<len;j++)//这里是给联系人排序,通过名字排序
- ?? ?{
- ?? ??? ?for (int i = j+1; i < len; i++)
- ?? ??? ?{
- ?? ??? ??? ?if (mail[j].getName() > mail[i].getName())
- ?? ??? ??? ?{
- ?? ??? ??? ??? ?temp = mail[i];
- ?? ??? ??? ??? ?mail[i] = mail[j];//交换
- ?? ??? ??? ??? ?mail[j] = temp;?? ?
- ?? ??? ??? ?}
- ?? ??? ?}
- ?? ??? ?
- ?? ?}
- ?? ?for (int i = 0; i < len; i++)//这里的len值来自loading()
- ?? ?{
- ?? ??? ?cout << setw(10) << mail[i].getName() << " ?" << setw(8) << mail[i].getAge() << " ?" << setw(4) <<
- ?? ??? ??? ?mail[i].getSex() << " ?" << setw(15) << mail[i].getTel() << " ?" << setw(20) << mail[i].getAddress();//setw()是格式控制函数
- ?? ??? ?cout << endl << endl;
- ?? ?}
- }
- void MailListManager::updateList()//修改联系人,修改联系人之前要找到这个联系人,存在才能删除
- {
- ?? ?loading();//加载一下
- ?? ?string name, age, sex, tel, address;
- ?? ?cout << "请输入要修改的联系人姓名:";
- ?? ?cin >> name;
- ?? ?int ret = exiet(name);//检验一下是否存在,存在exiet()会返回该联系人所在的数组下标,在这个数组下标里重新输入一遍数据覆盖掉原有数据就是修改联系人了
- ?? ?if (ret != -1)
- ?? ?{
- ?? ??? ?cout << "请重新输入联系人信息:" << endl;
- ?? ??? ?cout << "姓名:";
- ?? ??? ?cin >> name;
- ?? ??? ?mail[ret].setName(name);
- ?? ??? ?cout << "年龄:";
- ?? ??? ?cin >> age;
- ?? ??? ?mail[ret].setAge(age);
- ?? ??? ?cout << "性别:";
- ?? ??? ?cin >> sex;
- ?? ??? ?mail[ret].setSex(sex);
- ?? ??? ?cout << "电话号码:";
- ?? ??? ?cin >> tel;
- ?? ??? ?mail[ret].setTel(tel);
- ?? ??? ?cout << "家庭地址:";
- ?? ??? ?cin >> address;
- ?? ??? ?mail[ret].setAddress(address);
- ?? ?}
- ?? ?else
- ?? ??? ?cout << "啊哦~联系人不存在喔(-o-)";
- ?? ?save();//改完记得重新保存一下,不然是没有任何改动的哦
- }
- void MailListManager::deleteList()//删除联系人,理同修改一样,只不过是信息的覆盖变为了内容前移覆盖
- {
- ?? ?loading();
- ?? ?string name;
- ?? ?int o;
- ?? ?cout << "请输入要删除的联系人的姓名: ?";
- ?? ?cin >> name;
- ?? ?int ret = exiet(name);
- ?? ?if (ret == -1)
- ?? ?{
- ?? ??? ?cout << "啊哦~联系人不存在喔(-o-)";
- ?? ?}
- ?? ?else
- ?? ?{
- ?? ??? ?cout << "确定要删除吗?" << endl << "1.确定" << " ? ?" << "2.我再想想" << endl << "请选择:";
- ?? ??? ?cin >> o;
- ?? ??? ?if (o == 2)
- ?? ??? ??? ?cout << "好的~";
- ?? ??? ?else
- ?? ??? ?{
- ?
- ?? ??? ??? ?for (int i = ret; i < len; i++)
- ?? ??? ??? ?{
- ?? ??? ??? ??? ?mail[i] = mail[i + 1];
- ?? ??? ??? ?}
- ?
- ?
- ?? ??? ??? ?cout << "删除成功!";
- ?? ??? ?}
- ?? ?}
- ?? ?save();
- ?
- }
- void MailListManager::selectList()//查找联系人,查找联系人就更简单啦,调用一下检验存在的函数,然后根据下标直接输出此联系人信息就好
- {
- ?? ?loading();
- ?? ?string name;
- ?? ?cout << "请输入要查找的人的姓名: ? ";
- ?? ?cin >> name;
- ?? ?int ret = exiet(name);
- ?? ?if (ret != -1)
- ?? ?{
- ?? ??? ?cout << "姓名:" << mail[ret].getName() << endl;
- ?? ??? ?cout << "年龄:" << mail[ret].getAge() << endl;
- ?? ??? ?cout << "性别:" << mail[ret].getSex() << endl;
- ?? ??? ?cout << "电话号码:" << mail[ret].getTel() << endl;
- ?? ??? ?cout << "家庭地址:" << mail[ret].getAddress() << endl;
- ?? ?}
- ?? ?else
- ?? ??? ?cout << "啊哦~联系人不存在喔(-o-)";
- }
- void MailListManager::dropList()//清空通讯录,重新写入文件,写入一个空字符覆盖之前的数据,就清空啦
- {
- ?? ?int n;
- ?? ?cout << "确定清空吗?" << endl << " ?" << "1.YES" << " ? " << "2.NO" << "请选择:";
- ?? ?cin >> n;
- ?? ?if (n == 2)
- ?? ?{
- ?? ??? ?cout << "好的~";
- ?? ?}
- ?? ?else
- ?? ?{
- ?? ??? ?ofstream out;
- ?? ??? ?out.open("maillist/mail.txt");
- ?? ??? ?if (!out)
- ?? ??? ?{
- ?? ??? ??? ?cout << "--文件打开失败--" << endl;
- ?? ??? ?}
- ?? ??? ?else
- ?? ??? ?{
- ?? ??? ??? ?out << " ";
- ?? ??? ??? ?cout << "清除成功" << endl;
- ?? ??? ?}
- ?? ??? ?out.close();
- ?? ?}
- }
- string MailListManager::pw()//密码加密,未完成
- {
- ?? ?char psw[100] = { 0 }, c;
- ?? ?int i = 0;
- /*?? ?cin >> c*/;
- ?? ?while ((c = getch()) != '\r')
- ?? ?{
- ?? ??? ?if (c != '\b')
- ?? ??? ?{
- ?? ??? ??? ?cout << "*";
- ?? ??? ??? ?psw[i++];
- ?? ??? ?}
- ?? ??? ?else
- ?? ??? ?{
- ?? ??? ??? ?cout << "\b \b";
- ?? ??? ??? ?i--;
- ?? ??? ?}
- ?? ?}
- ?? ?psw[i] = '\0';
- ?? ?cout << psw;
- ? ??
- }
然后再写个登陆类
- #include<iostream>
- #include<fstream>
- #include<string>
- using namespace std;
- class User
- {
- public:
- ?? ?User();
- ?? ?void loading();
- ?? ?int check(string);
- ?? ?int login(string, string);
- ?? ?void sign(string,string);
- private:
- ?? ?string s[50];
- ?? ?int len;
- ?
- };
- User::User()
- {
- ?? ?len = 0;
- }
- ?
- void User::loading()//加载函数
- {
- ?? ?string name, password;
- ?? ?ifstream in;
- ?? ?in.open("User/user.txt");
- ?? ?if (!in)
- ?? ?{
- ?? ??? ?cout << "错误!";
- ?? ?}
- ?? ?else
- ?? ?{
- ?? ??? ?while (!in.eof())
- ?? ??? ?{
- ?? ??? ??? ?in >> s[len];
- ?? ??? ??? ?if (in.fail())break;
- ?? ??? ??? ?len++;
- ?? ??? ?}
- ?
- ?? ?}
- ?? ?in.close();
- }
- int User::check(string name)//检验函数
- {
- ?? ?loading();
- ?? ?for (int i = 0; i < len; i=i+2)
- ?? ?{
- ?? ??? ?if (name == s[i])
- ?? ??? ?{
- ?? ??? ??? ?return i;
- ?? ??? ?}
- ?? ??? ?else
- ?? ??? ?{
- ?? ??? ??? ?return -1;
- ?? ??? ?}
- ?? ?}
- }
- int User::login(string name, string password)//登陆函数
- {
- ?? ?if (check(name) == -1)
- ?? ?{
- ?? ??? ?cout << "用户根本不存在喔!";
- ?? ?}
- ?? ?else
- ?? ?{
- ?? ??? ?if (s[check(name) + 1] == password)
- ?? ??? ?{
- ?? ??? ??? ?return 1;
- ?? ??? ?}
- ?? ??? ?else
- ?? ??? ?{
- ?? ??? ??? ?cout << "密码输入错误!";
- ?? ??? ?}
- ?? ?}
- }
- void User::sign(string name,string password)//注册函数
- {
- ?? ?loading();
- ?? ?if (check(name) != -1)//已有用户名
- ?? ?{
- ?? ??? ?cout << "用户名已存在!";
- ?? ?}
- ?? ?else
- ?? ?{
- ?? ??? ?ofstream out;
- ?? ??? ?out.open("User/user.txt", ios::app);
- ?? ??? ?if (!out)
- ?? ??? ?{
- ?? ??? ??? ?cout << "文件打开失败!";
- ?? ??? ?}
- ?? ??? ?else
- ?? ??? ?{
- ?? ??? ??? ?out <<endl<< name<<" "<< password;
- ?? ??? ?}
- ?? ??? ?out.close();
- ?? ??? ?cout << " ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?注册成功!";
- ?? ??? ?system("pause");
- ?? ?}
- }
登陆类的加载函数,检验函数逻辑同之前管理类的一样,登陆函数与注册函数,实际上就是文件读出与写入。
最后,写主函数
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。