第 15 ç«  ç¹¼‰èˆ‡¤é‡ç¹¼‰

  • View
    56

  • Download
    0

Embed Size (px)

DESCRIPTION

第 15 章 繼承與多重繼承. 15-1 繼承的基礎 15-2 覆寫與隱藏父類別的成員 15-3 子類別的建構與解構子 15-4 多重繼承 15-5 軟體工程與繼承 15-6 類別的型態轉換與檢查. 15-1 繼承的基礎. 15-1-1 類別繼承的基礎 15-1-2 實作繼承 15-1-3 父類別的存取控制. 15-1-1 類別繼承的基礎 - 圖例. 「繼承」( Inheritance )是物件導向程式設計的一種進階觀念,繼承就是物件的再利用,當定義好一個類別後,其他類別可以繼承這個類別的成員資料和函數。 - PowerPoint PPT Presentation

Text of 第 15 章 ...

  • 15 15-1 15-2 15-3 15-4 15-5 15-6

  • 15-1 15-1-1 15-1-2 15-1-3

  • 15-1-1 -InheritancePersonPersonStudent

  • 15-1-1 -PersonStudentSuperclassBase ClassStudentPersonSubclassDerived ClassUML

  • 15-1-1 -RelationshipsClass Hierarchy

  • 15-1-1 -TruckCarMotorcycleSibling ClassesVehicleCarSportsCarJeepVehicleCarSportsCarJeepDirect Base ClassVehicleSportsCarJeepIndirect Base Class

  • 15-1-2 -vehicleclass vehicle {private: int engineNo; string owner;public: void setNumber(int no) { engineNo = no; } void setOwner(string owner) { this->owner = owner; } void printVehicle() { }};

  • 15-1-2 -()class : { // };:privateprotectedpublic

  • 15-1-2 -()carclass car : public vehicle {private: int doors;public: car(string owner, int no, int doors) { } void printCar() { }};

  • 15-1-2 -UMLUML

  • 15-1-2 -C++=

  • 15-1-3 -C++ {private: protected: public: };class : { };

  • 15-1-3 -privateprotectedpublic

    public

    protected

    private

    public

    public()

    protected()

    private()

    protected

    protected()

    protected()

    private()

    private

  • 15-1-3 -publicprotectedprivate

    public

    protected

    private

  • 15-2 15-2-1 15-2-2

  • 15-2-1 -Override

  • 15-2-1 -vehicleclass vehicle {private: public: static void showBrand() { } void printVehicle(int index) { }};showBrand()printVehicle()

  • 15-2-1 -carpublicvehicleclass car : public vehicle {private: public: static void showBrand() { } void printVehicle(int no) { }};carcar

  • 15-2-2 -

  • 15-2-2 -vehicleownerpublicstringclass vehicle {private: public: string owner; };

  • 15-2-2 -carpublicvehicleclass car : public vehicle {private: int owner; public: };carownerintstringpublicowner

  • 15-3 15-3-1 15-3-2

  • 15-3-1 Default Constructor

  • 15-3-2 carvehiclecarcar(int owner, int no, int doors) : vehicle(owner, no) { this->doors = doors;}:vehicle,ownerno

  • 15-4 -Multiple Inheritance

  • 15-4 -class : , .. { // };,

  • 15-4 -truckdriverdriven_tuckclass driven_truck : public truck, public driver {public: driven_truck(float pl, float fl, float pay, int pd): truck(pl, fl), driver( pay, pd) { } float costPerTom( float cost_of_gas ) { } void printData() { } };

  • 15-5 -RelationshipsIs-aGereralizationWhole-PartPart-ofHas-a

  • 15-5 -Part-ofHas-aPart-ofWheelDoorCarHas-aPart-ofCarHas-aWheelDoorUMLAggregationWhole-partComposition

  • 15-6 -ANSI-C++reinterpret_caststatic_castdynamic_castconst_caststatic_cast();const_cast();dynamic_cast();reinterpret_cast();ANSI-C++typeidtypeid()

  • 15-6 -static_castC++static_caststatic_castclass A {};class B: public A {};A *a = new A;B *b = static_cast(a);static_castAaBb

  • 15-6 -const_castC++const_castclass C {};const C *c1 = new C;C *c2 = const_cast(c1);const_castc1c2

  • 15-6 -dynamic_castC++dynamic_caststatic_castdynamic_castdynamic_castNULLclass D { virtual void dummy() {}; };class E: public D {};D *d1 = new E;D *d2 = new D;E *e1 = dynamic_cast(d1);E *e2 = dynamic_cast(d2); // NULL

  • 15-6 -reinterpret_castC++reinterpret_castclass F {};class G {};F *f = new F;G *g = reinterpret_cast(f);reinterpret_castFfGg

  • 15-6 -typeidC++typeid==!=class H {};H *h1;H h2;if ( typeid(h1) != typeid(h2) ) { cout