菜鸟学习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;
}
- 菜鸟学习c++—实现系统权限管理功能(类的public和private应用)
- object-c的存取权限(public/protected/private)
- 菜鸟学习c++—实现学生登陆管理系统
- OC学习中关于@private@protected@pakege@public的访问权限和继承问题
- swift学习记录(private和public)
- Java的public、protected、default和private权限
- public,protected,default和private的权限总结
- 学习记录:系统接口功能配置及应用权限管理
- JavaScript实现类的private、public
- Objective-C 访问权限 public private protect
- C++中的public、protected与private的类成员权限设置和继承方式
- 类的访问权限 public,protect,private,final
- c++ private public protected-->类的访问权限小结
- java类的访问权限问题(public default private protected)
- 功能权限和数据权限管理的实现
- private,protected,public的访问权限
- C++的public,private,protected权限
- JAVA的public,private,protected访问权限
- Cannot change version of project facet Dynamic Web Module to 2.5.
- 64位ubuntu在安装build-essential遇到依赖问题的解决办法
- 设计模式之2---代理模式
- OC时间格式转换方法
- 3分钟,实现友盟第三方分享与登陆-- -一键分享
- 菜鸟学习c++—实现系统权限管理功能(类的public和private应用)
- 求二叉树深度和copy二叉树
- Git 学习笔记
- 创建型设计模式
- HDU 1458Tax Avoidance
- 剑指off-归并两个有序链表
- HTTP协议:pipeline、持久连接、非持久连接
- Hive-分桶与排序
- 给美丽的生命点个赞