zoukankan      html  css  js  c++  java
  • C++深度解析教程学习笔记(5)面向对象

    1. 面向对象基本概念

    (1)面向对象的意义在于

    ①将日常生活中习惯的思维方式引入程序设计中

    ②将需求中的概念直观的映射到解决方案中

    ③以模块为中心构建可复用的软件系统

    ④提高软件产品的可维护性和可扩展性

    (2)类和对象是面向对象中的两个基本概念

     

    对象

    概念

    指的是一类事物,是一个抽象的概念

    指的是属于某个类的具体实例

    关系

    类是一种模型,这种模型可以创建出不同的对象。一个类可以有很多对象。

    对象实体是类模型的一个具体实例。一个对象必然属于某个类

    意义

    类用于抽象的描述一类事物所特有的属性和行为。如电脑拥有CPU、内存和硬盘,并县城可以开机和运行程序

    对象是具体的事物,拥用所属类中描述的一切属性和行为。如每一只老虎都有不同的体重、不同的食量和不同的性情。

    2.类之间的基本关系

    (1)继承

     

    ①从已存在类细分出来的类和原类之间具有继承关系(is-a)

    ②继承的类(子类)拥有原类(父类)的所有属性和行为

    (2)组合

    ①一些类的存在必须依赖于其它的类,这种关系叫组合

    ②组合的类在某一个局部上由其它的类组成

    3.类的表示法

    #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(){};
        void work(){};
    };
    
    int main()
    {
        return 0;
    }

     4.类的封装

    4.1.封装的基本概念

    (1)类通常分为两个部分:类的实现细节、类的使用方式。当使用类时,不需要关心其实现细节。当创建类时,才需要考虑其内部实现细节

    我们在创建类的时候,要将复杂的细节封装在内部,不让使用者知道,给使用者一些非常简单的接口就可以。

    (2)根据经验,并不是类的每个属性都是对外公开的,但一些属性是对外公开的。

    (3)必须在类的表示法中定义属性和行为的公开级别

    4.2. C++中类的封装

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

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

    (3)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() {};
        void work() {}; 
    };
    
    
    struct Girl : Human
    {
    private:
        int age;
        int weight;
    
    public:
        void print()
        {
            age = 22;
            weight = 48;
    
            printf("I'm a girl, I'm %d years old.
    ", age);
            printf("My weigth is %d kg.
    ", weight);
        };
    };
    
    
    struct Boy : Human
    {
    private:
        int height;
        int salary; 
    
    public:
        int age;
        int weight;
    
        void print()
        {
            height = 175;
            salary = 9000;
    
            printf("I'm a boy, my height is %d cm.
    ", height);
            printf("My salary is %d RMB.
    ", salary);
        };
    };
    
    int main()
    {
    
        Girl g;
        Boy  b;
    
        g.print();
    
        b.age = 19;
        b.weight = 120;
        //b.height = 180;//Error: private
    
        b.print();
    
        return 0;
    }

    4.3. 类成员的作用域

    (1)类成员的作用域只在类的内部,外部无法直接访问

    (2)成员函数可以直接访问成员变量和调用成员函数

    (3)类的外部可以通过类变量访问 public 成员

    (4)类成员的作用域与访问级别没有关系。(注意 C++中用 struct 定义的类中所有成员默认为 public,而 class 定义的类成员的默认属性为 private)

    类成员的作用域

    #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("main: i = %d
    ", i); //i = 2;
        printf("::i = %d
    ",::i);  //::i = 1;
        //printf("test.i = %d
    ", test.i); //Error
        printf("test.j = %d
    ", test.j);   //test.j = 4
        printf("test.getI() = %d
    ", test.getI()); //test.getI() = 3
    
        return 0;
    }

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

    5.类的关键字

    (1)struct 在 C 语言中己经有了自己的含义,必须继续兼容

    (2)在 C++中提供了新的关键字 class 用于类定义

    (3)class 和 struct 的用法完全相同。但 C++中用 struct 定义的类中所有成员默认访问级别为 public,而 class 定义的类成员的默认访问级别为 private

    #include <stdio.h>
    
    struct A
    {
        //默认访问级别为public
        int i;
        int getI(){return i;};
        
    };
    
    class B
    {
        //默认访问级别为private
        int i;
        int getI(){return i;};    
    };
    int main()
    {
        A  a;
        B  b;
    
        a.i = 4;//ok
        printf("a.getI() = %d
    ", a.getI());
    
        //b.i = 4; //Error: private
        //printf("b.getI() = %d
    ", b.getI()); //Error: private
    
        return 0;
    }

    开发一个用于四则运算的类

    //operator.h
    #ifndef _OPERATOR_H_
    #define _OPERATOR_H_
    
    class Operator
    {
    private:
        char mOp; //操作符
        double mP1; //操作数1
        double mP2; //操作数2
    
    public:
        bool setOperator(char op);
        void setParameter(double p1, double p2);
        bool result(double& r);
    };
    
    #endif
    //operator.cpp
    #include "operator.h"
    
    bool Operator::setOperator(char op)
    {
        bool bRet = (op == '+') || (op == '-') || (op == '*') || (op == '/');
    
        if (bRet)
        {
            mOp = op;
        }
        else
        {
            mOp = '';
        }
    
        return bRet;
    }
    
    void Operator::setParameter(double p1, double p2)
    {
        mP1 = p1;
        mP2 = p2;
    }
    
    bool Operator::result(double& r)
    {
        bool bRet = true;
    
        switch (mOp)
        {
        case '/':
            if ((-0.000000001<mP2) && (mP2 <0.000000001))
            {
                bRet = false;
            }
            else
            {
                r = mP1 / mP2;
            }
            break;
    
        case '+':
            r = mP1 + mP2;
            break;
    
        case '-':
            r = mP1 - mP2;
            break;
    
        case '*':
            r = mP1 * mP2;
            break;
        }
    
        return bRet;
    }
    //main.cpp
    #include <stdio.h>
    #include "operator.h"
    
    int main()
    {
        Operator op;
        double r = 0;
    
        op.setOperator('/');
        op.setParameter(9, 3);
    
        if (op.result(r))
        {
            printf("r = %lf
    ", r);
        }
        else
        {
            printf("Calculate error!
    ");
        }
    
        return 0;
    }

    6.类声明和实现的分离

    (1).h 头文件只用类的声明:成员变量和成员函数

    (2).cpp 源文件中完成类的其它实现(如成员函数的实现)

  • 相关阅读:
    JAVA基础--JAVA API常见对象(包装类和正则)12
    编程中最没用的东西是源代码,最有用的东西是算法和数据结构(转载)
    angularjs calling order
    Service vs provider vs factory 转自:http://stackoverflow.com/questions/15666048/service-vs-provider-vs-factory
    构建自己的AngularJS,第一部分:作用域和digest 转摘:http://www.ituring.com.cn/article/39865
    angularjs 信息链接 转摘自:http://www.zhihu.com/question/27427447
    activity 生命周期 http://stackoverflow.com/questions/8515936/android-activity-life-cycle-what-are-all-these-methods-for
    google+ sign in and get the oauth token 转摘:https://gist.github.com/ianbarber/5170508
    谁动了我的内存:php内存泄露,系统缓存消耗? 转摘:http://blog.csdn.net/tao_627/article/details/9532497
    installing-sql-server-2012-error-prior-visual-studio-2010-instances-requiring 转摘
  • 原文地址:https://www.cnblogs.com/CoderTian/p/7756379.html
Copyright © 2011-2022 走看看