主要应用了,函数的重载,类的继承,纯虚函数(虚函数),文件输入输出,运算符的重载,分文件实现等等。
可以在包含源文件和头文件的文件夹中创建2个txt文档,一个叫Phone.txt
一个叫PhoneCar.txt
目的是为了存储数据。
实现效果图:
下面是源文件,创建多个源文件
main.cpp
#include#include"Phonecar.h" #include"Phone.h" #include"shown.h" #include"Phone_List.h" #include"PhoneCar_List.h" #include"People.h" using namespace std; int main() { People t; while(true) { show(); cout<<"请输入数字"< >a; switch(a) { case 0:return 0; case 1:t.add(); break; case 2: t.de(); break; case 3:t.my(); break; case 4:t.finds(); break; case 5:t.display(); break; case 6:t.exchange_car1(); break; case 7:t.exchange_car2(); break; case 8:t.copy_car1(); break; case 9:t.copy_car2(); break; default:cout<<"输入错误,退出系统"< List.cpp
#include"List.h" void list1::add() { } void list1::de() { } void list1::display() { } void list1::finds() { } void list1::my() { }People.cpp
#include"People.h" #includeclass p { public: string name1,number1; }; int b; using namespace std; void People::add() // { list1 *temp; cout<<"1,添加到手机卡,2添加到手机"< >a; switch(a) { case 1:{temp=&car2; temp->add(); break;} case 2:{temp=&car1; temp->add(); break;} default:cout<<"输入错误,退出"< >a; switch(a) {case 1:{ temp=&car2; temp->de(); break;} case 2:{ temp=&car1; temp->de(); break;} default:cout<<"输入错误,退出"< >a; switch(a) {case 1:{temp=&car2; temp->display(); break;} case 2:{temp=&car1; temp->display(); break;} default:cout<<"输入错误,退出"< >a; switch(a) {case 1:{temp=&car2; temp->finds(); break;} case 2:{temp=&car1; temp->finds(); break;} default:cout<<"输入错误,退出"< >a; switch(a) {case 1:{temp=&car2; temp->my(); break;} case 2:{temp=&car1; temp->my(); break;} default:cout<<"输入错误,退出"< 手机卡 { int a=car2.count2();//手机卡numb if(a+car1.count1()>1005) { cout<<"容量不足,操作失败"< 1005) { cout<<"容量已满,操作失败"< Phone.cpp
#include"Phone.h" istream &operator>>(istream &in,Phone &A) { cout<<"请输入姓名,手机号,地址,QQ号"<>A.name>>A.number>>A.address>>A.Qid; return in; } ostream &operator<<(ostream &out,Phone &B) { out< Phone_List.cpp
#include"Phone_List.h" #include#include void swap2(Phone &a,Phone &b) //交换删除后的信息 {Phone c; c.name=a.name; c.address=a.address; c.Qid=a.Qid; c.number=a.number; a.name=b.name; a.address=b.address; a.Qid=b.Qid; a.number=b.number; b.name=c.name; b.address=c.address; b.Qid=c.Qid; b.number=c.number; } Phone_list::Phone_list() //从文本文件中读信息并创建数组 { for(int i=0;i<1005;i++) { Car1[i].address=" "; Car1[i].name=" "; Car1[i].number=" "; Car1[i].Qid=" "; } ifstream in("Phone.txt"); if(!in) { cout<<"打开失败"< >Car1[i].name>>Car1[i].number>>Car1[i].address>>Car1[i].Qid&&EOF) { i++; } num=i; in.close(); } Phone_list::~Phone_list() //写入文本文档 { ofstream out("Phone.txt"); if(!out) { cout<<"打开文件失败"< >a; if(a==0) break; if(num>1005) {cout<<"容量已满,无法添加"< >Car1[num]; num+=1; system("pause"); system("cls"); } } void Phone_list::de() //删除 { cout<<"请检查一下手机卡联系人中是否也有此人"< >a; if(a==0) break; cout<<"请输入要删除联系人的手机号"< >b; int ans=0; for(int i=1;i >a; if(a==0) break; cout<<"请输入要查找联系人的手机号"< >b; int ans=0; for(int i=1;i >a; if(a==0) break; cout<<"请输入要修改联系人的手机号"< >b; int ans=0; for(int i=1;i >Car1[i]; ans++; cout<<"修改成功"< PhoneCar.cpp
#include"Phonecar.h" istream &operator>>(istream &input,Phonecar &A) { cout<<"请输入姓名,手机号"<>A.name>>A.number; return input; } ostream &operator<<(ostream &out,Phonecar &B) { out< >a; switch(a) {case 1:{string p; cin>>p; name=p; break;} case 2:{string u; cin>>u; number=u; break; } default: break; } } PhoneCar_List.cpp
#include"PhoneCar_List.h" #include#include using namespace std; void swap1(Phonecar &a,Phonecar &b) { Phonecar c; c.name=a.name; c.number=a.number; a.name=b.name; a.number=b.number; b.number=c.number; b.name=a.name; } PhoneCar_List::PhoneCar_List() //从文本文件中读信息并创建数组 { for(int i=0;i<1005;i++) { Car2[i].name=" "; Car2[i].number=" "; } ifstream in("PhoneCar.txt"); if(!in) { cout<<"打开失败"< >Car2[i].name>>Car2[i].number&&EOF) { i++; } numb=i; in.close(); } PhoneCar_List::~PhoneCar_List() //写入文本文档 { ofstream out("PhoneCar.txt"); if(!out) { cout<<"打开文件失败"< >a; if(a==0) {return;} if(numb>1005) {cout<<"容量已满,无法添加"< >Car2[numb]; numb+=1; system("pause"); system("cls"); } } void PhoneCar_List::de() //删除 { while(true) { int a; cout<<"1、删除,0、退出"< >a; if(a==0) {break;} cout<<"请输入要删除联系人的手机号"< >b; int ans=0; int c=0,i; for( i=1;i >a; if(a==0) {break;} cout<<"请输入要查找联系人的手机号"< >b; int ans=0; for(int i=1;i >a; if(a==0) { break;} cout<<"请输入要修改联系人的手机号"< >b; int ans=0; for(int i=1;i >Car2[i]; ans++; cout<<"修改成功"< shown.cpp
#include"shown.h" #includeusing namespace std; void show() { cout<<"********欢迎使用通讯录系统*************"< 下面是源文件对应的头文件
List.h#ifndef LIST_H_INCLUDED #define LIST_H_INCLUDED class list1 //通讯录 {public: virtual void add()=0; //添加联系人 virtual void de()=0; //删除联系人 virtual void display()=0; // 显示全部联系人 virtual void my()=0; // 修改联系人 virtual void finds()=0; // 搜索并显示联系人 }; #endif // LIST_H_INCLUDEDList1.h
#ifndef LIST1_H_INCLUDED #define LIST1_H_INCLUDED class list1 //通讯录 {public: virtual void add()=0; //添加联系人 virtual void de()=0; //删除联系人 virtual void display()=0; // 显示全部联系人 virtual void my()=0; // 修改联系人 virtual void finds()=0; // 搜索并显示联系人 }; #endif // LIST1_H_INCLUDEDPeople.h
#ifndef PEOPLE_H_INCLUDED #define PEOPLE_H_INCLUDED #include"Phone_List.h" #include"PhoneCar_List.h" #include"List.h" class People {public: Phone_list car1; PhoneCar_List car2; void add(); //添加 void de(); //删除 void display(); // 展示 void my(); // 修改 void finds(); // 查找 void exchange_car1(); //手机->手机卡 void exchange_car2(); // 手机卡->手机 void copy_car1(); //手机复制->手机卡 void copy_car2(); //手机卡复制->手机 }; #endif // PEOPLE_H_INCLUDEDPhone.h
#ifndef PHONE_H_INCLUDED #define PHONE_H_INCLUDED #include"Phonecar.h" class Phone :public Phonecar //手机类 { public: string address,Qid; Phone(string c="0",string d="0",string a="0",string b="0") { name=a; number=b; address=c; Qid=d; } void set(string a,string b,string c,string d); void moidfy(); friend istream &operator>>(istream &in,Phone &A); friend ostream &operator<<(ostream &out,Phone &B); }; istream &operator>>(istream &in,Phone &A); ostream &operator<<(ostream &out,Phone &B); #endif // PHONE_H_INCLUDEDPhone_List.h
#ifndef PHONE_LIST_H_INCLUDED #define PHONE_LIST_H_INCLUDED #include"List.h" #include"Phone.h" using namespace std; class Phone_list:public list1 {public: int num; Phone Car1[1005]; Phone_list(); ~Phone_list(); void add(); // void de(); // void display(); // void my(); // void finds(); // int count1(); }; #endif PHONE_LIST_H_INCLUDEDPhonecar.h
#ifndef PHONECAR_H_INCLUDED #define PHONECAR_H_INCLUDED #includeusing namespace std; class Phonecar //手机卡类 { public: string name; string number; Phonecar(string a="s",string b="s",int c=0) {name=a; number=b; } void set(string c,string b); void moidfy(); friend istream &operator>>(istream &in,Phonecar &A); friend ostream &operator<<(ostream &out,Phonecar &B); }; istream &operator>>(istream &input,Phonecar &A); ostream &operator<<(ostream &out,Phonecar &B); #endif // PHONECAR_H_INCLUDED PhoneCar_List.h
#ifndef PHONECAR_LIST_H_INCLUDED #define PHONECAR_LIST_H_INCLUDED #include"List" #include#include"Phonecar.h" #include"List.h" //#include"Phone.h" //#include"Phone_List.h" #include"PhoneCar_List.h" class PhoneCar_List:public list1 { public: int numb; Phonecar Car2[1005]; PhoneCar_List(); ~PhoneCar_List(); void add(); //添加 void de(); //删除 void display(); // 展示 void my(); // 修改 void finds(); // 查找 int count2(); //返回numb值 }; #endif // PHONECAR_LIST_H_INCLUDED shown.h
#ifndef SHOWN_H_INCLUDED #define SHOWN_H_INCLUDED void show(); #endif // SHOWN_H_INCLUDED
上图为文件在磁盘中的情况,
下图器在编译器中的代码情况



