經過了漫長的等待,終於要在4/19迎來這學期首次大會考了,這是相當重要的一戰阿,我的命運究竟是從「計算機程式語言三」晉級「計算機程式語言四」,又或著是止步於此呢?將在下周揭曉~ 
在幾天前教授上傳了一些練習題,反正閒著也是閒著,就來小試身手一下吧。
第一題  
父類別跟main()不能動,基本上跟著題目要求走就好,繼承父類別,創造建構子,此題完成。
    
    
        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 #include  <bits/stdc++.h>  using  namespace  std;class  Father {    private :         int  money;     public :         Father (){             money = 100 ;             Print ();         }         Father (int  n){             money = n;             Print ();         }         void  Print ()  {             cout << money << "\n" ;         } }; class  Child :  public  Father{    public :         Child (int  n):Father (n){} }; int  main ()  {    int  n;     cout << "input money: " ;     cin >> n;     Child *ken = new  Child (n);     return  0 ; } 
  
      
     
第二題  
一樣父類別跟main()不能動。
由於money是private的,不能直接在子類別中使用,因此需利用父類別提供的函式取得其值。
另外因為父類別的Print()跟子類別的Print()名稱重複了,若想在子類別的Print()中呼叫父類別的Print(),要寫成Father::Print(),否則會視為呼叫自己,造成遞迴。
    
    
        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 #include  <bits/stdc++.h>  using  namespace  std;class  Father {    private :         int  money;     public :         Father (){             money = 200 ;         }         void  Print ()  {             cout << "money= "  << money << "\n" ;         }         int  MyMoney ()  {             return  money;         } }; class  Child :  public  Father{    private :         int  book;     public :         void  Print ()  {             int  price;             cout << "price: " ;             cin >> price;             book = MyMoney ()/price;             Father::Print ();             cout << "book= "  << book << "\n" ;         } }; int  main ()  {    Child john;     john.Print ();     return  0 ; } 
  
      
     
第三題  
父類別跟main()不能動。
因為Horse跟Donkey都繼承了Animal,擁有個別的leg,而Mule同時繼承了兩者,若在Mule類別內直接呼叫leg,編譯器會不知道你要哪一個,造成錯誤。這裡需使用virtual,將其leg視為一個。
    
    
        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 #include  <bits/stdc++.h>  using  namespace  std;class  Animal {    protected :         int  leg;     public :         Animal (){             leg = 4 ;         } }; class  Horse :  virtual  public  Animal{}; class  Donkey :  virtual  public  Animal{}; class  Mule :  public  Horse, public  Donkey{    public :         void  Print ()  {             cout << "Mule has "  << leg << "legs\n" ;         } }; int  main ()  {    Mule mule;     mule.Print ();     return  0 ; } 
  
      
     
第四題  
這題就單純把函式完成就好了,然後動態配置記憶體給其物件。
由於myClock是指向物件的指標,呼叫內部函式時需使用 -> 。
    
    
        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 #include  <bits/stdc++.h>  using  namespace  std;class  Clock {    private :         int  hour;         int  minute;         int  second;     public :         void  SetTime (int  h, int  m, int  s)  {             hour = h;             minute = m;             second = s;         }         void  WhatTime ()  {             cout << hour << ":"  << minute << ":"  << second << "\n" ;         } }; int  main ()  {    Clock *myClock = new  Clock;     int  hour, minute, second;     cout << "time= " ;     cin >> hour >> minute >> second;     myClock->SetTime (hour, minute, second);     myClock->WhatTime ();     return  0 ; } 
  
      
     
後記 其實打完這篇文時已經4/20號了,前幾天有點懶哈哈,不過我們的大會考也因為疫情的關係,從原本的上機考改成選擇加填充題,難度也沒有比較簡單,另外因為助教課有教Java,所以有考一點,然後就錯了…呼叫父類別函式的super.function()記成super::function(),阿我就不會Java嘛
另外還新增了口試這個項目,就是給你一些題目,要我們把程式碼生出來,問你為什麼這樣寫或這行什麼意思之類的,回答不出來還直接零分,萌新我瑟瑟發抖
挑戰一個接著一個,戰鬥一場接著一場,真是永遠都不會無聊的資工系生活呢!