zoukankan      html  css  js  c++  java
  • 第四十四课、继承中的访问级别

    一、一个令人疑惑的问题

    二、面向对象中的访问级别

    1、面向对象中的访问级别不只是publicprivate

    2、可以定义protected的访问级别

    3、关键字protected的意义

    (1)、修饰的成员不能被外界直接访问

    (2)、修饰的成员可以被子类直接访问

    #include <iostream>
    #include <string>
    
    using namespace std;
    
    class Parent
    {
    protected:
        int mv;
    public:
        Parent()
        {
            mv = 100;
        }
        
        int value()
        {
            return mv;
        }
    };
    
    class Child : public Parent
    {
    public:
        int addValue(int v)
        {
            mv = mv + v;    //可以访问父类的私有成员
        }
    };
    
    int main()
    {   
        Parent p;
        
        cout << "p.mv = " << p.value() << endl;
        
        // p.mv = 1000;    // error, protected
        
        Child c;
        
        cout << "c.mv = " << c.value() << endl;//继承了父类的成员函数
        
        c.addValue(50);
        
        cout << "c.mv = " << c.value() << endl;
        
        // c.mv = 10000;  // error, protected
        
        return 0;
    }

    三、定义类时访问级别的选择

     

    四、综合实例

    Point和Line继承自Object, Line由Point组合而成

    #include<iostream>
    #include<string>
    #include<sstream>
    
    using namespace std;
    
    class Object
    {
    protected:
        string mName;
        string mInfo;
    public:
        Object()
        {
            mName = "Object";
            mInfo = "";
        }
    
        string name()
        {
            return mName;
        }
    
        string info()
        {
            return mInfo;
        }
    
    };
    
    class Point : public Object
    {
    private:
        int mX;
        int mY;
    public:
        Point(int x=0, int y=0)
        {
            ostringstream s;
            mName = "Point";
            mX = x;
            mY = y;
            s << "p(" << mX << "," << mY << ")";
            mInfo = s.str();
        }
    };
    
    class Line : public Object
    {
    private:
        Point mP1;//Line 由 Point 组合而成
        Point mP2;
    public:
        Line(Point P1, Point P2)
        {
            ostringstream s;
            
            mP1 = P1;
            mP2 = P2;
            mName = "Line";
    
            s << "Line from" << mP1.info() << "to" << mP2.info();
            mInfo = s.str();        
        }
    
        Point begin()
        {
            return mP1;
        }
    
        Point end()
        {
            return mP2;
        }
    };
    
    int main()
    {
        Object o;
        Point p(1, 2);
        Point pn(5, 6);
        Line l(p, pn);
    
        cout << o.name() << endl;//Object
        cout << o.info() << endl;
    
        cout << endl;
    
        cout << p.name() << endl;//Point
        cout << p.info() << endl;//p(1, 2)
    
        cout << endl;
    
        cout << l.name() << endl;//Line
        cout << l.info() << endl;//Line fromP(1, 2)toP(5, 6)
    
        return 0;
    }

    五、小结

    1、面向对象的访问级别不只publicprivate

    2、protected修饰的成员不能被外界直接访问

    3、protected使得子类能够访问父类的成员

    3、protected关键字为了继承而专门设计的

    4、没有protected就无法完成真正意义上的代码复用

  • 相关阅读:
    Android的Activity屏幕切换动画(一)-左右滑动切换
    404 Not Found 由来
    HTML+CSS 制作HTML5标志图
    发现 网站错误
    链接指南
    偷懒省事有工具啊
    程序员很穷(转)
    谷歌浏览器修改CSS和js后同步保存到文件中 (译)
    程序员眼睛的保护(爱护眼睛,你我做起)
    仿站违法和侵权吗?
  • 原文地址:https://www.cnblogs.com/gui-lin/p/6367223.html
Copyright © 2011-2022 走看看