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

科目名稱:程式設計 開課系所:資訊工程 學系 考試日期 2016.6.2
系所別:
年級:
學號:
姓名:
考試時間 08:10-08: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).
    // Operator Overloading (P.331)
    #include <iostream>

    class CRational
    {
        int numerator;
        int denominator;
    public:
        CRational(int n, int d): numerator(n), denominator(d) {}

        bool operator<(CRational b)
        {
            if (numerator * b.denominator < denominator * b.numerator)
                return true;
            else
                return false;
        }
    };

    int main()
    {
        CRational a(2,3), b(1,2);
        std::cout << (a<b?"a<b":"a>=b") << std::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).
    // The Size of a Vector (P.495)
    #include <iostream>
    #include <vector>
    using std::vector;

    int main()
    {
        vector<int> p, q;
        for (int i=0; i<10+1; i++) p.push_back(0);
        for (int i=0; i<15+1; i++) q.push_back(0);
        for (int i=0; i<20+1; i++) p.push_back(0);
        std::cout << p.size() << '\t' << q.size() << std::endl;
        return 0;
    }