C++的特质——封装

本文通过一个C++实现的学生信息管理系统实例,详细解释了封装的概念。封装隐藏了类的内部实现,只对外提供接口进行交互。文中展示了如何创建一个`student`类,包括成员变量和成员函数,以及如何在类外定义和实现这些函数,实现了增删查改等操作。
摘要由CSDN通过智能技术生成

封装

  • 定义:隐藏对象的属性和实现细节,仅对外公开接口和对象进行交互,将数据和操作数据的方法进行有机结合。
  • 封装隐藏了实现的细节,实现代码模块化
  • 把实现的过程和数据全部隐藏,使用者只能进行调用,无法对过程进行修改 。
  • 举例来说,就像买好的笔记本电脑里的配件,CPU,内存条,风扇,硬盘之类的,我们去使用电脑的时候,只要能去使用他,或者在使用功能的时候,它能够起作用就可以了,并不需要知道它们的运作的原理。只关心使用方式,怎么实现的不用管
  • 把客观事物封装成抽象的类,并且类可以把自己的数据和方法只让可信的类或者对象操作,对不可信的进行信息隐藏。

这里的封装仅为我个人理解,因为问了别人产生了理解的分歧,写这篇博客主要为了做一个理解的记录,如发现有错误,请指出,会即刻修改

举例:

//stu.h
//这里把要实现的细节放在类外实现,可调用的参数与函数作为.h文件引用去调用
#include <iostream>
#include <fstream>
#include <list>
#include <string>

using namespace std;

class student
{
public:
    string name;
    int id;
    int age;
    student(string &m_name, int &m_id, int &m_age); //有参构造函数
    student(const student &s);                      //拷贝构造函数
    void display();                                 //输出信息
    student &operator=(const student &st1)          //重载=
    {
        this->name = st1.name;
        this->id = st1.id;
        this->age = st1.age;
        return *this;
    }
    bool operator==(const student &st1) const //重载==
    {
        return (id == st1.id) ? 1 : 0;
    }
    bool operator<(const student &st1) const
    {
        return (id < st1.id) ? 1 : 0;
    }
    bool operator>(const student &st1) const
    {
        return (id > st1.id) ? 1 : 0;
    }
};

void display(list<student> &m_list); //学生链表输出
void printout();                     //输出文件内学生信息
void show();                         //系统初始界面
void find();                         //查找
void sort();                         //排序
void insert();                       //增加
void deletes();                      //删除
void update();                       //修改
//stu.cpp
//这里类里的函数进行了定义,用户只看得到.h文件,看不到这个
#include "stu.h"
list<student> my_list; //学生链表

student::student(string &m_name, int &m_id, int &m_age)
{
    this->name = m_name;
    this->id = m_id;
    this->age = m_age;
}

student::student(const student &s)
{
    this->name = s.name;
    this->id = s.id;
    this->age = s.age;
}

void student::display()
{
    cout << "name  :" << name << endl;
    cout << "id    :" << id << endl;
    cout << "age   :" << age << endl;
    cout << "-----------" << endl;
}

void print(list<student> &m_list)
{
    auto it = m_list.begin(); //c++11先语法
    for (it; it != m_list.end(); ++it)
    {
        it->display();
    }
}

void show()
{
    cout << "************" << endl;
    cout << "1,增加学生信息" << endl;
    cout << "2,删除学生信息" << endl;
    cout << "3,查找学生信息" << endl;
    cout << "4,修改学生信息" << endl;
    cout << "5,显示学生信息" << endl;
    cout << "6,排序学生信息" << endl;
    cout << "7,  刷新界面 " << endl;
    cout << "8,  退出系统 " << endl;
    cout << "************" << endl;
}

void find()
{
    int flag = 0;
    cout << "输入要查询的id:";
    int id1;
    cin >> id1;
    auto it = my_list.begin();
    for (; it != my_list.end(); ++it)
    {
        if (id1 == it->id)
        {
            cout << "查询到此人信息:" << endl;
            it->display();
            flag = 1;
        }
        break;
    }
    if (flag == 0)
    {
        cout << "未查到此人信息" << endl;
    }
}

