【C++】 15_类于封装的概念

发布时间:2019-06-17 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了【C++】 15_类于封装的概念脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

类的组合

脑一般而言是由 CPU, 内存,主板,键盘和硬盘等部件组合而成。

clipboard.png

思考:

  1. 学习电脑组装需要多少时间?
  2. 学习电脑组装是否需要学习显示器,键盘,鼠标,主板,内存等部件的设计与制造?

类的封装

  • 类通常分为以下两个部分

    • 类的实现细节
    • 类的使用方式

clipboard.png

  • 当使用类时,不需要关心其实现细节
  • 当创建类时,才需要考虑其内部实现细节

例:

  • 普通用户使用手机

  • 手机开发工程师

    • 需要考虑手机内部的实现细节

封装的基本概念

  • 根据经验: 并不是类的每个属性都是对外开放的

    • 如: 女孩子不希望外人知道自己的体重和年龄
    • 如: 男孩子不希望别人知道自己的身高和收入
  • 而一些类的属性是对外开放的

    • 如: 人的姓名,学历,国籍,等
  • 必须在类的表示法中定义属性和行为的公开级别

    • 类似文件系统中文件的权限

C++ 中类的封装

  • 成员变量: C++ 中用于表示类属性的变量
  • 成员函数: C++ 中用于表示类行为的函数
  • C++ 中可以给成员变量和成员函数定义访问级别

    • public: 成员变量和成员函数都可以在类的内部和外部访问和调用
    • PRivate: 成员变量和成员函数只能在类的内部被访问和调用

编程实验: 类成员的访问属性

@H_530_126@
PE="button" class="copyCode code-tool" data-toggle="tooltip" data-placement="top" data-clipboard-text="#include struct BioLOGy { bool living; }; struct Animal : Biology { bool movable; void foiDFood() { }; }; struct Plant : Biology { bool growable; }; struct Beast : Animal { void sleep() { }; }; struct Human : Animal { void sleep() { }; void work() { }; }; struct Girl : Human { private: int age; int weight; public: void print() { age = 22; weight = 48; printf("I am a girl, I am %d years old.n", age); printf(";my weight is %d kg.n", age); } }; struct Boy : Human { private: int height; int salary; public: int age; int weight; void print() { height = 175; salary = 9000; printf("I am a boy, my height is %d cm.n", height); printf("My salary is %d RMB.n", salary); } }; int main() { Girl g; Boy b; g.print(); b.age = 19; b.weight = 120; b.print(); return 0; }" tITle="" data-original-title="复制">
#include <stdio.h>

struct Biology {
    bool living;
};

struct Animal : Biology {
    bool movable;
    void foidFood() { };
};

struct Plant : Biology {
    bool growable;
};

struct Beast : Animal {
    void sleep() { };
};

struct Human : Animal {
    void sleep() { };
    void work() { };
};

struct Girl : Human
{
private:
    int age;
    int weight;
public:
    void print()
    {
        age = 22;
        weight = 48;
        
        printf("I am a girl, I am %d years old.n", age);
        printf("My weight is %d kg.n", age);
    }
};

struct Boy : Human
{
private:
    int height;
    int salary;
public:
    int age;
    int weight;
    
    void print()
    {
        height = 175;
        salary = 9000;
        
        printf("I am a boy, my height is %d cm.n", height);
        printf("My salary is %d RMB.n", salary);
    }
};

int main()
{
    Girl g;
    Boy b;
    
    g.print();
    
    b.age = 19;
    b.weight = 120;
    
    b.print();

    return 0;
}
输出:
I am a girl, I am 22 years old.
My weight is 22 kg.
I am a boy, my height is 175 cm.
My salary is 9000 RMB.

类成员的作用域

  • 类成员的作用域只在类的内部,外部无法直接访问
  • 成员函数可以直接访问成员变量和调用成员函数
  • 类的外部可以通过类变量访问 public 成员
  • 类成员的作用域与访问级别没有关系

C++ 中用 struct 定义的类中所有成员默认为 public

编程实验:类成员的作用域

#include <stdio.h>

int i = 1;

struct Test
{
private:
    int i;
public:
    int j;
    
    int getI()
    {
        i = 3;
        
        return i;
    }
};

int main()
{
    int i = 2;
    
    Test test;
    
    test.j = 4;
    
    printf("i = %dn", i);
    printf("::i = %dn", ::i);
    // printf("test.i = %dn", test.i);  // Error
    printf("j = %dn", test.j);
    printf("test.getI() = %dn", test.getI());
    
    return 0;
}
输出:
i = 2
::i = 1
j = 4
test.getI() = 3

如果想访问类的成员变量或成员函数,都必须通过这个类的对象来进行,是否访问成功,需要看类成员变量或成员函数的访问级别。
对类而言是有作用域的,仅仅指的是类的成员变量和成员函数的作用域。
作用域与访问级别是不同的概念。

小结

  • 类通常可以分为使用方式和内部细节两部分
  • 类的封装机制使得使用方式和内部细节相分离
  • C++ 中通过定义类成员的访问级别实现封装机制
  • public 成员可以在类的内部和外界访问和调用
  • private 成员只能在类的内部被访问和调用

以上内容参考狄泰软件学院系列课程,请大家保护原创

脚本宝典总结

以上是脚本宝典为你收集整理的【C++】 15_类于封装的概念全部内容,希望文章能够帮你解决【C++】 15_类于封装的概念所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。