嵌入式养成计划-39----C++静态成员--继承--多继承

九十一、静态成员

  • 静态成员变量是属于类的变量,而不是属于类的对象的。
  • 它们在类的所有实例中是共享的。
  • 它们具有类范围的生命周期,因此与全局变量有一些相似之处。

在数据成员前+static ----->静态数据成员
在成员函数前+static ------>静态成员函数

静态数据成员 必须在类外初始化,如果不初始化(不建议),默认为0。
静态成员函数只能访问静态数据成员,不能访问非静态数据成员。

91.1 静态成员 的 生命周期

  • 静态成员变量的 初始化: 静态成员变量在程序启动时由编译器自动初始化为默认值,或者可以在类外部显式初始化。

  • 静态成员变量的 使用: 静态成员变量可以通过类名或类的对象来访问,它们在程序的整个生命周期内都是可用的。

  • 静态成员变量的 销毁: 静态成员变量的销毁发生在程序结束时,它们的析构函数不会被调用,因为它们没有与特定对象相关联
    它们的内存会在程序结束时被释放。

91.2 静态成员 格式:

class 类名
{
	static 数据类型 变量名; // 静态数据成员必须在类外初始化
	static 函数返回值类型 函数名(形参列表) //静态成员函数
	{函数体}
};
数据类型 类名::变量名 = 初始化;

91.3 银行账户实例

#include <iostream>
using namespace std;
//封装 银行账号 类
class BankAccount
{
private:
    double balance; //余额
    static double interest_rate; //利率  静态数据成员
public:
    //无参构造函数
    BankAccount() {}
    //有参构造函数
    BankAccount(double m):balance(m)
    {}
    //静态成员函数 获取当前利率
    static double getInterestRate()
    {
        return interest_rate;
        //balance = 100;  不能访问非静态数据成员
    }
    //静态成员函数 设置当前利率
    static void setInterestRate(double rate)
    {
        interest_rate = rate;
    }
    //静态成员函数 获取连本带利的余额
    static double getLastMoney(BankAccount &account)
    {
        return account.balance*(1+interest_rate);
    }
};
double BankAccount::interest_rate = 0.03; //静态数据成员必须在类外初始化
int main()
{
    cout << BankAccount::getInterestRate() << endl;
    BankAccount::setInterestRate(0.05);
    cout << BankAccount::getInterestRate() << endl;
    BankAccount account1(1000.0);
    cout << BankAccount::getLastMoney(account1) << endl;
    return 0;
}

九十二、继承

92.1 继承 的 目的

  1. 实现代码的重用性 (复用性)
  2. 可以建立父类和子类之间的联系
  3. 多态的实现,通过继承,可以实现子类对父类的重写

92.2 继承

  • 保持已有类的特性,在原来的基础上,增加新的特性而构造出新类的过程 称 继承 / 派生
  • 被继承者 称为 : 父类 / 基类
  • 继承者 称为 : 子类 / 派生类

92.3 格式

class 类名:继承方式 类名
{
    子类的拓展;
};
//继承方式:  public公共继承    protected保护继承  private私有继承
//一般都是以共有继承

92.4 继承方式

  • public
  • protected
  • private
父类中数据成员权限public–protected–privatepublic–protected–privatepublic–protected–private
继承方式publicprotectedprivate
子类中从父类继承
下来的成员访问权限
public–protected–不可访问protected–protected–不可访问private–private–不可访问
  • 子类不可以访问从父类继承下来的私有数据成员
  • 子类可以访问从父类继承下来的受保护数据成员
  • 子类可以访问从父类继承下来的公有数据成员

92.5 继承中的特殊成员函数

92.5.1 构造函数

  • 基类的构造函数会被继承到派生类中,
  • 先调用基类的构造函数,再调用派生类的构造函数。

92.5.2 析构函数

  • 父类的析构函数会被继承到子类中,
  • 先析构子类再析构父类。

92.5.3 拷贝构造函数

  • 父类的拷贝构造函数会被继承到子类中,

  • 在子类中需要调用父类的拷贝构造函数时,执行子类从父类继承下来的数据成员的初始化工作。

  • 如果有深拷贝问题,则需要在父类和子类各自完成深拷贝工作。

92.5.4 拷贝赋值函数

  • 父类的拷贝赋值函数会被继承到子类中,
  • 在子类中需要调用父类的拷贝赋值函数,执行子类从父类继承下来的数据成员的赋值工作。
  • 注意:调用父类的拷贝赋值函数时,需要加上类名和作用域限定符
  • 如果有深拷贝问题,则需要在父类和子类各自完成深拷贝工作。

示例 :