void sort()
{
    system("clear");
    my_list.sort();
    auto it = my_list.begin();
    for (; it != my_list.end(); ++it)
    {
        it->display();
    }
    cout << "排序完成!" << endl;
}

void insert()
{
    system("clear");
    string m_name;
    int m_id, m_age;
    cout << "姓名:";
    cin >> m_name;
    cout << "学号:";
    cin >> m_id;
    cout << "年龄:";
    cin >> m_age;
    student a(m_name, m_id, m_age);
    my_list.push_back(a);
    ofstream file("student.dat", ios::out);
    if (!file)
    {
        std::cout << "文件打开失败" << std::endl;
        return;
    }
    for (auto it = my_list.begin(); it != my_list.end(); ++it)
    {
        file << it->name << ' ' << it->id << ' ' << it->age << endl;
    }
    cout << "已加入信息列表";
}

void printout()
{
    ifstream infile("student.dat", ios::in | ios::binary);
    if (!infile)
    {
        std::cout << "文件打开失败" << endl;
    }
    for (auto it = my_list.begin(); it != my_list.end(); ++it)
    {
        string str1;
        int id, age;
        infile >> str1 >> id >> age;
        cout << "名字 " << str1 << endl;
        cout << "学号" << id << endl;
        cout << "年龄" << age << endl;
        cout << "**************" << endl;
    }
}

void deletes()
{
    system("clear");
    cout << "输入要删除的学生学号" << endl;
    int flag = 0;
    cout << "输入要删除的id:";
    int id1;
    cin >> id1;
    auto it = my_list.begin();
    for (; it != my_list.end(); it++)
    {
        if (id1 == it->id)
        {
            cout << "查询到此人信息:" << endl;
            it->display();
            flag = 1;
        }
        break;
    }
    if (flag == 0)
    {
        cout << "未查到此人信息" << endl;
    }
    cout << "是否确认删除(Y or N)";
    char ch;
    cin >> ch;
    if (ch == 'Y' || ch == 'y')
    {
        my_list.erase(it);
        cout << "已删除" << endl;
    }
    else if (ch == 'N' || ch == 'n')
    {
        cout << "已取消" << endl;
    }
    else
    {
        cout << "输入错误" << endl;
    }
}

void update()
{
    system("clear");
    cout << "输入要修改的学生学号" << endl;
    int flag = 0;
    cout << "输入要修改的id:";
    int id1;
    cin >> id1;
    auto it = my_list.begin();
    for (; it != my_list.end(); it++)
    {
        if (id1 == it->id)
        {
            cout << "查询到此人信息:" << endl;
            it->display();
            flag = 1;
        }
        break;
    }
    if (flag == 0)
    {
        cout << "未查到此人信息" << endl;
    }
    cout << "是否确认修改(Y or N)";
    char ch;
    cin >> ch;
    if (ch == 'Y' || ch == 'y')
    {
        my_list.erase(it);
        insert();
        cout << "已修改" << endl;
    }
    else if (ch == 'N' || ch == 'n')
    {
        cout << "已取消" << endl;
    }
    else
    {
        cout << "输入错误" << endl;
    }
}
//main.cpp
//用户的操作界面
#include "stu.h"

int main()
{
    cout << "欢迎进入学生信息管理系统" << endl;
    char c;
    show();
    while (cin >> c)
    {
        switch (c)
        {
        case '1':
            insert();
            break;
        case '2':
            deletes();
            break;
        case '3':
            find();
            break;
        case '4':
            update();
            break;
        case '5':
            printout();
            break;
        case '6':
            sort();
            break;
        case '7':
            system("clear");
            break;
        case '8':
            return 0;
        default:
            break;
        }
        show();
    }
    cout << "系统退出!" << endl;
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值