C++核心——类中的权限
C++中权限分为三种等级public、protected、private。主要用于限定类中的成员和继承时继承的方式的选择。
类中成员的权限
- 代码1
#include <iostream>
using namespace std;
class Base
{
private:
int a;
protected:
int b;
public:
int c;
Base(int a, int b, int c)
{
this->a = a;
this->b = b;
this->c = c;
}
void showInfo()
{
cout << this->a << this->b << this->c << endl;
}
};
int main()
{
Base base(1, 2, 3);
base.showInfo();
return 0;
}
*****************************输出结果*********************************
(base) BigfishdeMacBook-Pro:学习笔记 bigfish$ g++ 03_权限.cpp
(base) BigfishdeMacBook-Pro:学习笔记 bigfish$ ./a.out
123
(base) BigfishdeMacBook-Pro:学习笔记 bigfish$
- 全部输出正常,在类中可以访问任意权限成员。
- 代码2
#include <iostream>
using namespace std;
class Base
{
private:
int a;
protected:
int b;
public:
int c;
Base(int a, int b, int c)
{
this->a = a;
this->b = b;
this->c = c;
}
};
int main()
{
Base base(1, 2, 3);
cout << base.c << endl;
cout << base.b << endl;
cout << base.a << endl;
return 0;
}
*************************输出结果********************************
(base) BigfishdeMacBook-Pro:学习笔记 bigfish$ g++ 03_权限.cpp
03_权限.cpp:34:18: error: 'b' is a protected member of 'Base'
cout << base.b << endl;
^
03_权限.cpp:9:9: note: declared protected here
int b;
^
03_权限.cpp:35:18: error: 'a' is a private member of 'Base'
cout << base.a << endl;
^
03_权限.cpp:7:9: note: declared private here
int a;
^
2 errors generated.
(base) BigfishdeMacBook-Pro:学习笔记 bigfish$
- 编译报错,提示了A和B的权限,如果这个代码main函数中后两行注释掉也能正常运行,可见类外只可以访问public权限的成员,protected和private权限的成员都是不可以访问的。
- 代码3-1
#include <iostream>
using namespace std;
class Base
{
private:
int a;
protected:
int b;
public:
int c;
void showInfo()
{
cout << this->a << this->b << this->c << endl;
}
};
class Son: public Base
{
protected:
public:
Son(int a, int b, int c)
{
this->a = a;
this->b = b;
this->c = c;
}
void showInfo()
{
cout <<this->a << this->b << this->c << endl;
}
};
int main()
{
Son s(10, 20, 30);
s.showInfo();
return 0;
}
*****************************运行结果**************************
(base) BigfishdeMacBook-Pro:学习笔记 bigfish$ g++ 03_权限.cpp
03_权限.cpp:25:15: error: 'a' is a private member of 'Base'
this->a = a;
^
03_权限.cpp:7:9: note: declared private here
int a;
^
03_权限.cpp:31:22: error: 'a' is a private member of 'Base'
cout <<this->a << this->b << this->c << endl;
^
03_权限.cpp:7:9: note: declared private here
int a;
^
2 errors generated.
(base) BigfishdeMacBook-Pro:学习笔记 bigfish$
- 编译阶段就报错,提示a 的权限是private。如果将a的代码主食掉,如下:
- 代码3-2
#include <iostream>
using namespace std;
class Base
{
private:
int a;
protected:
int b;
public:
int c;
void showInfo()
{
cout << this->a << this->b << this->c << endl;
}
};
class Son: public Base
{
protected:
public:
Son(int a, int b, int c)
{
// this->a = a;
this->b = b;
this->c = c;
}
void showInfo()
{
cout <<this->b << this->c << endl;
}
};
int main()
{
Son s(10, 20, 30);
s.showInfo();
return 0;
}
*************************运行结果*********************************
(base) BigfishdeMacBook-Pro:学习笔记 bigfish$ g++ 03_权限.cpp
(base) BigfishdeMacBook-Pro:学习笔记 bigfish$ ./a.out
2030
(base) BigfishdeMacBook-Pro:学习笔记 bigfish$
运行结果正常,说明b和c都是可以被继承的,a是不可以被继承,这也就验证了开头给出的规律。
继承的方式
继承的时候在父类的前面也会加上权限,规律如下:
-
公共继承 属性不变
-
保护继承 公共变保护
-
私有继承 都变私有
代码3-2运行正常,但是如果在main函数改为如下
int main()
{
Son s(10, 20, 30);
s.showInfo();
cout << s.c << s.b << endl;
return 0;
}
*************************运行结果*********************************
(base) BigfishdeMacBook-Pro:学习笔记 bigfish$ g++ 03_权限.cpp
03_权限.cpp:39:22: error: 'b' is a protected member of 'Base'
cout << s.c << s.b << endl;
^
03_权限.cpp:9:9: note: declared protected here
int b;
^
1 error generated.
(base) BigfishdeMacBook-Pro:学习笔记 bigfish$
- 运行结果显示b的权限是protected,而c可以在类外使用肯定是public。所以使用public方式继承,protected还是protected,public还是public。当然private就不讨论,上面已经讨论过,因为private类型是不能继承的。
#include <iostream>
using namespace std;
class Base
{
private:
int a;
protected:
int b;
public:
int c;
void showInfo()
{
cout << this->a << this->b << this->c << endl;
}
};
class Son: protected Base
{
protected:
public:
Son(int a, int b, int c)
{
// this->a = a;
this->b = b;
this->c = c;
}
void showInfo()
{
cout <<this->b << this->c << endl;
}
};
int main()
{
Son s(10, 20, 30);
s.showInfo();
cout << s.c << endl;
return 0;
}
*************************运行结果*********************************
(base) BigfishdeMacBook-Pro:学习笔记 bigfish$ g++ 03_权限.cpp
03_权限.cpp:39:13: error: cannot cast 'Son' to its protected base class 'Base'
cout << s.c << endl;
^
03_权限.cpp:18:12: note: declared protected here
class Son: protected Base
^~~~~~~~~~~~~~
03_权限.cpp:39:15: error: 'c' is a protected member of 'Base'
cout << s.c << endl;
^
03_权限.cpp:18:12: note: constrained by protected inheritance here
class Son: protected Base
^~~~~~~~~~~~~~
03_权限.cpp:11:9: note: member is declared here
int c;
^
2 errors generated.
(base) BigfishdeMacBook-Pro:学习笔记 bigfish$
- 从编译的提示信息中可以看出c已经变为了protected类型的了。
#include <iostream>
using namespace std;
class Base
{
private:
int a;
protected:
int b;
public:
int c;
void showInfo()
{
cout << this->a << this->b << this->c << endl;
}
};
class Son: private Base
{
protected:
public:
Son(int a, int b, int c)
{
// this->a = a;
this->b = b;
this->c = c;
}
void showInfo()
{
cout <<this->b << this->c << endl;
}
};
int main()
{
Son s(10, 20, 30);
s.showInfo();
cout << s.c << s.b << endl;
return 0;
}
*************************运行结果*********************************
(base) BigfishdeMacBook-Pro:学习笔记 bigfish$ g++ 03_权限.cpp
03_权限.cpp:39:13: error: cannot cast 'Son' to its private base class 'Base'
cout << s.c << s.b << endl;
^
03_权限.cpp:18:12: note: declared private here
class Son: private Base
^~~~~~~~~~~~
03_权限.cpp:39:15: error: 'c' is a private member of 'Base'
cout << s.c << s.b << endl;
^
03_权限.cpp:18:12: note: constrained by private inheritance here
class Son: private Base
^~~~~~~~~~~~
03_权限.cpp:11:9: note: member is declared here
int c;
^
03_权限.cpp:39:20: error: cannot cast 'Son' to its private base class 'Base'
cout << s.c << s.b << endl;
^
03_权限.cpp:18:12: note: declared private here
class Son: private Base
^~~~~~~~~~~~
03_权限.cpp:39:22: error: 'b' is a private member of 'Base'
cout << s.c << s.b << endl;
^
03_权限.cpp:18:12: note: constrained by private inheritance here
class Son: private Base
^~~~~~~~~~~~
03_权限.cpp:9:9: note: member is declared here
int b;
^
4 errors generated.
(base) BigfishdeMacBook-Pro:学习笔记 bigfish$
- 从编译的提示信息中可以看出b和c的权限已经变为了private类型。
版权声明:本文为wtzhu_13原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。