1 封装的意义

  • 属性行为作为一个整体,表现生活中的事物
  • 将属性和行为加以权限控制

1.1 封装意义一

封装意义一:在设计类的时候,属性和行为写在一起,表现事物

语法: class 类名{ 访问权限: 属性 / 行为 };

示例1:设计一个圆类,求圆的周长

  1. #include<iostream>
  2. using namespace std;
  3. //圆周率
  4. const double PI = 3.14;
  5. //1、封装的意义
  6. //将属性和行为作为一个整体,用来表现生活中的事物
  7. //封装一个圆类,求圆的周长
  8. //class代表设计一个类,后面跟着的是类名
  9. class Circle
  10. {
  11. public: //访问权限 公共的权限
  12. //属性
  13. int m_r;//半径
  14. //行为
  15. //获取到圆的周长
  16. double calculateZC()
  17. {
  18. //2 * pi * r
  19. //获取圆的周长
  20. return 2 * PI * m_r;
  21. }
  22. };
  23. int main() {
  24. //通过圆类,创建圆的对象
  25. // c1就是一个具体的圆
  26. Circle c1;
  27. c1.m_r = 10; //给圆对象的半径 进行赋值操作
  28. //2 * pi * 10 = = 62.8
  29. cout << "圆的周长为: " << c1.calculateZC() << endl;
  30. return 0;
  31. }

运行结果:
image.png

示例2:设计一个学生类,属性有姓名和学号,可以给姓名和学号赋值,可以显示学生的姓名和学号

  1. #include<iostream>
  2. using namespace std;
  3. //学生类
  4. class Student {
  5. public:
  6. void setName(string name) {
  7. m_name = name;
  8. }
  9. void setID(int id) {
  10. m_id = id;
  11. }
  12. void showStudent() {
  13. cout << "name:" << m_name << " ID:" << m_id << endl;
  14. }
  15. public:
  16. string m_name;
  17. int m_id;
  18. };
  19. int main() {
  20. Student stu;
  21. stu.setName("德玛西亚");
  22. stu.setID(250);
  23. stu.showStudent();
  24. return 0;
  25. }

运行结果:
image.png

1.2. 封装意义二

封装意义二:类在设计时,可以把属性和行为放在不同的权限下,加以控制

访问权限有三种:

  • public 公共权限
  • protected 保护权限
  • private 私有权限

示例:

  1. #include<iostream>
  2. using namespace std;
  3. //三种权限
  4. //公共权限 public 类内可以访问 类外可以访问
  5. //保护权限 protected 类内可以访问 类外不可以访问
  6. //私有权限 private 类内可以访问 类外不可以访问
  7. class Person
  8. {
  9. //姓名 公共权限
  10. public:
  11. string m_Name;
  12. //汽车 保护权限
  13. protected:
  14. string m_Car;
  15. //银行卡密码 私有权限
  16. private:
  17. int m_Password;
  18. public:
  19. void func()
  20. {
  21. m_Name = "张三";
  22. m_Car = "拖拉机";
  23. m_Password = 123456;
  24. }
  25. };
  26. int main() {
  27. Person p;
  28. p.m_Name = "李四";
  29. //p.m_Car = "奔驰"; //保护权限类外访问不到
  30. //p.m_Password = 123; //私有权限类外访问不到
  31. return 0;
  32. }

2 struct和class区别

在C++中 struct和class唯一的区别就在于 默认的访问权限不同

  • struct 默认权限为公共
  • class 默认权限为私有

示例:

  1. #include<iostream>
  2. using namespace std;
  3. class C1
  4. {
  5. int m_A; //默认是私有权限
  6. };
  7. struct C2
  8. {
  9. int m_A; //默认是公共权限
  10. };
  11. int main() {
  12. C1 c1;
  13. c1.m_A = 10; //错误,访问权限是私有
  14. C2 c2;
  15. c2.m_A = 10; //正确,访问权限是公共
  16. return 0;
  17. }

3 成员属性设置为私有

优点:

  • 将所有成员属性设置为私有,可以自己控制读写权限
  • 对于写权限,我们可以检测数据的有效性

