实验二:oop版通讯录管理程序

题目

通讯录管理程序。通讯录是由通讯录条目组成的。…
一、子问题描述:通讯录条目由姓名、电话组成的。可以进行输入、输出、修改姓名、修改电话。(可以进行适当的扩展)

程序主菜单如下:
1.输入通讯录条目
2.输出通讯录条目
3.修改姓名
4.修改电话
0.退出

要求:采用面向对象方式编写此程序。
UML设计


提示:该实验只做了一条条目

根据UML图编写出CommEntry对象

将CommEntry对象定义为头文件,便于主函数调用。
CommEntry.h文件

#ifndef CommEntry_H
#define CommEntry_H

#include<iostream>
using namespace std;
class CommEntry{
    public:
    void input();
    void output();
    void setName(string nm);
    void setTel(string t);
    private:
    string name;
    string tel;
};

void CommEntry::input(){
    cout<<"Name:";
    cin>>name;
    cout<<"Tel:";
    cin>>tel;
}
void CommEntry::output(){
    cout<<"Name:"<<name<<"  Tel:"<<tel<<endl;
}
void CommEntry::setName(string nm){
    cout<<"NewName:";
    cin>>nm;
    name=nm;
}
void CommEntry::setTel(string t){
    cout<<"NewTel:";
    cin>>t;
    tel=t;
}

#endif

主函数部分

main.cpp文件

#include<iostream>
#include"CommEntry.h"
using namespace std;
void content()
{
    cout<<"==========================="<<endl;
    cout<<"1.输入通讯录条目"<<endl;
    cout<<"2.输出通讯录条目"<<endl;
    cout<<"3.修改姓名"<<endl;
    cout<<"4.修改电话"<<endl;
    cout<<"0.退出"<<endl;
    cout<<"============================"<<endl;
    cout<<"请输入(0-4):";
}
int main(){
    system("chcp 65001");
    int n;
    CommEntry one;
    while(1){
        content();
        cin>>n;
        switch(n){
            case 0:return 0;
            case 1:{
                one.input();
                break;
            }
            case 2:{
                one.output();
                cout<<"按回车键继续";
                getchar();
                getchar();
                break;
            }
            case 3:{
                string newn;
                one.setName(newn);
                break;
            }
            case 4:{
                string newt;
                one.setTel(newt);
                break;
            }
            default:{
                cout<<"Error!!!"<<endl;
                break;
            }
        }
    }
    return 0;
}

总结

该实验是熟悉面向对象编程的开始,熟悉使用封装、UML图、定义头文件。

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的通讯录管理程序的示例代码,其中包含了构造函数、析构函数以及对通讯录条目的输入、输出、修改姓名和修改电话的操作: ```cpp #include <iostream> #include <string> #include <vector> using namespace std; class Contact { private: string name; vector<string> phones; public: Contact(string name, vector<string> phones) { this->name = name; this->phones = phones; } ~Contact() {} void setName(string name) { this->name = name; } void setPhones(vector<string> phones) { this->phones = phones; } string getName() { return name; } vector<string> getPhones() { return phones; } void print() { cout << "Name: " << name << endl; cout << "Phones: "; for (int i = 0; i < phones.size(); i++) { cout << phones[i] << " "; } cout << endl; } }; class AddressBook { private: vector<Contact> contacts; public: AddressBook() {} ~AddressBook() {} void addContact(Contact contact) { contacts.push_back(contact); } void deleteContact(int index) { contacts.erase(contacts.begin() + index); } void modifyName(int index, string name) { contacts[index].setName(name); } void modifyPhones(int index, vector<string> phones) { contacts[index].setPhones(phones); } void print() { for (int i = 0; i < contacts.size(); i++) { contacts[i].print(); } } }; int main() { AddressBook addressBook; vector<string> phones1 = {"123456789", "987654321"}; vector<string> phones2 = {"111111111", "222222222", "333333333"}; Contact contact1("Alice", phones1); Contact contact2("Bob", phones2); addressBook.addContact(contact1); addressBook.addContact(contact2); addressBook.print(); addressBook.modifyName(0, "Emily"); addressBook.modifyPhones(1, {"444444444"}); addressBook.print(); addressBook.deleteContact(0); addressBook.print(); return 0; } ``` 在上述代码中,`Contact` 类表示通讯录条目,包含姓名和多个电话号码。`AddressBook` 类表示整个通讯录,包含多个 `Contact` 对象。构造函数和析构函数在两个类中都被定义为空函数。`Contact` 类中的 `print()` 函数用于输出通讯录条目的信息。`AddressBook` 类中的函数实现了添加、删除、修改通讯录条目的操作,并提供了一个 `print()` 函数用于输出整个通讯录的信息。 在 `main()` 函数中,首先创建了两个 `Contact` 对象,并将它们添加到 `AddressBook` 对象中。然后输出整个通讯录的信息,并分别修改第一个通讯录条目的姓名和第通讯录条目的电话号码。最后删除第一个通讯录条目,并再次输出整个通讯录的信息。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值