问题:通讯录管理程序。通讯录是由通讯录条目组成的。…
子问题:通讯录条目由姓名、电话组成的。可以进行输入、输出、修改姓名、修改电话。加入静态数据成员,拥有者Owner。
测试程序:
1.输入通讯录条目
2.输出通讯录条目
3.修改姓名
4.修改电话
5.修改拥有者
0.退出
#include <iostream>
#include<string>
using namespace std;
class CommEntry
{
public:
void input();
void output();
void setName(string nm);
void setTel(string t);
void setOwner(string ow);
string getName();
string getTel();
string getOwner();
private:
string name;
string tel;
static string Owner;
};
string CommEntry::Owner;//在全局作用域对静态数据成员初始化,如果不赋予初值,则使用其默认值零
void CommEntry::input()
{
cout << "姓名:" << endl;
cin >> name;
cout << "电话:" << endl;
cin >> tel;
cout<<"拥有者:"<<endl;
cin>>CommEntry::Owner;
}
void CommEntry::output()
{
cout << "姓名" << "\t" << "电话" <<"\t\t"<<"拥有者"<< endl;
cout << name << "\t" << tel <<"\t"<<CommEntry::Owner<< endl;
}
void CommEntry::setName(string nm)
{
cout << "将名字改为:" << endl;
cin >> nm;
name = nm;
}
void CommEntry::setTel(string t)
{
cout << "将电话改为:" << endl;
cin >> t;
tel = t;
}
void CommEntry::setOwner(string ow)
{
cout << "将拥有者改为:" << endl;
cin >> ow;
Owner = ow;
}
string CommEntry::getName()
{
return name;
}
string CommEntry::getTel()
{
return tel;
}
string CommEntry::getOwner()
{
return CommEntry::Owner;
}
int menu()
{
int i;
cout << "*******************************************" << endl;
cout << " 1.输入通讯录条目" << endl;
cout << " 2.输出通讯录条目" << endl;
cout << " 3.修改姓名" << endl;
cout << " 4.修改电话" << endl;
cout << " 5.修改拥有者" << endl;
cout << " 0.退出" << endl;
cout << "*******************************************" << endl;
cout << "请选择(0-5):";
while(1)
{
cin >> i;
if(i >= 0 && i <= 5)
break;
else
{
cout << "该选项不在目录中,请重新输入!" << endl;
continue;
}
}
return i;
}
int main()
{
int t = 1;
CommEntry person;
string nm,tl,ow;
while(t != 0)
{
t = menu();
switch(t)
{
case 1:
person.input();
break;
case 2:
person.output();
break;
case 3:
person.setName(nm);
break;
case 4:
person.setTel(tl);
break;
case 5:
person.setOwner(ow);
break;
case 0:
break;
}
}
return 0;
}