示例:

  1. #include<iostream>
  2. #include<string>
  3. using namespace std;
  4. class Person {
  5. public:
  6. //姓名设置可读可写
  7. void setName(string name) {
  8. m_Name = name;
  9. }
  10. string getName()
  11. {
  12. return m_Name;
  13. }
  14. //获取年龄
  15. int getAge() {
  16. return m_Age;
  17. }
  18. //设置年龄
  19. void setAge(int age) {
  20. if (age < 0 || age > 150) {
  21. cout << "你个老妖精!" << endl;
  22. return;
  23. }
  24. m_Age = age;
  25. }
  26. //情人设置为只写
  27. void setLover(string lover) {
  28. m_Lover = lover;
  29. }
  30. private:
  31. string m_Name; //可读可写 姓名
  32. int m_Age; //只读 年龄
  33. string m_Lover; //只写 情人
  34. };
  35. int main() {
  36. Person p;
  37. //姓名设置
  38. p.setName("张三");
  39. cout << "姓名: " << p.getName() << endl;
  40. //年龄设置
  41. p.setAge(50);
  42. cout << "年龄: " << p.getAge() << endl;
  43. //情人设置
  44. p.setLover("苍井");
  45. //cout << "情人: " << p.m_Lover << endl; //只写属性,不可以读取
  46. return 0;
  47. }

运行结果:
image.png

练习案例1:设计立方体类
设计立方体类(Cube),求出立方体的面积和体积,分别用全局函数和成员函数判断两个立方体是否相等。
参考代码:

  1. #include<iostream>
  2. using namespace std;
  3. bool PanDuan(Cube temp)
  4. {
  5. int a, b, c;
  6. temp.Get(a, b, c);
  7. if((a*b) == (l*w) && (a*b*c) == (l*w*h)){
  8. return true;
  9. }
  10. else{
  11. return false;
  12. }
  13. }
  14. class Cube {
  15. int l, w, h;
  16. public:
  17. bool Set(){
  18. cout << "请输入长宽高:" << endl;
  19. cin >> l >> w >> h;
  20. if((l >= 1) && (w >= 1) && (h >= 1)){
  21. return true;
  22. }
  23. else{
  24. cout << "输入错误!" << endl;
  25. return false;
  26. }
  27. }
  28. void Get(int& L, int& W, int& H){
  29. L = l; W = w; H =h;
  30. }
  31. int Area(){
  32. return l*w;
  33. }
  34. int Volume(){
  35. return l*w*h;
  36. }
  37. bool PanDuan(Cube temp)
  38. {
  39. int a, b, c;
  40. temp.Get(a, b, c);
  41. if((a*b) == (l*w) && (a*b*c) == (l*w*h)){
  42. return true;
  43. }
  44. else{
  45. return false;
  46. }
  47. }
  48. }

练习案例2:点和圆的关系
设计一个圆形类(Circle),和一个点类(Point),计算点和圆的关系。
参考代码:

  1. #include<iostream>
  2. using namespace std;
  3. //圆周率
  4. const double PI = 3.14;
  5. class Point
  6. {
  7. int x;
  8. int y;
  9. public:
  10. Point()
  11. {
  12. x = 0;
  13. y = 0;
  14. }
  15. void set(int a, int b)
  16. {
  17. x = a;
  18. y = b;
  19. }
  20. void get(Point &a)
  21. {
  22. a.set(x, y);
  23. }
  24. };
  25. class Circle
  26. {
  27. public: //访问权限 公共的权限
  28. //属性
  29. int m_r;//半径
  30. //行为
  31. //获取到圆的周长
  32. double calculateZC()
  33. {
  34. //2 * pi * r
  35. //获取圆的周长
  36. return 2 * PI * m_r;
  37. }
  38. };
  39. int main() {
  40. //通过圆类,创建圆的对象
  41. // c1就是一个具体的圆
  42. Circle c1;
  43. c1.m_r = 10; //给圆对象的半径 进行赋值操作
  44. //2 * pi * 10 = = 62.8
  45. cout << "圆的周长为: " << c1.calculateZC() << endl;
  46. return 0;
  47. }