数据封装是面向对象编程的一个重要特点,它防止函数直接访问类类型的内部成员。类成员的访问限制是通过在类主体内部对各个区域标记 public、private、protected 来指定的。关键字 public、private、protected 称为访问修饰符。 一个类可以有多个 public、protected 或 private 标记区域。每个标记区域在下一个标记区域开始之前或者在遇到类主体结束右括号之前都是有效的。成员和类的默认访问修饰符是 private

访问修饰符

  1. class Base {
  2. public: // 公有成员
  3. protected: // 受保护成员
  4. private: // 私有成员
  5. };

public 公有成员

公有成员在程序中类的外部是可访问的。您可以不使用任何成员函数来设置和获取公有变量的值,如下所示:

  1. #include <iostream>
  2. using namespace std;
  3. class Line
  4. {
  5. public:
  6. double length;
  7. void setLength( double len );
  8. double getLength( void );
  9. };
  10. // 成员函数定义
  11. double Line::getLength(void)
  12. {
  13. return length ;
  14. }
  15. void Line::setLength( double len )
  16. {
  17. length = len;
  18. }
  19. // 程序的主函数
  20. int main( )
  21. {
  22. Line line;
  23. // 设置长度
  24. line.setLength(6.0);
  25. cout << "Length of line : " << line.getLength() <<endl;
  26. // 不使用成员函数设置长度
  27. line.length = 10.0; // OK: 因为 length 是公有的
  28. cout << "Length of line : " << line.length <<endl;
  29. return 0;
  30. }
  31. /*
  32. Length of line : 6
  33. Length of line : 10
  34. */

protected 受保护成员

protected(受保护)成员变量或函数与私有成员十分相似,但有一点不同,protected(受保护)成员在派生类(即子类)中是可访问的。
在下一个章节中,您将学习到派生类和继承的知识。现在您可以看到下面的实例中,我们从父类 Box 派生了一个子类 smallBox
下面的实例与前面的实例类似,在这里 width 成员可被派生类 smallBox 的任何成员函数访问。

  1. #include <iostream>
  2. using namespace std;
  3. class Box
  4. {
  5. protected:
  6. double width;
  7. };
  8. class SmallBox:Box // SmallBox 是派生类
  9. {
  10. public:
  11. int height;
  12. void setSmallWidth( double wid );
  13. double getSmallWidth( void );
  14. };
  15. // 子类的成员函数
  16. double SmallBox::getSmallWidth(void)
  17. {
  18. return width ;
  19. }
  20. void SmallBox::setSmallWidth( double wid )
  21. {
  22. width = wid;
  23. }
  24. // 程序的主函数
  25. int main( )
  26. {
  27. SmallBox box;
  28. // 使用成员函数设置宽度
  29. box.setSmallWidth(5.0);
  30. cout << "Width of box : "<< box.getSmallWidth() << endl;
  31. cout << box.height << endl;
  32. return 0;
  33. }
  34. /*
  35. Width of box : 5
  36. -304519264
  37. */

private 私有成员

私有成员变量或函数在类的外部是不可访问的,甚至是不可查看的。只有类和友元函数可以访问私有成员。
默认情况下,类的所有成员都是私有的。例如在下面的类中,width 是一个私有成员,这意味着,如果您没有使用任何访问修饰符,类的成员将被假定为私有成员:

  1. class Box
  2. {
  3. double width;
  4. public:
  5. double length;
  6. void setWidth( double wid );
  7. double getWidth( void );
  8. };

实际操作中,我们一般会在私有区域定义数据,在公有区域定义相关的函数,以便在类的外部也可以调用这些函数,如下所示:

  1. #include <iostream>
  2. using namespace std;
  3. class Box
  4. {
  5. public:
  6. double length;
  7. void setWidth( double wid );
  8. double getWidth( void );
  9. private:
  10. double width;
  11. };
  12. // 成员函数定义
  13. double Box::getWidth(void)
  14. {
  15. return width ;
  16. }
  17. void Box::setWidth( double wid )
  18. {
  19. width = wid;
  20. }
  21. // 程序的主函数
  22. int main( )
  23. {
  24. Box box;
  25. // 不使用成员函数设置长度
  26. box.length = 10.0; // OK: 因为 length 是公有的
  27. cout << "Length of box : " << box.length <<endl;
  28. // 不使用成员函数设置宽度
  29. // box.width = 10.0; // Error: 因为 width 是私有的
  30. box.setWidth(10.0); // 使用成员函数设置宽度
  31. cout << "Width of box : " << box.getWidth() <<endl;
  32. return 0;
  33. }
  34. /*
  35. Length of box : 10
  36. Width of box : 10
  37. */

public protected private 继承中的特点

有public, protected, private三种继承方式,它们相应地改变了基类成员的访问属性

继承方式 基类的public成员 基类的protected成员 基类的private成员 继承引起的访问控制关系变化概括
public继承 仍为public成员 为protected成员 不可见 基类的非私有成员在子类的访问属性不变
protected继承 为protected成员 为protected成员 不可见 基类的非私有成员都为子类的保护成员
private继承 为private成员 为private成员 不可见 基类中的非私有成员都称为子类的私有成员
  • public 继承基类 public 成员,protected 成员,private 成员的访问属性在派生类中分别变成:public, protected, private
  • protected 继承基类 public 成员,protected 成员,private 成员的访问属性在派生类中分别变成:protected, protected, private
  • private 继承基类 public 成员,protected 成员,private 成员的访问属性在派生类中分别变成:private, private, private

