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

科目名稱:程式設計 開課系所:資訊工程 學系 考試日期 2014.5.20
系所別:
年級:
學號:
姓名:
考試時間 14:10-14: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.446)
    #include <iostream>
    using std::cout;
    using std::endl;

    class CData
    {
    public:
        int* pdata;

        CData(int v = 0)
        { pdata = new int(v); }

        CData Add(CData d)
        { return CData(*pdata - *d.pdata); }

        CData operator+(CData d)
        { return Add(d); }

        void Print()
        { cout << *pdata << endl; }
    };

    int main()
    {
        CData a(5), b(3);
        CData c;
        c = a + b;
        c.Print();
        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).
    // Overloading the Assignment Operator (P.454)
    #include <iostream>
    using std::cout;
    using std::endl;

    class CData
    {
    public:
        int* pdata;

        CData(int v = 0)
        { pdata = new int(v); }

        CData operator=(CData d)
        { return CData( *d.pdata ); }

        void Print()
        { cout << *pdata << endl; }
    };

    int main()
    {
        CData a(5), b(3);
        CData c;
        c = a;
        a = b;
        c.Print();
        return 0;
    }