Midterm Exam (2)
C++ Programming
NCNU CSIE

Date: March 30th, 2012
Time: 09:10-11:00
Open book; turn off computer & mobile phone

  1. (10%) Determine whether the following code has syntax erros or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).

    // data member of a class
    #include <iostream>
    using std::cout;

    class CRational
    {
        int numerator;
        int denominator;

        CRational(int n, int d = 1): numerator(n)
        { denominator = 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 erros or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).
    // Constructor
    #include <iostream>
    using std::cout;

    class CRational
    {
        int numerator;
        int denominator;

    public:
        CRational(int n, int d = 1): numerator(n), denominator(d)
        { cout << "Constructor called for ";
          Print();
          cout << '\n';
        }

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

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


  3. (10%) Determine whether the following code has syntax erros or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).
    // data member of a class
    #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,3);
    CRational b(a);
    a = CRational(1,4);
    b.Print();
    return 0;
    }


  4. (10%) Determine whether the following code has syntax erros or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).
    // data member of a class
    #include <iostream>
    using std::cout;

    class CRational
    {
    public:
    int numerator;
    int *denominator;

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

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

    int main()
    {
    CRational a(1,3);
    CRational b(a);
    *(a.denominator) = 4;
    b.Print();
    return 0;
    }


  5. (10%) Determine whether the following code has syntax erros 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=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 = a; // Copy constructor is called
    b.Print();
    return 0;
    }


  6. (10%) Determine whether the following code has syntax erros 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=0, int d=1): numerator(n), denominator(d) {}
    CRational(CRational& a): numerator(1), denominator(1) {}

    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(2,3);
    CRational b = a; // Copy constructor is called
    b.Print();
    return 0;
    }

  7. (10%) Determine whether the following code has syntax erros 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) {}
    CRational(CRational& a): numerator(1), denominator(1) {}

    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(3,4);
    CRational c = b = a; // Copy constructor is called
    c.Print();
    return 0;
    }


  8. (10%) Determine whether the following code has syntax erros 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) {}
    CRational(CRational& a): numerator(1), denominator(1) {}

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

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

    int main()
    {
    CRational a(3,4);
    CRational c , b ; // Copy constructor is called
    c = b = a;
    c.Print();
    return 0;
    }


  9. (10%) Determine whether the following code has syntax erros or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).
    // When is Destructor called?
    #include <iostream>
    using std::cout;

    class CData
    {
    private:
    static int count;
    public:
    CData() { ++count; }
    ~CData() { --count; }
    void ShowIt() { cout << count << '\n'; }
    };

    int CData::count = 0;

    void test1()
    {
    CData htc;
    htc.ShowIt();
    }

    void test2()
    {
    CData iphone[4];
    iphone[0].ShowIt();
    }

    int main()
    {
    test2();
    test1();
    return 0;
    }

  10. (10%) Determine whether the following code has syntax erros 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 CRectangle
    {
    int left, top, right, bottom;

    int Area() const
    { return (right-left)*(bottom-top); }
    public:
    CRectangle(int x1, int y1, int x2, int y2):
    left(x1), top(y1), right(x2), bottom(y2) {}
    bool operator>(const CRectangle& aRect) const
    { return Area() > aRect.Area(); }
    };

    int main()
    {
    CRectangle pool(10, 20, 30, 50);
    CRectangle hut(20,30, 45, 50);
    if (pool > hut)
    cout << "The pool is bigger.\n";
    else
    cout << "The hut is bigger.\n";
    }