温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C++ 继承(一)

发布时间:2020-07-12 03:20:59 来源:网络 阅读:385 作者:Aonaufly 栏目:编程语言

C++继承的标准写法:

class BaseMonster
{
public:
    string name;
    string skill;
    uint32_t hp;
    void attack()
    {
        cout << "Base :: 发动***" << endl;
    }
};
class RedDemonKing : public BaseMonster
{
} ;

解析:

① 类 RedDemonKing 集成了 类BaseMonster

② 集成的限定词为 public->

    C++ 继承(一)

③ 对限定词的解析->

采用降级处理 : 级别从高到低(降序) public , protected , private 。如采用public,则基类中比public高的成员,全部降一级(因为没有比public高的级别 , 所以都不降级。但是protected的话 , 基类中的public在子类中的访问级别就降了一级变成protected)。

④ 如果省略继承的限定词,则默认为private限定词。


C++ 继承(一)


具体:

一,继承构造函数的调用顺序->先调用父类的构造函数,再调用子类(自己)的构造函数

1,当基类的构造函数为无参构造:

#include <iostream>
using namespace std;
class BaseMonster
{
public:
    string name;
    string skill;
    uint32_t hp;
    void attack()
    {
        cout << "Base :: 发动***" << endl;
    }
};
class RedDemonKing : public BaseMonster
{
} ;
int main()
{
    RedDemonKing rdk;
    rdk.name = "赤妖王";
    cout << rdk.name << endl;
    return 0;
}

2,当基类有有参数的构造函数时:

#include <iostream>
using namespace std;
class BaseMonster
{
public:
    string name;
    string skill;
    BaseMonster( string name )
    {
        this->name = name;
    }
    void attack()
    {
        cout << "Base :: 发动***" << endl;
    }
};
class RedDemonKing : public BaseMonster
{
public:
    RedDemonKing( string name ) : BaseMonster(name)
    {
        this->skill = "会心一击";
    }
} ;
int main()
{
    RedDemonKing rdk("赤妖王");
    cout << rdk.name << endl;
    return 0;
}

解析:

① RedDemonKing( string name ) : BaseMonster(name)  , 以此格式(参数列表)为调用基类构造。


二,覆盖

#include <iostream>
using namespace std;
class BaseMonster
{
public:
    string name;
    string skill;
    BaseMonster( string name )
    {
        this->name = name;
    }
    void attack()
    {
        cout << "Base :: 发动***" << endl;
    }
};
class RedDemonKing : public BaseMonster
{
public:
    string name;
    RedDemonKing( string name ) : BaseMonster(name)
    {
        this->name = "赤妖王";
        this->skill = "会心一击";
    }
} ;
int main()
{
    RedDemonKing rdk("妖怪");
    cout << rdk.name << endl;
    cout << rdk.BaseMonster::name << endl;
    return 0;
}

解析:

① 当子类和基类有相同名字的成员如上(name),那么子类的name就对积累的name形成了覆盖(相当与作用域)

② 默认取值 rdk.name , 取得是子类(自己)的值 。 如果要取基类的值需指定域 : rdk.BaseMonster::name

③ 重点 , 只要是名字一样都会形成覆盖,不存在重载:

    C++ 继承(一)

要调用基类的函数 必须指定域


firend(友元)不能被继承( 可以这么理解 : 父亲的朋友 , 不一定是自己的朋友 )

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI