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

科目名稱:程式設計 開課系所:資訊工程 學系 考試日期 2020.3.26
系所別:
年級:
學號:
姓名:
考試時間 8:20-8:30
  1. (10%) Determine whether the syntax of the following code is correct or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).
    // Function Overloading
    #include <iostream>

    class Rational {
    public:
        Rational(int n = 0, int d = 1)      // Constructor
        : numerator(n), denominator(d) {}

        Rational(Rational& b)                // same function name
        : numerator(b.numerator), denominator(b.denominator) {}

        void print() {
            std::cout << numerator << '/' << denominator << ' '; }

    private:
        int numerator;
        int denominator;
    };

    int main() {
        Rational a;
        Rational b(2);
        Rational c(1,2);
        Rational d(b);
        a.print(); b.print(); c.print(); d.print();
        return 0;
    }


  2. (10%) Determine whether the syntax of the following code is correct or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).
    /* Destructor */
    #include <iostream>

    class Student {
    public:
        Student(char c) { name = c; }
        ~Student() { std::cout << "Destructor of " << name << " called.\n"; }
    private:
        char name;
    };

    void f1() { Student a('A'); }

    void f2() {
        Student b('B');
        f1();
    }

    int main()
    {
        f1();
        f2();
        return 0;
    }



  3. (10%) Determine whether the syntax of the following code is correct or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).
    /* Friend Function */
    #include <iostream>
    using std::cin;
    using std::cout;
    using std::endl;

    class Student {
    public:
        Student(char s): name(s) {}
    private:
        char name;
    };

    void Teacher(Student a) {
        friend Student;
        cout << a.name << endl;
    }

    int main()
    {
        Student a('A');
        Teacher(a);
        return 0;
    }