面向对象程序设计实验6


实验内容

  • 重载运算符<<,>>.使得其可用于$CommEntry$.
  • 技术要点:
    (1)为了方便,用全局运算符重载。
    (2)为能访问类中的私有成员,可定义为友元函数。在$CommEntry$的类声明文件中声明:
    // 函数访问非$public$成员时,必为友元
     friend ostream& operator<<(ostream&, CommEntry&);
     friend istream& operator>>(istream&, CommEntry&);
    
    (3)在$CommEntry$的类定义文件中定义全局函数:
    //--operater overloading
    istream& operator>>( istream& in, CommEntry& c ){
    // …
    }

    ostream& operator<<( ostream& out, CommEntry& c ){
    //…
    }

(4)使用

    CommEntry ce;
    //… …
    cin>>ce;
    cout<<ce;

代码



#include<iostream>
#include<algorithm>
#include<cstring>

using namespace std;

class CommEntry{

public:
    //int displaymenu();
    void update_name(string change_name);
    void update_tel(string change_tel);
    void update_addr(string change_addr);
    string get_name();
    string get_tel();
    string get_addr();   
    friend istream& operator>>(istream &in,CommEntry &ce);
    friend ostream& operator<<(ostream &out,CommEntry &ce);
private:
    string name;
    string tel;
    string addr;
};

class Comms{

public:
    Comms(int num,int maxnum=50)
    {
        maxCount=maxnum;
        Count=num;
        pCe=new CommEntry[num];
    }
    ~Comms()
    {
        maxCount=Count=0;
        delete []pCe;
    }
    int displaymenu();
    void inputAll();
    void outputAll();
    CommEntry& get_CommEntry(int inum);
    int find(string nm);
    void modify(string nm,string t);

private:
    CommEntry *pCe;
    int maxCount;
    int Count;
};


int Comms::displaymenu()
{
    int op;
    cout << "-------------功能菜单-------------" << endl;
    cout << "         1.输入通讯录"  << endl;
    cout << "         2.输出通讯录"  << endl;
    cout << "         3.查找通讯录姓名"  << endl;
    cout << "         4.修改通讯录指定名字的电话"  << endl;
    cout << "         0.退出系统"  << endl;
    cout << "----------------------------------" << endl;
    cout << "请输入你要选择的功能编号(0-4):  ";
    while(cin >> op)
    {
        if(op>=0&&op<=4)
            break;
        else
            cout << "输入出错,请重新输入你要选择的功能编号(0-4):  ";
    }
    return op;
}

istream& operator>>(istream &in,CommEntry &ce)
{
    cout << "请分别输入通讯录的姓名,电话和地址(中间用空格隔开):  ";
    in >> ce.name >> ce.tel >> ce.addr;
    return in;
}

ostream& operator<<(ostream&out,CommEntry &ce)
{
    out << "姓名:  " << ce.name << "  电话:  " << ce.tel << "  地址:  " << ce.addr << endl;
    return out;
}


CommEntry& Comms::get_CommEntry(int inum)
{
    return pCe[inum];
}

int Comms::find(string nm)
{
    int flag=0;
    for(int i=0;i<Count;i++)
        if(get_CommEntry(i).get_name()==nm)
        {
            flag=1;
            return i;
        }
    return -1;//找不到该姓名
}

void Comms::modify(string nm,string t)
{
    int pos=find(nm);
    if(pos!=-1)
        get_CommEntry(pos).update_tel(t);
    else cout << "查无此人,无法修改!" << endl;
}



void Comms::inputAll()
{
    for(int i=0;i<Count;i++)
    {
        cout << "***第" << i << "个通讯录条目***" << endl;
        cin >> get_CommEntry(i);
    }
}

void Comms::outputAll()
{
    for(int i=0;i<Count;i++)
    {
        cout << "***第" << i << "个通讯录条目***" << endl;
        cout << get_CommEntry(i);
    }
}


string CommEntry::get_name()
{
    return name;
}
string CommEntry::get_tel()
{
    return tel;
}
string CommEntry::get_addr()
{
    return addr;
}



void CommEntry::update_name(string change_name)
{
    name=change_name;
}

void CommEntry::update_tel(string change_tel)
{
    tel=change_tel;
}

void CommEntry::update_addr(string change_addr)
{
    addr=change_addr;
}


int main()
{
    int num;
    cout << "请输入通讯录的总人数:  " ;
    cin >> num;
    Comms data=Comms(num);
    while(1)
    {
        int op=data.displaymenu();
        if(op==1)
            data.inputAll();
        else if(op==2)
            data.outputAll();
        else if(op==3)
        {
            string temp_name;
            cout << "请输入查找的姓名:  " << endl;
            cin >> temp_name;
            int pos=data.find(temp_name);
            if(pos!=-1)
                cout << data.get_CommEntry(pos);      
            else cout << "查无此人" << endl;  
        }
        else if(op==4)
        {
            string temp_name,temp_tel;
            cout << "请输入要修改的名字:  " ;
            cin >> temp_name;
            cout << "请输入要修改的电话:  ";
            cin >> temp_tel;
            data.modify(temp_name,temp_tel);
        }
        else if(op==0)
            break;
    }
    return 0;
}

文章作者: 保底不歪抽早柚
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 保底不歪抽早柚 !
评论
  目录