zoukankan      html  css  js  c++  java
  • 第五章 数据的共享和保护

    1标识符的作用域与可见性
    ▫ 函数原型作用域
    比如 int run(int a) ; 的范围是()括号里面
     
    ▫ 局部作用域(块作用域)
    int run(int a){
      int b;
      yuju;
    }  //a的作用于是  { }里面,b的作用域是  定义位置 到 }
     
    ▫ 类作用域
    • 类的成员具有类作用域,其范围包括类体和非内联成员函数的函数体。
    • 如果在类作用域以外访问类的成员,要通过类名(访问静态成员),或者该类的对象名、对象引用、对象指针(访问非静态成员)。
    (看不懂现在)
     
    ▫ 文件作用域
    全局变量,从声明到文件尾。
     
    ▫ 命名空间作用域  没学到
     
    说明:
    • 标识符应声明在先,引用在后。
    • 如果某个标识符在外层中声明,且在内层中没有同一标识符的声明,则该标识符在内层可见。
    • 对于两个嵌套的作用域,如果在内层作用域内声明了与外层作用域中同名的标识符,则外层作用域的标识符在内层不可见。
     
     
    2 对象的生存期
    对象从产生到结束的这段时间就是它的生存期。在对象生存期内,对象将保持它的值,直到被更新为止。
    静态生存期
    • 这种生存期与程序的运行期相同。
    • 在文件作用域中声明的对象具有这种生存期。(全局变量)
    • 在函数内部声明静态生存期对象,要冠以关键字static 。
     
    动态生存期
    • 块作用域中声明的,没有用static修饰的对象是动态生存期的对象(习惯称局部生存期对象)。
    • 开始于程序执行到声明点时,结束于命名该标识符的作用域结束处。
     
    3 类的静态成员 
    • 静态数据成员
    ▫ 用关键字static声明
    ▫ 为该类的所有对象共享,静态数据成员具有静态生存期。
    ▫ 必须在类外定义和初始化,用(::)来指明所属的类。
     
    • 静态函数成员
    ▫ 类外代码可以使用类名和作用域操作符来调用静态成员函数。
    ▫ 静态成员函数只能引用属于该类的静态数据成员或静态成员函数。
     
     
    4 类的友元
    • 友元是C++提供的一种破坏数据封装和数据隐藏的机制。
    • 通过将一个模块声明为另一个模块的友元,一个模块能够引用到另一个模块中本是被隐藏的信息。
    • 可以使用友元函数和友元类。
    • 为了确保数据的完整性,及数据封装与隐藏的原则,建议尽量不使用或少使用友元。
     
    • 友元函数是在类声明中由关键字friend修饰说明的非成员函数,在它的函数体中能够通过对象名访问 private 和 protected成员
    • 作用:增加灵活性,使程序员可以在封装和快速性方面做合理选择。
    • 访问对象中的成员必须通过对象名。
     
    • 若一个类B为另一个类A的友元,则类B的所有成员都能访问对方类的私有成员。
    • 声明语法:将友元类名在另一个类中使用friend修饰说明。
     
    如果声明B类是A类的友元,B类的成员函数就可以访问A类的私有和保护数据,但A类的成员函数却不能访问B类的私有、保护数据。
     
    5 共享数据的保护
    • 对于既需要共享、又需要防止改变的数据应该声明为常类型(用const进行修饰)。对于不改变对象状态的成员函数应该声明为常函数。
    ▫ 常对象:必须进行初始化,不能被更新。const 类名 对象名
    ▫ 常成员用const进行修饰的类成员:常数据成员和常函数成员
    ▫ 常引用:被引用的对象不能被更新。const 类型说明符 &引用名
    ▫ 常数组:数组元素不能被更新(详见第6章)。类型说明符const 数组名[大小]...
    ▫ 常指针:指向常量的指针(详见第6章)
     
    常成员函数
    ▫ 使用const关键字说明的函数。
    ▫ 常成员函数不更新对象的数据成员。
    ▫ 常成员函数说明格式:类型说明符函数名(参数表)const;这里,const是函数类型的一个组成部分,因此在实现部分也要带const关键字。
    ▫ const关键字可以被用于参与对重载函数的区分
    • 通过常对象只能调用它的常成员函数。
     
    • 常数据成员
    ▫ 使用const说明的数据成员。
     
    • 如果在声明引用时用const修饰,被声明的引用就是常引用。
    • 常引用所引用的对象不能被更新。
    • 如果用常引用做形参,便不会意外地发生对实参的更改。常引用的声明形式如下:
                const 类型说明符 &引用名;
     
    粘一下代码
    //5_4.cpp
    #include <iostream>
    using namespace std;
    class Point { //Point类定义
        public: //外部接口
        Point(int x = 0, int y = 0) : x(x), y(y) { //构造函数
            //在构造函数中对count累加,所有对象共同维护同一个count
            count++;
        }
        Point(Point &p) {//复制构造函数
            x = p.x;
            y = p.y;
            count++;
        }
        ~Point() { count--; }
        int getX() { return x; }
        int getY() { return y; }
        void showCount() { //输出静态数据成员
            cout << " Object count = " << count << endl; 
        }
        private: //私有数据成员
        int x, y;
        static int count;//静态数据成员声明,用于记录点的个数
    };
    int Point::count = 0;//静态数据成员定义和初始化,使用类名限定
    int main() { //主函数
        Point a(4, 5); //定义对象a,其构造函数回使count增1
        cout << "Point A: " << a.getX() << ", " << a.getY();
        a.showCount(); //输出对象个数
        Point b(a); //定义对象b,其构造函数回使count增1
        cout << "Point B: " << b.getX() << ", " << b.getY();
        b.showCount(); //输出对象个数
        return 0;
    }
    //5_5.cpp静态函数成员
     
    #include <iostream>
    using namespace std;
    class Point { //Point类定义
        public: //外部接口
        Point(int x = 0, int y = 0) : x(x), y(y) { //构造函数
            //在构造函数中对count累加,所有对象共同维护同一个count
            count++;
        }
        Point(Point &p) { //复制构造函数
            x = p.x;
            y = p.y;
            count++;
        }
        ~Point() { count--; }
        int getX() { return x; }
        int getY() { return y; }
        static void showCount() { //静态函数成员
        cout << " Object count = " << count << endl; } 
        private: //私有数据成员
        int x, y;
        static int count;//静态数据成员声明,用于记录点的个数
    };
    int Point::count = 0;//静态数据成员定义和初始化,使用类名限定
    int main() { //主函数
        Point a(4, 5); //定义对象a,其构造函数回使count增1
        cout << "Point A: " << a.getX() << ", " << a.getY();
        Point::showCount(); //输出对象个数
        Point b(a); //定义对象b,其构造函数回使count增1
        cout << "Point B: " << b.getX() << ", " << b.getY();
        Point::showCount(); //输出对象个数
        return 0;
    }
    //友元类 
    #include<bits/stdc++.h>
    #define endl '
    '
    #define _for(i,a,b) for(int i=a;i<b;i++)
    using namespace std;
    const int N = 1e5+5;
    typedef long long ll;
    class A {
        friend class B;
        public:
        void display() {
            cout << x << endl; 
        }
        private:
        int x;
    };
    class B {
        public:
        void set(int i);
        void display();
        private:
        A a;
    }; 
    void B::set(int i) {
        a.x=i;
    }
    void B::display() {
        a.display();
    }
    int main(){
        A a; 
        a.display();
        B b; 
        b.set(4);
        b.display();
        return 0;
    }
    //友元函数 
    #include <bits/stdc++.h> 
    using namespace std;
    class Point { //Point类声明
        public: //外部接口
        Point(int x=0, int y=0) : x(x), y(y) { }
        int getX() { return x; }
        int getY() { return y; }
        friend float dist(Point &a, Point &b); 
        private: //私有数据成员
        int x, y;
    }; 
    float dist( Point& a, Point& b) {
        double x = a.x - b.x;
        double y = a.y - b.y;
        return static_cast<float>(sqrt(x * x + y * y));
    }
    int main() {
        Point p1(1, 1), p2(4, 5);
        cout <<"The distance is: ";
        cout << dist(p1, p2) << endl;
        return 0;
    }
    //例5-7 常成员函数举例
    #include<iostream>
    using namespace std;
    class R {
        public:
        R(int r1, int r2) : r1(r1), r2(r2) { }
        void print();
        void print() const;
        private:
        int r1, r2;
    }; 
    void R::print() {
        cout << r1 << ":" << r2 << endl; 
    }
    void R::print() const {
        cout << r1 << ";" << r2 << endl; 
    }
    int main() {
        R a(5,4);
        a.print(); //调用void print()
        const R b(20,52); 
        b.print(); //调用void print() const
        return 0;
    }
    //例5-8 常数据成员举例
    #include <iostream>
    using namespace std;
    class A {
        public:
            A(int i);
            void print();
        private:
            const int a;
            static const int b; //静态常数据成员
    }; 
    const int A::b=10; 
    A::A(int i) : a(i) { }
    void A::print() {
        cout << a << ":" << b <<endl; 
    }
    int main() {
        /*建立对象a1和a2,并以100和0作为初值,分别调用构造
        函数,通过构造函数的初始化列表给对象的常数据成员赋
        初值*/
        A a1(100), a2(0);
        a1.print();
        a2.print();
        return 0;
    }

    //常引用做形参
    #include <iostream> #include <cmath> using namespace std; class Point { //Point类定义 public: //外部接口 Point(int x = 0, int y = 0) : x(x), y(y) { } int getX() { return x; } int getY() { return y; } friend float dist(const Point &p1, const Point &p2); private: //私有数据成员 int x, y; }; float dist(const Point &p1, const Point &p2) { double x = p1.x - p2.x; double y = p1.y - p2.y; return static_cast<float>(sqrt(x * x + y * y)); } int main() { //主函数 const Point myp1(1, 1), myp2(4, 5); cout << "The distance is: "; cout << dist(myp1, myp2) << endl; return 0; }
     
    //变量,生存期
    #include<iostream> using namespace std; int i = 1; // i 为全局变量,具有静态生存期。 void other() { static int a = 2; static int b; // a,b为静态局部变量,具有全局寿命,局部可见。 //只第一次进入函数时被初始化。 int c = 10; // C为局部变量,具有动态生存期, //每次进入函数时都初始化。 a += 2; i += 32; c += 5; cout<<"---OTHER--- "; cout<<" i: "<<i<<" a: "<<a<<" b: "<<b<<" c: "<<c<<endl; b = a; } int main() { static int a;//静态局部变量,有全局寿命,局部可见。 int b = -10; // b, c为局部变量,具有动态生存期。 int c = 0; cout << "---MAIN--- "; cout<<" i: "<<i<<" a: "<<a<<" b: "<<b<<" c: "<<c<<endl; c += 8; other(); cout<<"---MAIN--- "; cout<<" i: "<<i<<" a: "<<a<<" b: "<<b<<" c: "<<c<<endl; i += 10; other(); return 0; }
  • 相关阅读:
    关于代码的一系列调整
    题目清单(更新至2016年12月17日 10:52)
    USACO翻译:USACO 2013 NOV Silver三题
    USACO翻译:USACO 2013 DEC Silver三题
    USACO翻译:USACO 2014 DEC Silver三题
    USACO翻译:USACO 2012 FEB Silver三题
    USACO翻译:USACO 2012 JAN三题(3)
    USACO翻译:USACO 2012 JAN三题(2)
    USACO翻译:USACO 2012 JAN三题(1)
    USACO翻译:USACO 2014 FEB SILVER 三题
  • 原文地址:https://www.cnblogs.com/SunChuangYu/p/12577453.html
Copyright © 2011-2022 走看看