1. 程式人生 > 其它 >實驗四 繼承

實驗四 繼承

  • 實驗任務二
  • task2.cpp
  • task2.cpp(虛擬函式)
  • 總結:

    1.同名覆蓋原則

    派生類與基類中有相同成員時:
    若未強行指名,則通過派生類物件使用的是派生類的同名成員;

    2.二元作用域分辨符

    如果成員函式中定義了和類域中的變數同名的變數,則類域中的變數被塊作用域中的變數隱藏。這時可以在變數名前面加類名和作用域運算子(::)訪問這種隱藏變數。

    3.型別相容原則

    在需要基類物件的任何地方,都可以使用公有派生類的物件來替代,編譯器會隱式地執行派生類到基類的轉換
    存在三種轉換:
    ->>派生類的物件可以隱含轉換為基類物件。
    ->>派生類的物件可以初始化基類的引用。


    ->>派生類的指標可以隱含轉換為基類的指標。

  • 實驗任務三
  •  1 #ifndef CAR_HPP
     2 #define CAR_HPP
     3 #include <iostream>
     4 #include <string>
     5 
     6 using namespace std;
     7 
     8 class Car
     9 {
    10 public:
    11     Car(string maker0,string model0,int year0,int odometers0=0):maker(maker0),model(model0),year(year0),odometers(odometers0){ };
    
    12 void info()const; 13 void update_odometers(int new_odometers); 14 private: 15 string maker,model; 16 int year,odometers; 17 }; 18 void Car::info()const 19 { 20 cout << "maker:\t\t" << maker <<endl; 21 cout << "model:\t\t" << model <<endl;
    22 cout << "year:\t\t" << year <<endl; 23 cout << "odometers:\t" << odometers <<endl; 24 } 25 void Car::update_odometers(int new_odometers) 26 { 27 if(new_odometers<odometers) 28 cout << "資料錯誤,請重新輸入" << endl; 29 odometers=new_odometers; 30 } 31 #endif // CAR_HPP
    Car.hpp
  •  1 #ifndef BATTERY_HPP
     2 #define BATTERY_HPP
     3 #include<iostream>
     4 
     5 using namespace std;
     6 
     7 class Battery
     8 {
     9 public:
    10     Battery(int capacity0 = 70):capacity{capacity0} {};
    11     int get_capacity() {return capacity;}
    12 private:
    13     int capacity;
    14 };
    15 
    16 #endif // BATTERY_HPP
    Battery.hpp
  •  1 #ifndef ElECTRIC_CAR_HPP
     2 #define ElECTRIC_CAR_HPP
     3 #include "Car.hpp"
     4 #include "Battery.hpp"
     5 #include <iostream>
     6 #include <string>
     7 
     8 using namespace std;
     9 
    10 class ElectricCar:public Car
    11 {
    12 public:
    13     ElectricCar(string maker0,string model0,int year0,int odometers0=0,int capacity0=70):Car(maker0,model0,year0,odometers0),battery(capacity0) {};
    14     void info() ;
    15 private:
    16     Battery battery;
    17 };
    18 void ElectricCar:: info()
    19 {
    20     Car::info();
    21     cout << "capacity:\t" << battery.get_capacity() << endl;
    22 }
    23 #endif // ElECTRIC_CAR_HPP
    EletricCar.hpp
  •  1 #include <iostream>
     2 #include "electricCar.hpp"
     3 int main()
     4 {
     5     using namespace std;
     6     // test class of Car
     7     Car oldcar("Audi", "a4", 2016);
     8     cout << "--------oldcar's info--------" << endl;
     9     oldcar.update_odometers(25000);
    10     oldcar.info();
    11     cout << endl; // test class of ElectricCar
    12     ElectricCar newcar("Tesla", "model s", 2016);
    13     newcar.update_odometers(2500);
    14     cout << "\n--------newcar's info--------\n";
    15     newcar.info();
    16 }
    main.hpp
  • 實驗任務四
  •  1 #ifndef PETS_HPP
     2 #define PETS_HPP
     3 #include <iostream>
     4 #include <string>
     5 using namespace std;
     6 
     7 class MachinePets
     8 {
     9 public:
    10     MachinePets(const string s):nickname(s){ }
    11     virtual string talk() {return "a~";}
    12     string get_nickname() const {return nickname;}
    13 private:
    14     string nickname;
    15 };
    16 
    17 class PetCats:public MachinePets
    18 {
    19 public:
    20     PetCats(const string s):MachinePets(s) { }
    21     string talk() {return "miao wu~";}
    22 };
    23 
    24 class PetDogs:public MachinePets
    25 {
    26 public:
    27     PetDogs(const string s):MachinePets(s) { }
    28     string talk() {return "wang wang~";}
    29 };
    30 #endif // PETS_HPP
    Pets.hpp
  •  1 #include <iostream>
     2 #include "pets.hpp"
     3 
     4 void play(MachinePets *ptr)
     5 {
     6     std::cout << ptr->get_nickname() << " says " << ptr->talk() << std::endl;
     7 }
     8 int main()
     9 {
    10     PetCats cat("miku");
    11     PetDogs dog("da huang");
    12     play(&cat);
    13     play(&dog);
    14 }
    main.hpp