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

科目名稱:程式設計 開課系所:資訊工程 學系 考試日期 2014.5.6
系所別:
年級:
學號:
姓名:
考試時間 18:10-18:20
  1. (10%) Determine whether the following code is correct or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).
    // Accessing the Data Members of a Class    (P.367)
    #include <iostream>
    using std::cout;
    using std::endl;

    class CCircle
    {
        int x;
        int y;
        int r;
    };

    int main()
    {
        CCircle aCircle;
        aCircle.x = 10;
        aCircle.y = 10;
        aCircle.r = 5;
        cout << aCircle.x + aCircle.y + aCircle.r << endl;
        return 0;
    }


  2. (10%) Determine whether the following code is correct or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).
    // Class Constructors (P.374)
    #include <iostream>
    using std::cout;
    using std::endl;

    class CCircle
    {
    public:
        int x;
        int y;
        int r;

        CCircle(int a, int b, int c)
        {
            x = a;
            y = b;
            r = y;
        }
    };

    int main()
    {
        CCircle aCircle(10, 10, 5);
        cout << aCircle.x + aCircle.y + aCircle.r << endl;
        return 0;
    }
  3. (10%) Determine whether the following code is correct or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).
    // Pointers to Class Objects (P.401)
    #include <iostream>
    using std::cout;
    using std::endl;

    class CCircle
    {
    public:
        int x;
        int y;
        int r;

        CCircle(int a, int b, int c)
        {
            x = a;
            y = 5;
            r = c;
        }
    };

    int main()
    {
        CCircle aCircle(10, 10, 5);
        CCircle pCircle = &aCircle;
        cout << aCircle.x * pCircle->r << endl;
        return 0;
    }