面向对象语言都有类,也都有继承,继承可以很大程度上减少代码量,而让代码逻辑更清晰。继承可以使得子类(派生类)具有父类(基类)的属性和方法或者重新定义、追加属性和方法等。
比如车是基类,轿车和公交车从车类派生而来,这就是一个继承的例子。
应该有的人会问了,这车继承例子跟编程好像比较远?如果有这想法就说明还没有面向对象编程思想。
下面写下车继承例子代码,让大家明白面向对象。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
// 梁笔记 // https://zouzhongliang.com class BaseCar{ private: string brand; int wheelNum; float carL; float carW; public: BaseCar() :brand(""), wheelNum(4), carL(3),carW(1.8) { } BaseCar(string _brand, int _wheelNum, float _carL, float _carW) :brand(_brand), wheelNum(_wheelNum), carL(_carL),carW(_carW) { } }; class Car : public BaseCar{ private: bool gps; public: Car() :BaseCar(), gps(true) { } Car(string _brand, int _wheelNum, float _carL, float _carW, bool _gps) :BaseCar(_brand,_wheelNum,_carL,_carW), gps(_gps) { } }; class Bus : public BaseCar{ private: bool electric; public: Bus() :BaseCar(), electric(true) { } Bus(string _brand, int _wheelNum, float _carL, float _carW, bool _electric) :BaseCar(_brand,_wheelNum,_carL,_carW), electric(_electric) { } }; |
以上代码写出了继承的关系,不用太在意设计的成员变量是否合理,现在有了大概了解类继承,再来写下与继承有关系的内容。
类继承的语法
1 2 3 4 5 6 7 8 9 10 11 |
class B : public A { }; class B : private A { }; class B : protected A { }; |
可以看到有三种权限的继承,public、private、protected,现在就这三种继承方式做说明
public公有继承:基类的非私有成员的访问属性在派生类中保持不变,派生类可以防问基类公有的和保护的部份
private私有继承:基类的非私有成员的访问属性在派生类变成私有成员,私有继承一般使用较少
protected保护继承:基类的非私有成员的访问属性在派生类变成保护成员
不管如何继承基类,基类中的私有成员都是无法访问。
继承后派生类在内存如何存放基类成员:
用上面车类定义代码来写一段测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
// 梁笔记 // https://zouzhongliang.com #include <iostream> #include <ctime> using namespace std; class BaseCar{ private: string brand; int wheelNum; float carL; float carW; public: BaseCar() :brand(""), wheelNum(4), carL(3),carW(1.8) { } BaseCar(string _brand, int _wheelNum, float _carL, float _carW) :brand(_brand), wheelNum(_wheelNum), carL(_carL),carW(_carW) { } void Baseprint(){ cout<<&brand<<endl; cout<<&wheelNum<<endl; cout<<&carL<<endl; cout<<&carW<<endl; } }; class Car : public BaseCar{ private: bool gps; public: Car() :BaseCar(), gps(true) { } Car(string _brand, int _wheelNum, float _carL, float _carW, bool _gps) :BaseCar(_brand,_wheelNum,_carL,_carW), gps(_gps) { } void print(){ Baseprint(); cout<<&gps<<endl; } }; class Bus : public BaseCar{ private: bool electric; public: Bus() :BaseCar(), electric(true) { } Bus(string _brand, int _wheelNum, float _carL, float _carW, bool _electric) :BaseCar(_brand,_wheelNum,_carL,_carW), electric(_electric) { } void print(){ Baseprint(); cout<<&electric<<endl; } }; int main() { cout<<endl; Bus bus; bus.print(); cout<<endl; Car car; car.print(); return 0; } |
继承后派生类在内存中测试结果:
1 2 3 4 5 6 7 8 9 10 11 |
0x66ff00 0x66ff04 0x66ff08 0x66ff0c 0x66ff10 0x66fee0 0x66fee4 0x66fee8 0x66feec 0x66fef0 |
从测试结果可以看出,类继承后,派生类的成员变量在内存中是连续存储的。前面基类成员变量,后面是派生类成员为变量。
Useful tips. Thank you. viagra without insurance
Many thanks! Quite a lot of content.
drugs for sale
Nicely put, Thanks a lot! How Much Does Augmentin Cost With Insurance
Whoa lots of fantastic material! canadian pharmacies that are legit
Reliable write ups. With thanks. canadian pharmacies online prescriptions
With thanks, Numerous advice.
Precios Cialis Farmacias Venezuela
Wonderful info. Kudos! generic levitra online
Very good knowledge. With thanks. atarax 25
Thank you, Good information! modafinil
Nicely put, Cheers. canadian pharmacies without an rx