c++访问私有成员变量和私有成员函数的常用方法

朱雀 2022-12-04 01:21 1060阅读 0赞

类的对象不能直接访问类声明的私有成员变量,否则破坏了信息隐藏的目的。
在C++中,为了防止某些数据成员或成员函数从外部被直接访问,可以将它们声明为private,这样编译器会阻止任何来自外部非友元的直接访问。

1.私有成员变量的四种访问方法

(1)通过公共函数为私有成员赋值

  1. #include <iostream>
  2. using namespace std;
  3. class Test
  4. {
  5. private:
  6. int x, y;
  7. public:
  8. void setX(int a)
  9. {
  10. x=a;
  11. }
  12. void setY(int b)
  13. {
  14. y=b;
  15. }
  16. void print(void)
  17. {
  18. cout<<"x="<<x<<'\t'<<"y="<<y<<endl;
  19. }
  20. } ;
  21. int main()
  22. {
  23. Test p1;
  24. p1.setX(1);
  25. p1.setY(9);
  26. p1.print( );
  27. return 0;
  28. }

(2)利用函数访问私有数据成员

  1. #include <iostream>
  2. using namespace std;
  3. class Test
  4. {
  5. private:
  6. int x,y;
  7. public:
  8. void setX(int a)
  9. {
  10. x=a;
  11. }
  12. void setY(int b)
  13. {
  14. y=b;
  15. }
  16. int getX(void)
  17. {
  18. return x; //返回x值
  19. }
  20. int getY(void)
  21. {
  22. return y; //返回y值
  23. }
  24. };
  25. int main()
  26. {
  27. Test p1;
  28. p1.setX(1);
  29. p1.setY(9);
  30. int a,b;
  31. a=p1.getX( );
  32. b=p1.getY();
  33. cout<<a<<'\t'<<b<<endl;
  34. return 0;
  35. }

(3)利用引用访问私有数据成员

  1. #include <iostream>
  2. using namespace std;
  3. class Test
  4. {
  5. private:
  6. int x,y;
  7. public:
  8. void setX(int a)
  9. {
  10. x=a;
  11. }
  12. void setY(int b)
  13. {
  14. y=b;
  15. }
  16. void getXY(int &px, int &py) //引用
  17. {
  18. px=x; //提取x,y值
  19. py=y;
  20. }
  21. };
  22. int main()
  23. {
  24. Test p1,p2;
  25. p1.setX(1);
  26. p1.setY(9);
  27. int a,b;
  28. p1.getXY(a, b); //将 a=x, b=y
  29. cout<<a<<'\t'<<b<<endl;
  30. return 0;
  31. }

(4)利用指针访问私有数据成员

  1. #include <iostream>
  2. using namespace std;
  3. class Test
  4. {
  5. private:
  6. int x,y;
  7. public:
  8. void setX(int a)
  9. {
  10. x=a;
  11. }
  12. void setY(int b)
  13. {
  14. y=b;
  15. }
  16. void getXY(int *px, int *py)
  17. {
  18. *px=x; //提取x,y值
  19. *py=y;
  20. }
  21. };
  22. int main()
  23. {
  24. Test p1;
  25. p1.setX(1);
  26. p1.setY(9);
  27. int a,b;
  28. p1.getXY(&a,&b); //将 a=x, b=y
  29. cout<<a<<'\t'<<b<<endl;
  30. return 0;
  31. }

详见 https://blog.csdn.net/KgdYsg/article/details/82492797?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522159922886019724839809393%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=159922886019724839809393&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v3~pc_rank_v3-2-82492797.pc_ecpm_v3_pc_rank_v3&utm_term=c%2B%2B%E8%AE%BF%E9%97%AE%E7%A7%81%E6%9C%89private%E6%88%90%E5%91%98%E5%8F%98%E9%87%8F%E7%9A%84%E5%B8%B8%E7%94%A8%E6%96%B9%E6%B3%95&spm=1018.2118.3001.4187

2.在类外调用类的私有成员函数的两种方法

(1).通过类的public成员函数调用private成员函数

  1. #include<iostream>
  2. using namespace std;
  3. class Test
  4. {
  5. public:
  6. void fun2()
  7. {
  8. fun1();
  9. }
  10. private:
  11. void fun1()
  12. {
  13. cout<<"fun1"<<endl;
  14. }
  15. };
  16. int main()
  17. {
  18. Test t;
  19. t.fun2();
  20. return 0;
  21. }

(2).通过类的友元函数调用该类的private成员函数,但是该成员函数必须设为static,这样就可以在友元函数内通过类名调用,否则无法调用

  1. #include<iostream>
  2. using namespace std;
  3. class Test
  4. {
  5. friend void fun2(); //fun2()设为类Test的友元函数
  6. private:
  7. static void fun1()
  8. {
  9. cout<<"fun1"<<endl;
  10. }
  11. };
  12. void fun2()
  13. {
  14. Test::fun1();
  15. }
  16. int main()
  17. {
  18. fun2();
  19. return 0;
  20. }

详见 https://blog.csdn.net/jackchoise030/article/details/86158780

发表评论

表情:
评论列表 (有 0 条评论,1060人围观)

还没有评论,来说两句吧...

相关阅读