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

科目名稱:程式設計 開課系所:資訊工程 學系 考試日期 2015.5.13
系所別:
年級:
學號:
姓名:
考試時間 08:20-08:30
  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).
    // Define a Class
    #include <iostream>
    using std::cout;
    using std::endl;

    class CRational
    {
        int numerator;
        int denominator;
    };


    int main()
    {
        CRational A;
        A.numerator = 1;
        A.denominator = 3;
        cout << A.numerator << '/' << A.denominator << 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).
    // Constructor
    #include <iostream>
    using std::cout;
    using std::endl;

    class CRational
    {
        int numerator;
        int denominator;

    public:
        CRational(int p, int q = 1)
        {
            numerator = p; denominator = q;
            cout << numerator << '/' << denominator
                 << " created." << endl;
        }
    };


    int main()
    {
        CRational A(1, 3);
        CRational B(2);
        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).
    // Member Function
    #include <iostream>
    using std::cout;
    using std::endl;

    class CRational
    {
        int numerator;
        int denominator;

    public:
        CRational(int p, int q = 1)
        { numerator = p; denominator = q; }
        void Print()
        { cout << numerator << '/' << denominator << endl; }
    };


    int main()
    {
        CRational A(2, 5);
        Print(A);
        return 0;
    }