國立暨南國際大學 100 學年度第二學期小考試卷                   (考試時間: 13:30-13:40)
科目名稱:資訊系統 與網路導論 開課系所:資訊工程 學系 任課教師
吳坤熹
系所別:
年級:
學號:
姓名:
考試日期
2012.3.28
  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).
    // Copy Constructor
    #include <iostream>
    using std::cout;

    class CRational
    {
    int numerator;
    int *denominator;

    public:
    CRational(int n, int d = 1): numerator(n)
    { denominator = new int(d); }

    void Print()
    { cout << numerator << '/' << *denominator; }
    };

    int main()
    {
    CRational a(1,2);
    CRational b(a);
    a = CRational(1,3);
    b.Print();
    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).
    // Operator Overloading
    #include <iostream>
    using std::cout;

    class CRational
    {
    int numerator;
    int denominator;

    public:
    CRational(int n=0, int d=1): numerator(n), denominator(d) {}

    void operator=(CRational a) // What should be the return type?
    { numerator = a.numerator;
    denominator = a.denominator;
    }

    void Print()
    { cout << numerator << '/' << denominator; }
    };

    int main()
    {
    CRational a(1,2);
    CRational b;
    b = a;
    b.Print();
    return 0;
    }