44-继承中的访问级别

44-继承中的访问级别

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

【范例代码】继承中的访问级别

#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(int argc, const char* argv[]) {
    return 0;
}

继承中的访问级别:

  • public:不改变父类成员的性质,即父类public的成员在子类中仍然是public,protected的成员在子类中仍然是protected
  • private:父类所有的成员在子类中变成private成员,基类中的private成员变量,在派生类中不可访问,但是基类的protected的成员变量可以访问
  • protected:父类public类型的成员在子类中变为protected类型,protected的成员被继承仍然是protected,protected成员可以在子类中访问,但不能在外界访问

【范例代码】protected初体验

#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(int argc, const char* argv[]) {
    Parent p;

    cout << "p.mv = " << p.value() << endl;

    // p.mv = 1000;    // error

    Child c;

    cout << "c.mv = " << c.value() << endl;

    c.addValue(50);

    cout << "c.mv = " << c.value() << endl;

    // c.mv = 10000;  // error

    return 0;
}
【问题】为什么面向对象中需要protected?

【范例代码】综合实例

#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";

        s << "P(" << mX << ", " << mY << ")";

        mInfo = s.str();
    }
    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";

        s << "Line from " << mP1.info() << " to " << mP2.info();

        mInfo = s.str();
    }
    Point begin() {
        return mP1;
    }
    Point end() {
        return mP2;
    }
};

int main(int argc, const char* argv[]) {
    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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值