c++第四次做业

继承与派生

知识要点

1.关于继承、派生

类的继承, 是新的类从已有类那里获得已有的特性。简单来讲,就是在原有类的基础上作一些扩充。
定义方式:
ios

class 派生类名:继承方式 基类名1,继承方式 基类名...
{
   派生类成员声明; /*对除了从基类继承来的成员以外,新增长的数据和函数成员的声明*/
};

例子函数

class person//定义一个person类
{
    string name[6];
    int age;
    char sex;
};
class student:public person/*定义student类,公有继承person类,那么student里面也有name、age、sex*/
{
    int id;
    int score;
};

[attention]在派生过程当中构造函数和析构函数都不被继承
没有指明继承方式时默认private
this

2.三种继承方式

公有继承(public):基类的公有成员和保护成员在派生类中的访问属性不变,而基类的私有成员不可直接访问
私有继承(private):基类的公有成员和保护成员以私有成员身份出如今派生类中,而基类的私有成员在派生类中不可直接访问
保护继承(protected):基类的公有成员和保护成员以保护成员身份出如今派生类中,而基类的私有成员在派生类中不可直接访问

---
呈现以下spa

继承方式 子类访问权限 类外对象访问权限
public 能够访问public、protected 能够访问public
private 能够访问public、protected 没法访问
protected 能够访问public、protected 没法访问

具体例子code

#include"pch.h"
#include<iostream>
using namespace std;
class Point
{
public:
    int getX()
    {
        return x;
    }
    int getY()
    {
        return y;
    }
protected:
    void initial(int xx, int yy);
private:
    int x, y;
};
void Point::initial(int xx, int yy)
{
    x = xx;
    y = yy;
}
class Rectangle:public Point/*此处为公有继承,稍后会对继承方式进行改动*/
{
public:
    int getWidth()
    {
        return width;
    }
    int getLength()
    {
        return length;
    }
    void initial_1(int length, int width, int x, int y)
    {
        initial(x, y);//此处代表公有继承在派生类中能够访问基类的protected
        this->length = length;
        this->width = width;
    }
private:
    int length, width;
};
int main()
{
    Rectangle rec;
    rec.initial_1(3, 2, 9, 5);//访问基类的保护成员
    cout << rec.getLength()<<endl;
    cout << rec.getX();//访问基类的公有成员
    return 0;
}

对于公有继承在类外的访问
对象

对于私有和保护继承在类外的访问
blog

3.派生类的构造函数与析构函数

构造派生类的对象时,须要对基类的成员对象和新增成员对象进行初始化。但在继承的过程当中,基类的构造函数不被继承,因此派生类的构造函数中的一些参数就须要传给基类的构造函数。
通常语法形式
继承

派生类名::派生类名(参数表):基类名1(基类1初始化参数表),...,基类名n(初始化n参数表)
{
    派生类构造函数的其余初始化操做;
}

派生类构造函数执行的通常次序:

(1)调用基类构造函数,调用顺序按照他们被继承时声明的顺序
(2)对派生类新增的成员对象初始化时,调用顺序按照他们在类中声明的顺序
(3)执行派生类构造函数体中的内容
get

#include"pch.h"
#include<iostream>
using namespace std;
class A
{
public:A(int i)//有参数的构造函数
{
    cout << "constructing A " << i << endl;
}
};
class B
{
public:B(int j)//有参数的构造函数
{
    cout << "constructing B " << j << endl;
}
};
class C
{
public:C()//无参数的构造函数
{
    cout << "constructing C* " << endl;
}
};
class D :public A, public B, public C//公有继承
{
public:D(int a,int b,int c,int d):A(a),m1(d),m2(c),B(b)
{}
private:A m1;
        B m2 ;
        C m3;
};
int main()
{
    D d(1, 2, 3, 4);
    return 0;
}

构造代码结果
string

4.虚基类

声明语法形式

class 派生类名:virtual 继承方式 基类名

以实例来讲明

#include"pch.h"
#include<iostream>
using namespace std;
class A
{
public:
    int a;
    void funa()
    {
        cout << "member of A" << endl;
    }
};
class B:virtual public A//以A为虚基类
{
public:
    int b;
};
class C :virtual public A//以A为虚基类
{
public:
    int c;
};
class D :public B, public C//D是B和C,以A为虚基类派生的
{
public:
    int d;
    void fun()
    {
        cout << "member of D" << endl;
    }
};
int main()
{
    D d;
    //直接访问虚基类的数据成员和函数成员
    d.a = 2;
    d.funa();
    return 0;

}

虚基类代码结果

相关文章
相关标签/搜索