國立暨南國際大學 97 學年度第二學期小考試卷

科目名稱:程式設計 開課系所:資訊工程 學系 任課教師
吳坤熹
系所別:
年級:
學號:
姓名:
考試日期
2009.4.29

(考試時間: 8:10-8:30)


  1. (10%) Determine whether the following code has syntax errors or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).
    // Class Inheritance
    #include <iostream>
    #include <cstring>
    using std::cout;
    using std::endl;

    class CBox
    {
    public:
    CBox(double lv = 1.0, double wv = 2.0, double hv = 3.0):
    m_Length(lv), m_Width(wv), m_Height(hv)
    { cout << "CBox constructor called.\n"; }
    double Volume() { return m_Length*m_Width*m_Height; }
    private:
    double m_Length;
    double m_Width;
    double m_Height;
    };

    class CCandyBox: public CBox
    {
    public:
    char* m_Contents;

    CCandyBox(char* str = "Candy") // Constructor
    {
    m_Contents = new char[ strlen(str) + 1 ];
    strcpy(m_Contents, str);
    { cout << "CCandyBox constructor called.\n"; }
    }

    ~CCandyBox() // Destructor
    { delete[] m_Contents; };
    };

    int main()
    {
    CCandyBox myMintBox("Wafer Thin Mints");
    cout << myMintBox.Volume() << "\n";
    }










  2.  (10%) Determine whether the following code has syntax errors or not.  If it is correct, predict its output.  If it is
    incorrect, point out the mistake(s).

    // Constructor of the base part
    #include <iostream>
    #include <cstring>
    using std::cout;
    using std::endl;

    class CBox
    {
    public:
    CBox(double lv = 1.0, double wv = 1.0, double hv = 1.0):
    m_Length(lv), m_Width(wv), m_Height(hv)
    { cout << "CBox constructor called.\n"; }

    double Volume() { return m_Length*m_Width*m_Height; }

    private:
    double m_Length;
    double m_Width;
    double m_Height;
    };

    class CCrate: public CBox
    {
    public:
    int m_nBottles;
    CCrate(int n = 12) : m_nBottles(n) // Constructor
    { cout << "CCrate constructor called.\n"; }
    };

    class CBeerCrate: public CCrate
    {
    public:
    char* m_Beer;
    CBeerCrate(char* str = "Heineken") // Constructor
    {
    m_Beer = new char [ strlen(str) + 1 ];
    strcpy(m_Beer, str);
    cout << "CBeerCrate constructor called.\n";
    }

    ~CBeerCrate()
    {
    delete [] m_Beer;
    }
    };

    int main()
    {
    CBeerCrate myBeer("Taiwan Beer");
    cout << myBeer.m_Beer << "\n";
    }