对C++继承的研究

继承
继承不仅是面向对象的基本特性之一,更是面向对象技术能够提高软件开发效率的重要原因之一。
通俗点说:继承就是从先辈处得到属性和行为的特征
类的继承新的类从已有类那里得到已有的特性
通过继承也了解到派生。
类的派生从已有的类产生新类的过程。
继承方式:
1、公有继承public
2、私有继承private
3、保护继承protected
一、公有继承
特性:基类的公有成员和保护成员的访问属性在派生类中不变,而基类的私有成员不可直接访问
...

#include<iostream>
#include<cmath>
using namespace std;
class Point {
public:
void initPoint(float x = 0, float y = 0)
{
    this->x = x;
    this->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);
        this->w = w;
        this->h = w;
    }
    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;
}

...
最后的输出结果:
图二
二、私有继承
特性:基类中的公有成员和保护成员都以私有成员身份出现在派生类中,而基类的私有成员在派生类中不可直接直接访问。
...

#include<iostream>
#include<cmath>
using namespace std;
class Point {
public:
    void initPoint(float x=0, float y=0)
    {
        this->x = x;
        this->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);
        this->w = w;
        this->h = h;
    }
    void move(float offX, float offY)
    {
        Point::move(offX, offY);
    }
    float getX()const {
        return Point::getX();
    }
    float getY()const
    {
        return Point::getY();
    }
    float getH()const
    {
        return h;
    }
    float getW()const
    {
        return w;
    }
private:
    float w, h;
};
int main()
{
    Rectangle rect;
    rect.initRectangle(2, 3, 20, 10);
    cout << rect.getX() << ","
        << rect.getY() << ","
        << rect.getW() << ","
        << rect.getH() << endl;
    return 0;
}

...
最后的输出结果:
图二
三、保护继承
特性:基类的公有成员和保护成员都以保护成员的身份出先在派生类中,而基类的私有成员不可直接访问。
...

#include <iostream>
using namespace std;

class A
{
private:
    int a;
protected:
    int b;
public:
    int c;
    void show()
    {
        cout << "a = " << a << " b = " << b << " c = " << c << endl;
    }

    void set_a(int a)
    {
        this->a = a;
    }
};

class B :protected A
{
public:
    void set_a(int a)
    {
        A::set_a(a);
    }   

    void set_b(int b)
    {
        this->b = b;
    }
};

class C :protected B
{
public:
    void set(int a, int b, int c)
    {
        set_a(a);
        set_b(b);
        this->c = c;
    }

    void show()
    {
        A::show();
    }
};

int main()
{
    C c;
    c.set(10, 20, 30);
    c.show();
    return 0;
}

...
最后输出的结果:
图二

猜你喜欢

转载自www.cnblogs.com/xlog/p/11666840.html