C++(笔记)三种继承机制

来源:互联网 发布:ubuntu怎么查看分区 编辑:程序博客网 时间:2024/06/14 11:02

在实际编程中其实公有继承占绝大多数,但其他也是都需要了解的


首先要知道子类继承了基类的所以数据成员和全部函数成员(除了构造、析构器),但它们的继承方式不同,访问权限也会不一样


明确访问有两种:
1.子类中的新增成员访问从基类继承的成员
2.子类外部(非类族内的成员)通过子类的对象访问从基类继承的成员


注意:不管哪种继承方式,子类都不能直接访问基类的私有成员


一、public继承(公有继承)

基类的public和protected成员的访问属性在子类中不变,而基类的私有成员不可直接访问(基类的public和protected成员做子类的public和protected成员),—基类public成员,子类可以通过子类内部访问该成员,也可以通过子类对象(类外)访问该成员。—基类protected成员,可以通过子类内部访问该成员,但不能通过子类对象(外部)访问该成员。

#include <iostream>using namespace std;class Point{    public:        void initPoint(float _x=0,float _y=0)        {            x=_x;            y=_y;        }        void move(float offx,float offy)        {            x+=offx;            y+=offy;        }        float getX() const        {            return x;        }        float gety() const        {            return y;        }    private:        float x,y;};class Rectangle:public Point{    public:        void initRectangle(float _x,float _y,float _w,float _h)        {            initPoint(_x,_y);//类内调用基类成员函数             w=_w;            h=_h;        }        float getH() const        {            return h;        }        float getW() const        {            return w;        }    private:        float w,h;};int main(){    Rectangle rect;    rect.initRectangle(2,3,20,10);    rect.move(3,2);//类外通过对象调用基类成员函数     cout<<"The data of rect(x,y,w,h):"<<endl;    cout<<rect.getX()<<","<<rect.gety()<<","<<rect.getW()<<","<<rect.getH()<<endl;    return 0;}

这里写图片描述


二、private继承(私有继承)

基类中的public和protected成员都以私有成员身份在子类中而基类私有私有成员不用说,在子类中肯定是不可直接访问的,也就是说基类的public和protected成员被继承后作为子类的私有成员,子类其他成员可以访问,但子类对象是无法直接访问的

若还是以上的代码会出现错误
这里写图片描述
这里写图片描述

move成员函数不可被访问
getX,getY成员函数不可被访问

只能通过类内访问函数,不可通过子类对象访问

#include <iostream>using namespace std;class Point{    public:        void initPoint(float _x=0,float _y=0)        {            x=_x;            y=_y;        }        void move(float offx,float offy)        {            x+=offx;            y+=offy;        }        float getX() const        {            return x;        }        float getY() const        {            return y;        }    private:        float x,y;};class Rectangle:private Point{    public:        void initRectangle(float _x,float _y,float _w,float _h)        {            initPoint(_x,_y);//类内调用基类成员函数             w=_w;            h=_h;            cout<<getX()<<endl;//因为是private继承,所以只能在类内来访问基类函数             cout<<getY()<<endl;            cout<<getW()<<endl;            cout<<getH()<<endl;        }        float getH() const        {            return h;        }        float getW() const        {            return w;        }    private:        float w,h;};int main(){    Rectangle rect;    //rect.move(3,2);//错误,move函数是基类成员,通过private继承,成为了子类的private成员,子类对象无权限     cout<<"The data of rect(x,y,w,h):"<<endl;    rect.initRectangle(2,3,20,10);    //cout<<rect.getX()<<","<<rect.gety()<<","<<rect.getW()<<","<<rect.getH()<<endl;//同理,错误     return 0;}

三、protected继承(保护继承)

基类的public和protected成员以保护成员(protected)出现在子类中,而基类私有成员还是不可直接访问,所以子类的其他成员可以直接访问基类继承来的public和protected成员,但是子类对象无法直接访问(到目前为止和private继承相同)


四、private继承机制与protected继承机制区别

看了上面可能会觉得两者完全相同,但是这只是通过一次继承,通过一次继承,protected和private继承机制是完全相同的,但经过private继承后,基类成员都成为了子类的私有成员或者不可访问成员,若进一步派生,那就是子类成为了基类,因为前面基类的成员在子类都是私有属性了,所以进一步派生之后私有成员都是无法直接访问的———但是protected继承机制在这一点若进一步派生,是protected成员被继承,那么protected成员还是可以通过子类内部来访问的(不能通过子类对象访问)——-这就是两种继承机制的区别

4 0
原创粉丝点击