菜鸟学习c++—实现系统权限管理功能(类的public和private应用)

来源:互联网 发布:牛仔衬衫女淘宝 编辑:程序博客网 时间:2024/06/05 00:26

面向对象语言中类的3大特征之一—封装 的意义在于实现隐藏类的具体细节。但是为什么要把类的属性设置为private,而把类的成员函数置为public呢?

我的理解是为了提高安全性,防止用户随意修改类对象的属性值。

假设我们有名为user的类,如下所示:

#include <string>

usingnamespace std;

class user

{

public:

    user()=default;                                //默认构造函数

    user(string s,string p,int r):            //构造函数1

    id(s),password(p),root(r){}

    user(string s,string p):                 //构造函数2        方法

    id(s),password(p){}

    void set_root(int i);                      //设置权限

    void reset_password();              //重置密码

    int get_root();                           //返回权限

    string get_password();         //返回密码

    void grant(user&u);            //授权函数(如管理员给用户授权)

private:

    stringid;                           //使用者id

    string password;             //密码                    属性

    int root;                          //权限

};

假设我们有user类对象student,如果我们把属性:id,password,root全置为public,那么用户可轻易通过student.root=i随意修改其权限属性,但是我们把属性置为私有,对象就不能直接访问类的私有成员。只有类的公有成员能直接访问私有成员,而类对象只能通过其共有成员间接使用其私有成员。我们可以通过类中的公有成员如   void set_root(int i), void grant(user&u)来给类的root属性重新赋值。

根据以上的这些特点,我们只需在用户使用修改类属性的方法前设置使用权限级别(只有满足权限级别的用户才能执行此操作)就能实现用户的权限控制

user类的方法具体实现代码如下:

#include <iostream>

#include "user.h"

using namespacestd;

void user::set_root(int i)

{

    this->root=i;

    

}


int user::get_root()

{

    returnroot;

}

void user::reset_password()

{

   this->password="12345";

}

string user::get_password()

{

    returnpassword;

}

void user::grant(user&u)            // 注意:形参必须为user类型的引用,因为要直接修改另一类对象的属性值

{                                        

    int i;

    cout<<"请输入给予权限级别:"<<flush;

    cin>>i;

    u.set_root(i);

}


系统权限管理实现源码如下所示:

#include <iostream>

#include <string>

#include "user.h"

using namespacestd;

user student=user("student","111");              //一个用于测试的student对象

int main()

{

    void reset_password(user sample);

    void input_grade(user sample);

    void administer_grant(user sample);        //管理员给用户授权

    user create(int i);

    user test;

    int option1;

      int option;

    cout<<"1.学生身份进入  2.教师身份进入"<<endl;             // 设置进入系统的用户身份

    cin>>option1;

    test=create(option1);                                                         //根据输入构造类对象

    cout<<"       欢迎登陆教务在线系统"<<endl;

    cout<<"==================="<<endl;

    cout<<"            1.重置密码"<<endl;

    cout<<"            2.成绩录入"<<endl;

    cout<<"            3.授予权限"<<endl;

    cout<<"             (4退出)"<<endl;

    while(1)

    {

    cin>>option;

    switch (option) {

        case1:

            reset_password(test);

            break;

            case2:

            input_grade(test);

            break;

            case3:

            administer_grant(test);

            break;

            case4:

            break;

        default:

            cout<<"你的输入无效!"<<endl;

            break;

    }

    if(option==4)

    {

        cout<<"退出成功"<<endl;

        break;

    }

}

}

void reset_password(user sample)

{

    if (sample.get_root()>=0) {

        sample.reset_password();

        cout<<"密码已重置为:"<<sample.get_password()<<endl;

    }else

    {

        cout<<"你没有权限进行此操作,请与管理员联系解决!"<<endl;

    }

    

}

void input_grade(user sample)

{

    if (sample.get_root()>0) {

        cout<<"请选择输入项目!"<<endl;

    }else

    {

        cout<<"你没有权限进行此操作,请与管理员联系解决!"<<endl;

    }


}

   user create(int i)

{

    user sample;

    if (i==1) {

        sample=user("student","0000");

        cout<<"权限信息:"<<sample.get_root()<<endl;

    }else

    {

          sample=user("teacher","111", 1);

        cout<<"权限信息:"<<sample.get_root()<<endl;

    }

    return sample;


}

void administer_grant(user sample)

{

    if (sample.get_root()>0) {

        cout<<"授权对象旧权限级别为:"<<student.get_root()<<endl;

        sample.grant(student);                                                                           //给用于测试的student对象授权

        cout<<"授权对象新权限级别为"<<student.get_root()<<endl;

    }

    else

        cout<<"你没有权限进行此操作,请与管理员联系解决!"<<endl;

}



0 0
原创粉丝点击