zoukankan      html  css  js  c++  java
  • C++--第8课

    第8课 - 类中的封装

    1. 类的组成

    类的实现细节;

    类的使用方式。

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

    例:

    使用手机时,只需要知道如何发短信,打电话,拍照等等即可,不需要关心手机是如何开发出来的。

    手机开发工程师就需要考虑手机内部的实现细节,如硬件细节和软件细节。

    2. 封装的基本概念

    根据日常生活中的经验:并不是类的每个属性都是对外公开的。

    如:女孩子不希望外人知道自己的年龄和身高。

    如:一些男孩子不希望别人知道自己会尿床。

    而一些类的属性是对外公开的。

    如:人类的姓名,学历,国籍等。

    因此,需要在类的表示法中定义属性和行为的公开级别。

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

    3. C++中的类的封装

    成员变量:C++中用于表示类属性的变量。

    成员函数:C++中用于表示类行为的函数。

    在C++中可以给成员变量和成员函数定义访问级别。

    public:成员变量和成员函数可以在类的内部和外界访问和调用。

    private:成员变量和成员函数只能在类的内部被访问和调用。

    程序:类成员的访问属性

    #include <stdio.h>

    struct Biology

    {

        bool living;

    };

    struct Animal : Biology

    {

        bool movable;

        void findFood()

        {

        }

    };

    struct Plant : Biology

    {

        bool growable;

    };

    struct Beast : Animal

    {

        void sleep()

        {

        }

    };

    struct Human : Animal

    {

        void sleep()

        {

            printf("I'm sleeping... ");

        }  

        void work()

        {

            printf("I'm working... ");

        }

    };

    struct Girl : Human

    {

    private:

        int age;

    public:

        void play()

        {

            printf("I'm girl, I'm playing... ");

        }  

        void print()

        {  

            age = 22;

            printf("Girl's age is %d ", age);     

            play();

            sleep();

            work();

        }

    };

    struct Boy : Human

    {

    public:

        int age; 

        void play()

        {

            printf("I'm boy, I'm playing... ");

        }

        void print()

        {

            age = 23;   

            printf("Boy's age is %d ", age);   

            play();

            sleep();

            work();

        }

    };

    int main(int argc, char *argv[])

    {

        Girl girl;

        //girl.age = 25;

        girl.print();

        Boy boy; 

        boy.print();

        printf("Press any key to continue...");

        getchar();

        return 0;

    }

    4. 类成员的作用域

    类成员的作用域都只在类的内部,外部无法直接访问,成员函数可以直接访问成员变量和调用其它成员函数。

    类的外部可以通过类变量访问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 = %d ", i);

        printf("::i = %d ", ::i);

        //printf("test.i = %d ", test.i);

        printf("test.j = %d ", test.j);

        printf("test.getI() = %d ", test.getI());  

        printf("Press any key to continue...");

        getchar();  

        return 0;

    }

    2,1,4,3

    5. 类的真正形态

    struct在C语言中已经有了自己的含义,只能继续兼容。在C++中提供了新的关键字class用于类定义。class和struct的用法是完全相同的。

    在用struct定义类时,所有成员的默认属性为public。

    在用class定义类时,所有成员的默认属性为private。

    #include <stdio.h>

    struct A

    {

        int i;

       

        int getI()

        {

            return i;

        }

    };

    class B

    {

        int i;

       

        int getI()

        {

            return i;

        }

    };

    int main(int argc, char *argv[])

    {

        A a;

        B b;

        a.i = 1;

        a.getI();  

        b.i = 2;

        b.getI();   

        printf("Press any key to continue...");

        getchar();

        return 0;

    }

    运行的时候,struct A中的内容可以访问,class B中的内容无法访问。

    6. 手把手写代码

    要求:

    提供setOperator函数设置运算类型,如:加,减,乘,除。

    提供setParameter函数设置运算参数,类型为整型。

    提供result函数进行运算,其返回值表示运算的合法性,通过引用参数返回结果。

    7. 运算类的改进

    在Operator中所有成员函数的实现都在类中完成。

    问题:对于使main函数来说,只需要知道怎么使用Operator即可,没有必要知道其实现。

    解决方案:将Operator类的实现和定义分开。

    operator.h头文件中只有类的声明。

    operator.cpp中完成类的其它实现。

    operator.h

    #ifndef _OPERATOT_H_

    #define _OPERATOT_H_

    class Operator

    {

    private:

             char mop;

             double mP1;

             double mP2;   

            

    public:

             bool setOperator(char op);  //boolÀàÐÍ×öºÏ·¨ÅÐ¶Ï    

             void setParameter(double p1, double p2);

             bool result(double& r);

    };

    #endif

    operator.cpp

    #include "operator.h"

    bool Operator::setOperator(char op)  //boolÀàÐÍ×öºÏ·¨ÅжÏ

    {

             bool ret = false;                

             if((op == '+') || (op == '-') || (op == '*') || (op == '/'))

             {

                      ret = true;

                      mop = op;

             }               

             return ret;

    }

            

    void Operator::setParameter(double p1, double p2)

    {

                      mP1 = p1;

                      mP2 = p2;

    }

                     

    bool Operator::result(double& r)

    {

             bool ret = true;                 

             switch(mop)

             {

                      case '/':

                               if((-0.0000001 < mP2) && (mP2 < 0.0000001))

                               {

                                        ret = false;

                                       

                               }

                               else

                               {

                                        r = mP1 /mP2;

                               }

                               break;                        

                      case '+':

                               r = mP1 + mP2;

                               break;                        

                      case '*':

                               r = mP1 * mP2;

                               break;                                 

                      case '-':

                               r = mP1 - mP2;

                               break;               

                      default:

                               ret = false;

                               break;

             }

             return ret;       

    }

    main.cpp

    #include<stdio.h>

    #include"Operator.h"

    int main(int argc, char *argv[])

    {

             Operator op;

             double r = 0;

            

             op.setOperator('+');

             op.setParameter(8,2);

            

             if( op.result(r) )

             {

                      printf("Result is %f ",r);

             }

                     

             getchar();

             return 0;

    }

    小结:

    类的精华在于封装:将实现细节和使用方式相分离。

    C++中通过public和private实现类的封装:public成员可以通过变量被外界访问,private成员只能够在类内部使用。

    类的定义和实现可以分开到不同的文件中。

  • 相关阅读:
    Android实现简单的检测手机自由落体关闭屏幕
    Android Disable Package/Component 跳过app安装
    求单向链表中倒数第k个节点(c++):快慢指针/递归
    算法学习笔记---链表与数组
    pycharm使用matplotlib绘图学习笔记
    pycharm使用matplotlib绘制图像报错
    python numpy学习笔记
    python刷leetcode算法-- 左旋转字符串
    机器学习算法扫盲篇
    Kaggle入门篇
  • 原文地址:https://www.cnblogs.com/free-1122/p/11336183.html
Copyright © 2011-2022 走看看