44-继承中的访问级别

注:博客中内容主要来自《狄泰软件学院》,博客仅当私人笔记使用。

测试环境:Ubuntu 10.10

GCC版本:9.2.0

 

一、值得思考的问题

        子类是否可以直接访问父类的私有成员

 

二、思考过程

子类不能直接访问父类的私有成员!

 

编程实验
继承中的访问级别
44-1.cpp
#include <iostream>
#include <string>

using namespace std;

class Parent
{
private:
    int mv;
public:
    Parent()
    {
        mv = 100;
    }
    
    int value()
    {
        return mv;
    }
};

class Child : public Parent
{
public:
    int addValue(int v)
    {
        mv = mv + v;    // ???? 如何访问父类的非公有成员
    }
};

int main()
{   
    return 0;
}

操作:

1) g++ 44-1.cpp -o 44-1.out编译错误:

44-1.cpp:9:6: error: 'int Parent::mv' is private
    int mv;
44-1.cpp:27:3: error: within this context
    mv = mv + v;
错误:'int Parent::mv'是私有的
错误:在这个上下文中有错
    mv = mv + v;

三、继承中的访问级别

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

2)可以定义protected访问级别

3)关键字protected的意义(重点)

    -    修饰的成员不能被外界直接访问(可以被自身的成员函数使用)

    -    修饰的成员可以被子类直接访问(被子类成员函数使用)

编程实验
protected初体验
44-2.cpp
#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;  //100
    
    Child c;
    
    cout << "c.mv = " << c.value() << endl;    //100
    
    c.addValue(50);
    
    cout << "c.mv = " << c.value() << endl;    //150
    
    return 0;
}

操作:

1) g++ 44-2.cpp -o 44-2.out编译正确,打印结果:

p.mv = 100
c.mv = 100
c.mv = 150

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;  //100
    cout << "p.mv = " << p.mv << endl;   //error: ‘int Parent::mv’ is protected
    p.mv = 1000;    // error。父类自身可以访问数据(成员函数),但是不能修改
    
    Child c;
    
    cout << "c.mv = " << c.value() << endl;    //100
    
    c.addValue(50);
    
    cout << "c.mv = " << c.value() << endl;    //150
    
    c.mv = 10000;  // error
    
    return 0;
}

g++ 44-2.cpp -o 44-2.out编译错误:

44-2.cpp:9:6: error: 'int Parent::mv' is protected
    int mv;
错误:'int Parent::mv'是被保护的
44-2.cpp:37:25: error: within this context
    cout << "p.mv = " << p.mv << endl;
错误:在这个上下文内有错(不能通过对象直接外部访问保护成员mv)
44-2.cpp:9:6: error: 'int Parent::mv' is protected
    int mv;
错误:'int Parent::mv'是被保护的
44-2.cpp:39:4: error: within this context
    p.mv = 1000;
错误:在这个上下文有错(不能给保护成员mv的赋值)
44-2.cpp:9:6: error: 'int Parent::mv' is protected
    int mv;

44-2.cpp:49:4: error: within this context
    c.mv = 10000;
错误:不能给c.mv赋值,因为是被保护的成员

分析:

        protected修饰的成员不能被外部直接访问,但是可以在自身的成员函数访问或者子类成员函数中访问。

 

四、思考

         为什么面向对象中需要protected?        

           专门为继承准备!

 

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

 

六、组合与继承的综合实例

Point继承自Object(父类)

Line继承自Object(父类)

Point和Line是组合关系(两点构成一条直线)

Point和Line都是派生类,继承了Object

编程实验
综合实例
44-3.cpp
#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;    
        
        mX = x;    
        mY = y;
        mName = "Point";    //访问mName变量
        //字符串输入到s对象中
        s << "P(" << mX << ", " << mY << ")";    
        
        mInfo = s.str();    //给mInfo赋值
    }
    int x()
    {
        return mX;
    }
    int y()
    {
        return mY;
    }
};

class Line : public Object
{
private:
    Point mP1;
    Point mP2;
public:
    Line(Point p1, Point p2)
    {
        ostringstream s;
        
        mP1 = p1;
        mP2 = p2;
        mName = "Line";    //任意访问mName变量
        //字符串输入到s对象中
        s << "Line from " << mP1.info() << " to " << mP2.info();
        
        mInfo = s.str();    //给mInfo赋值
    }
    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;
    cout << o.info() << endl;
    
    cout << endl;
    
    cout << p.name() << endl;
    cout << p.info() << endl;
    
    cout << endl;
    
    cout << l.name() << endl;
    cout << l.info() << endl;
    
    return 0;
}

操作:

1) g++ 44-3.cpp -o 44-3.out编译正确,打印结果:

Object
(空) 
 
Point
p(1,2)
 
Line
Line from p(1,2) to p(5,6)

 

小结

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

2)protected修饰的成员不能被外界所访问

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

4)protected关键字是为了继承而专门设计的

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

 

public继承时,访问权限:

子类不能访问父类的private成员

子类可以访问父类的protected成员

子类可以访问父类的public成员

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值