國立暨南國際大學 101 學年度第二學期小考試卷                   (考試時間: 14:10-14:20)
科目名稱:程式設計 開課系所:資訊工程 學系 任課教師
吳坤熹
系所別:
年級:
學號:
姓名:
考試日期
2013.4.16
  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>

    class CBox
    {
    public:
        CBox(double lv, double wv, double hv)
            : m_Length(lv), m_Width(wv), m_Height(hv) {}
    protected:
        double m_Length;
        double m_Width;
        double m_Height;
    };

    class CCandyBox : CBox   // The default default access specifier will be private.
    {
    public:
        CCandyBox(double lv, double wv, double hv)
            : CBox(lv, wv, hv) {}
        double Volume()
        { return m_Length * m_Width * m_Height; }
    };

    int main()
    {
        CCandyBox abox(1.0, 2.0, 3.0);
        std::cout << abox.Volume() << std::endl;
        return 0;
    }



  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).

    // Class Inheritance
    #include <iostream>

    class CBox
    {
    public:
        CBox(double lv, double wv, double hv)
            : m_Length(lv), m_Width(wv), m_Height(hv) {}
    private:   // Please notice that these data members are private.
        double m_Length;
        double m_Width;
        double m_Height;
    };

    class CCandyBox : CBox
    {
    public:
        CCandyBox(double lv, double wv, double hv)
            : CBox(lv, wv, hv) {}
        double Volume()
        { return m_Length * m_Width * m_Height; }
    };

    int main()
    {
        CCandyBox abox(1.0, 3.0, 5.0);
        std::cout << abox.Volume() << std::endl;
        return 0;
    }



  3. (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).
    // Memory alignment
    #pragma pack(8)
    #include <iostream>
    using std::endl;
    using std::cout;

    int main()
    {
    class C1
    {
    public:
    int a;
    char b;
    int c;
    };

    class C2
    {
    public:
    short a1;
    short a2;
    char b;
    short c1;
    short c2;
    };

    cout << "Size of C1 is " << sizeof(C1) << endl;
    cout << "Size of C2 is " << sizeof(C2) << endl;

    return 0;
    }