但无论哪种继承方式,上面两点都没有改变:

  • private 成员只能被本类成员(类内)和友元访问,不能被派生类访问;
  • protected 成员可以被派生类访问。

public 继承

  1. #include<iostream>
  2. #include<assert.h>
  3. using namespace std;
  4. class A{
  5. public:
  6. int a;
  7. A(){
  8. public_a1 = 1;
  9. protected_a2 = 2;
  10. private_a3 = 3;
  11. a = 4;
  12. }
  13. void fun(){
  14. cout << a << endl; //正确
  15. cout << public_a1 << endl; //正确
  16. cout << protected_a2 << endl; //正确
  17. cout << private_a3 << endl; //正确
  18. }
  19. public:
  20. int public_a1;
  21. protected:
  22. int protected_a2;
  23. private:
  24. int private_a3;
  25. };
  26. class B : public A{
  27. public:
  28. int a;
  29. B(int i){
  30. A();
  31. a = i;
  32. }
  33. void fun(){
  34. cout << a << endl; //✅ 正确,public成员
  35. cout << public_a1 << endl; //✅ 正确,基类的public成员,在派生类中仍是public成员。
  36. cout << protected_a2 << endl; //✅ 正确,基类的protected成员,在派生类中仍是protected可以被派生类访问。
  37. cout << private_a3 << endl; //❌ 错误,基类的private成员不能被派生类访问。
  38. }
  39. };
  40. int main(){
  41. B b(10);
  42. cout << b.a << endl;
  43. cout << b.public_a1 << endl; //✅ 正确
  44. cout << b.protected_a2 << endl; //❌ 错误,类外不能访问protected成员
  45. cout << b.private_a3 << endl; //❌ 错误,类外不能访问private成员
  46. system("pause");
  47. return 0;
  48. }

protected 继承

  1. #include<iostream>
  2. #include<assert.h>
  3. using namespace std;
  4. class A{
  5. public:
  6. int a;
  7. A(){
  8. public_a1 = 1;
  9. protected_a2 = 2;
  10. private_a3 = 3;
  11. a = 4;
  12. }
  13. void fun(){
  14. cout << a << endl; //正确
  15. cout << public_a1 << endl; //正确
  16. cout << protected_a2 << endl; //正确
  17. cout << private_a3 << endl; //正确
  18. }
  19. public:
  20. int public_a1;
  21. protected:
  22. int protected_a2;
  23. private:
  24. int private_a3;
  25. };
  26. class B : protected A{
  27. public:
  28. int a;
  29. B(int i){
  30. A();
  31. a = i;
  32. }
  33. void fun(){
  34. cout << a << endl; //✅ 正确,public成员。
  35. cout << public_a1 << endl; //✅ 正确,基类的public成员,在派生类中变成了protected,可以被派生类访问。
  36. cout << protected_a2 << endl; //✅ 正确,基类的protected成员,在派生类中还是protected,可以被派生类访问。
  37. cout << private_a3 << endl; //❌ 错误,基类的private成员不能被派生类访问。
  38. }
  39. };
  40. int main(){
  41. B b(10);
  42. cout << b.a << endl; //✅ 正确。public成员
  43. cout << b.public_a1 << endl; //❌ 错误,protected成员不能在类外访问。
  44. cout << b.protected_a2 << endl; //❌ 错误,protected成员不能在类外访问。
  45. cout << b.private_a3 << endl; //❌ 错误,private成员不能在类外访问。
  46. system("pause");
  47. return 0;
  48. }

private 继承

  1. #include<iostream>
  2. #include<assert.h>
  3. using namespace std;
  4. class A{
  5. public:
  6. int a;
  7. A(){
  8. public_a1 = 1;
  9. protected_a2 = 2;
  10. private_a3 = 3;
  11. a = 4;
  12. }
  13. void fun(){
  14. cout << a << endl; //正确
  15. cout << public_a1 << endl; //正确
  16. cout << protected_a2 << endl; //正确
  17. cout << private_a3 << endl; //正确
  18. }
  19. public:
  20. int public_a1;
  21. protected:
  22. int protected_a2;
  23. private:
  24. int private_a3;
  25. };
  26. class B : private A{
  27. public:
  28. int a;
  29. B(int i){
  30. A();
  31. a = i;
  32. }
  33. void fun(){
  34. cout << a << endl; //✅ 正确,public成员。
  35. cout << public_a1 << endl; //✅ 正确,基类public成员,在派生类中变成了private,可以被派生类访问。
  36. cout << protected_a2 << endl; //✅ 正确,基类的protected成员,在派生类中变成了private,可以被派生类访问。
  37. cout << private_a3 << endl; //❌ 错误,基类的private成员不能被派生类访问。
  38. }
  39. };
  40. int main(){
  41. B b(10);
  42. cout << b.a << endl; //✅ 正确。public成员
  43. cout << b.public_a1 << endl; //❌ 错误,private成员不能在类外访问。
  44. cout << b.protected_a2 << endl; //❌ 错误, private成员不能在类外访问。
  45. cout << b.private_a3 << endl; //❌ 错误,private成员不能在类外访问。
  46. system("pause");
  47. return 0;
  48. }

virtual 虚函数

friend 友元

static 静态