[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
1.继承方式
语法:class 子类 :继承方式 父类
继承方式:
public: 公有继承
private:私有继承
protected:保护继承
class A { public: //可以继承,类外可以访问 int a; protected: //可以继承,类外不可以访问 int b;
private: //不可以继承,类外不可以访问 int c; } //公有继承 class B :public A { public: int a;
protected: int b; 不可访问: int c; } //保护继承 class B :protected A { protected: int
a; int b; 不可访问: int c; } //私有继承 class B :private A { private: int a; int b;
不可访问: int c; }
不管公有继承,保护继承,还是私有继承,基类中的私有属性,都不可以继承下去
2.对象模型
子类中,会继承父类中的私有成员,只是被编译器给隐藏起来,访问不到私有成员。
3.继承中的构造和析构函数
子类会继承父类的成员属性,成员函数。但是子类不会继承父类的构造函数和析构函数
构造函数:子类在创建对象时,先调用父类的构造,然后在调用子类的构造
如果父类中没有合适的默认构造函数,那么子类可以利用初始化列表的方式显示的调用父类的其他构造
Son(int a):Base(a) {}
4. 继承中的同名处理
当父类和子类中拥有同名的函数、属性的情况,子类对象可以直接访问到子类的成员。需要访问到父类的成员,需要添加作用域。
s1.m_A; //访问子类的成员 s1.Base::m_A; //访问父类的成员
如果子类与父类的成员函数名相同,子类会把父类的所有同名的版本都隐藏起来。如果需要调用,必须加上作用域。
5.继承中静态成员的处理
静态成员类内声明,类外初始化。
class Base { public: static int m_A; } int Base::m_A = 10;
静态成员属性,子类可以继承下来。
如果想访问父类的成员需要加上作用域
Son::Base::func()
6.多继承
多个继承可能会导致函数,变量名等同名导致较多的歧义(二义性 )
class A : public B1 ,public B2 {}
7.菱形继承
class Animal { public: int m_age; }; //虚基类 class sheep : virtual public Animal
{ }; //虚基类 class tuo :virtual public Animal { }; class sheeptuo :public
sheep,public tuo { }; sheeptuo st; st.sheep::m_age = 10; st.tuo::m_age = 20;
利用虚基类就不会有二义性,用的都是基类的值 (只有一份值,不会被继承下来,都是对应的指针)
原理:通过表找到偏移量