#include <iostream>
using namespace std;
// 封装 人 类  父类(基类)
class Person
{
private:
    string name;
protected:
    int age;
public:
    int h;
public:
    //无参构造
    Person(){ cout << "父类的无参构造" << endl;}
    //有参构造函数
    Person(string n, int a, int h) :name(n),age(a),h(h)
    {
        cout << "父类的有参构造" << endl;
    }
    //拷贝构造函数
                 // const Person &other = other
                 //赋值兼容规则
                 //1.子类对象可以赋值给父类的对象
                 //2.子类对象可以初始化父类的引用
                 //3.父类的指针可以指向子类对象地址
    Person(const Person &other):name(other.name),age(other.age),h(other.h)
    {
        cout << "父类的拷贝构造" << endl;
    }
    //拷贝赋值函数
    Person &operator=(const Person &other)
    {
        name = other.name;
        cout << "父类的拷贝赋值" << endl;
        return *this;
    }
    //析构函数
    ~Person()
    {
        cout << "父类的析构函数" << endl;
    }
    void show()
    {
       cout << "父类" << endl;
    }
};
//封装 学生类  共有继承 人 类
class Stu:public Person  //子类  、派生类
{
private:
    int id;
    int math;
public:
    Stu() {cout << "子类的无参构造函数" << endl;}
    Stu(string n, int a, int h,int i, int m):Person(n,a,h),id(i),math(m)
    {
        cout << "子类的有参构造函数" << endl;
    }
    //拷贝构造函数
    Stu(const Stu &other):Person(other),id(other.id),math(other.math)
                          //Person(other)
    {
        cout << "子类的拷贝构造函数" << endl;
    }
    Stu &operator=(const Stu &other)
    {
        cout << "子类的拷贝赋值函数" << endl;
        id = other.id;
        Person::operator=(other);
        return *this;
    }
    ~Stu()
    {
        cout << "子类的析构函数" << endl;
    }
    void show()
    {
        cout << "子类" << endl;
        //cout << name << endl;  子类不可以访问从父类继承下来的私有数据成员
        cout << age << endl;  //子类可以访问从父类继承下来的受保护数据成员
        cout << h << endl; //子类可以访问从父类继承下来的公有数据成员
    }
};
int main()
{
    Stu s1;
    Stu s2("zhangsan",18,190,1001,99);
    //s2.show();  //默认调用子类的show
    s2.Person::show();  //调用从父类继承下来的show
    Stu s3(s2); //先调用父类的拷贝构造函数,再调用子类的拷贝构造函数
    s1 = s2;//先调用父类的拷贝赋值函数,再调用子类的拷贝赋值函数
    return 0;
}

92.6 小结

  1. 父类的数据成员初始化必须在子类之前,先调用父类的构造函数,再调用子类的构造函数。
  2. 当父类和子类出现同名同类型的函数时,不是重载,也不是重复定义,是重写,原因:作用域不同。
    如果子类实例化一个对象,对象调用该函数,默认调用是子类的函数,
    如果想调用父类的函数,则需要加上类名和作用域限定符。

九十三、多继承

93.1 概念

  • 多继承 :即一个子类可以有多个父类,它继承了多个父类的特性。

93.2 格式

class 子类名:继承方式1 基类名1,继承方式2 基类名2,······,继承方式n 基类名n
{
    子类自己的内容;
};

小作业

  • 多继承代码实现沙发床
  • 沙发床继承于沙发和床

我写的

#include <iostream>
using namespace std;

class Bed{
    friend class Sofa_Bed;
private:
    int size;
public:
    //无参构造
    Bed() {cout << "床的无参构造" << endl;}
    //有参构造
    Bed(int size):size(size) {cout << "床的有参构造" << endl;}
    //拷贝构造
    Bed(const Bed &other):size(other.size) {cout << "床的拷贝构造" << endl;}
    //拷贝赋值
    Bed &operator=(const Bed &other){
        cout << "床的拷贝赋值" << endl;
        this->size = other.size;
        return *this;
    }
    //析构函数
    ~Bed() {cout << "床的析构函数" << endl;}
    //功能函数
    void func() {cout << "能躺" << endl;}
};

class Sofa
{
    friend class Sofa_Bed;
private:
    int high;
public:
    //无参构造
    Sofa() {cout << "沙发的无参构造" << endl;}
    //有参构造
    Sofa(int high):high(high) {cout << "沙发的有参构造" << endl;}
    //拷贝构造
    Sofa(const Sofa &other):high(other.high) {cout << "沙发的拷贝构造" << endl;}
    //拷贝赋值
    Sofa &operator=(const Sofa &other) {
        cout << "沙发的拷贝赋值函数" << endl;
        this->high = other.high;
        return *this;
    }
    //析构函数
    ~Sofa() {cout << "沙发的析构函数" << endl;}
    //功能函数
    void func() {cout << "能坐" << endl;}
};

class Sofa_Bed:protected Bed, protected Sofa
{
private:
    string color;
public:
    //无参构造
    Sofa_Bed() {cout << "沙发床的无参构造" << endl;}
    //有参构造
    Sofa_Bed(string color, int size, int high):Bed(size),Sofa(high),color(color) {cout << "沙发床的有参构造" << endl;}
    //拷贝构造
    Sofa_Bed(const Sofa_Bed &other):Bed(other),Sofa(other),color(other.color) {cout << "沙发床的拷贝构造" << endl;}
    //拷贝赋值
    Sofa_Bed &operator=(const Sofa_Bed &other){
        this->color = other.color;
        this->Bed::operator=(other);
        this->Sofa::operator=(other);
        cout << "沙发床的拷贝赋值" << endl;
        return *this;
    }
    //析构
    ~Sofa_Bed() {cout << "沙发床的析构函数" << endl;}
    //功能
    void func() {
        cout << "沙发床" << endl;
        this->Bed::func();
        this->Sofa::func();
    }
    void show_info(){
        cout << "大小 : " << this->Bed::size << endl;
        cout << "高度 : " << this->high << endl;
        cout << "颜色 : " << this->color << endl;
    }
};

int main()
{
    Sofa_Bed sb1;
    Sofa_Bed sb2("黑色", 256, 56);
    puts("");
    sb2.func();
    sb2.show_info();
    puts("");
    sb1 = Sofa_Bed("灰色", 128, 90);//调用的是拷贝赋值,将匿名对象的值拷贝赋值给sb2
    puts("");
    sb1.show_info();
    puts("");
    return 0;
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhk___

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值