C++通讯录实现

C++通讯录花了半天时间,利用C++中的vector容器、迭代器实现了通讯录的简单功能,更直接的见识了C++与C语言之间的差别,封装真的是超级方便啊~下面贴出我的代码,希望对大家有所帮助~addr.h:#ifndef _ADDR_H_#define _ADDR_H_#include using namespace std;class Addr {public: friend ostream& operator<<(ostream& out,cons
摘要由CSDN通过智能技术生成

C++通讯录

花了半天时间,利用C++中的vector容器、迭代器实现了通讯录的简单功能,更直接的见识了C++与C语言之间的差别,封装真的是超级方便啊~

下面贴出我的代码,希望对大家有所帮助~

addr.h:

#ifndef _ADDR_H_
#define _ADDR_H_
#include <iostream>

using namespace std;

class Addr 
{
public:
    friend ostream& operator<<(ostream& out,const Addr& s);
    Addr();
    void Interface();
    void Choose();
    void Input();
    void Display();
    void Search();
    void MyDelete();
    ~Addr();
private:
    int id;
    char name[1024];
    char tel[1024];
    char address[1024];
};

#endif

addr.cpp:

#include <iostream>
#include <vector>
#include "addr.h"

using namespace std;

vector<Addr> a;

static int ID = 1;

ostream& operator << (ostream& out,const Addr& s)
{
    out << s.name;
    return out;
}

Addr :: Addr()
{
    cout << "Welcome to Address_Book!" << endl;
}

Addr :: ~Addr()
{

}
void Addr :: Interface()
{
    system("clear"); 

    cout << "+================&#
  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,让我们来实现一个简单的通讯录,用面向对象的方式实现。 首先,我们需要一个联系人类,它应该有姓名、电话号码等基本信息。我们可以这样定义一个联系人类: ```cpp class Contact { public: Contact(const string& name, const string& phone) : name_(name), phone_(phone) {} string name() const { return name_; } string phone() const { return phone_; } private: string name_; string phone_; }; ``` 接下来,我们需要一个通讯录类,它应该能添加、删除、查找联系人等操作。我们可以这样定义一个通讯录类: ```cpp class AddressBook { public: void AddContact(const Contact& contact) { contacts_.push_back(contact); } void RemoveContact(const string& name) { for (auto it = contacts_.begin(); it != contacts_.end(); ++it) { if (it->name() == name) { contacts_.erase(it); return; } } } Contact FindContact(const string& name) const { for (const auto& contact : contacts_) { if (contact.name() == name) { return contact; } } return Contact("", ""); } private: vector<Contact> contacts_; }; ``` 现在,我们可以在主函数中使用这些类来实现一个简单的通讯录程序: ```cpp int main() { AddressBook address_book; address_book.AddContact(Contact("Tom", "123456789")); address_book.AddContact(Contact("Jerry", "987654321")); address_book.RemoveContact("Tom"); Contact jerry = address_book.FindContact("Jerry"); cout << jerry.name() << " " << jerry.phone() << endl; return 0; } ``` 这样,我们就用面向对象的方式实现了一个简单的通讯录程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值