Final Exam
C++ Programming
NCNU CSIE

Date: June 22nd, 2011
Time: 14:10-15:30
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).

    #include <iostream>    // Declaring struct and class.

    using std::cout;
    using std::endl;

    struct RECT
    {
         int left;
        int top;
        int right;
        int bottom;
    }

    class CRECT
    {
    public:
        int left;
        int top;
        int right;
        int bottom;

    CRECT(int x1, int y1, int x2, int y2) // Constructor
         : left(x1), top(y1), right(x2), bottom(y2) {}

    int Area()
         { return (right-left)*(bottom-top); }

    }

    int main()
    {
        RECT aRect = { 10, 10, 60, 60 };    // Initial value
        CRECT bRect(10, 10, 110, 110);      // Initial value
        cout << (aRect.right - aRect.left) * (aRect.bottom - aRect.top)
                    << endl;
        cout << bRect.Area() << endl;

        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).

    // Accessing the members of a class
    #include <iostream>
    using std::cout;
    using std::endl;

    class CComplex
    {
    double real;
    double imaginary;

    CComplex(double a = 0.0, double b = 0):
    real(a), imaginary(b)
    { }

    CComplex operator+(CComplex w)
    { return CComplex(real - w.real, imaginary - w.imaginary); }

    void Show()
    {
    if (real != 0.0) cout << real;
    if (imaginary < 0.0)
    cout << imaginary << "i";
    else if
    (imaginary > 0.0)
    cout << "+" << imaginary << "i";
    cout << "\n";
    }

    };

    int main()
    {
    CComplex c1(6,2);
    CComplex c2(2,1);
    CComplex c3 = c2 + c1;
    c3.Show(); // "cout << 3.0" will display "3".
    }



  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).
    // Inheritance
    #include <iostream>
    using std::cout;
    using std::endl;

    class Window // Base class
    {
    public:
    void Create()
    { cout <<"Base class Window" << endl; }
    };

    class CommandButton : public Window
    {
    public:
    virtual void Create() // "Try to Override a C++ function"
    { cout<<"Derived class Command Button" << endl; }
    };

    int main()
    {
    Window *x, *y;

    x = new Window();
    x->Create();

    y = new CommandButton();
    y->Create();
    }


  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).
    #include <iostream>		// Overloading the Increment Operator
    using std::cout;
    using std::endl;

    class CChar
    {
    private:
    char ch;
    public:
    CChar(char c): ch(c) {}
    void Show() { cout << ch << endl; }
    CChar& operator++() { ch++; return *this; } // Prefix increment operator
    const CChar CChar::operator++(int) // Postfix increment operator
    { CChar c = *this;
    ch--;
    return c;
    }
    };

    int main()
    {
    CChar choice('D');
    choice++;
    choice.Show();
    return 0;
    }



  5. (10%) If we replace the OnDraw() function in P.953 as below, what result will be shown on the screen after you run the program?  Please specify the coordinates of endpoints of each segment.

    void CSketcherView::OnDraw(CDC* pDC)
    {
    CSketcherDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    if (!pDoc)
    return;

    pDC->Arc(20,20, 180,180, 180,100, 100,60);
    pDC->MoveTo(180,100);
    pDC->LineTo(100,100);
    pDC->LineTo(100,180);
    }

  6. (10%) If we replace the OnDraw() function in P.953 as below, what result will be shown on the screen after you run the program?  Please specify the coordinates of endpoints of each segment.
        void CSketcherView::OnDraw(CDC* pDC)
    {
    CTestDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    if (!pDoc)
    return;

    const int j = 0;
    const int k = 400;
    const int level = 5;
    square(pDC, CPoint(j, j), CPoint(j, k), CPoint(k, k), CPoint(k, j), level);
    }


    Assume that in the SketcherView class, we defined two public member functions:

    CPoint middle(CPoint p1, CPoint p2)
    {
    return CPoint( (p1.x+p2.x)/2, (p1.y+p2.y)/2 );
    }

    void square(CDC* pDC, CPoint p1, CPoint p2, CPoint p3, CPoint p4, int n)
    {
    pDC->MoveTo(p1);
    pDC->LineTo(p2);
    pDC->LineTo(p3);
    pDC->LineTo(p4);
    pDC->LineTo(p1);
    if (n>1)
    square(pDC, middle(p1, p2), middle(p2, p3), middle(p3, p4), middle(p4, p1), n-1);
    }
  7. (10%) If we replace the OnDraw() function in P.953 as below, what result will be shown on the screen after you run the program? 

    void CSketcherView::OnDraw(CDC* pDC)
    {
        CTestDoc* pDoc = GetDocument();
        ASSERT_VALID(pDoc);
        if (!pDoc)
            return;

        CPoint a(CPoint(0,0));
        CPoint b(CPoint(0,0));
        if ( a == b )
            AfxMessageBox(_T("The two points are equal."), MB_OK);
        else
            AfxMessageBox(_T("The two points are not identical."), MB_OK);

    }


  8. (10%) If we replace the OnDraw() function in P.953 as below, what result will be shown on the screen after you run the program?  Please specify the coordinates of endpoints of each segment.
        void CSketcherView::OnDraw(CDC* pDC)
    {
    CTestDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    if (!pDoc)
    return;

    const int j = 0;
    const int k = 300;
    const int level = 3;

    box(pDC, k/3, k, k*2/3, k, level);
    }


    Assume that in the SketcherView class, we defined a public member function:

    void box(CDC* pDC, int x1, int y1, int x2, int y2, int n)
    {
    int length;
    if (y1 == y2)
    {
    length = x2 - x1;
    pDC->Rectangle(x1, y1 - length, x2, y2);
    if (n>1)
    {
    box(pDC, x1, y1-length/3, x1, y1-length*2/3, n-1);
    box(pDC, x1 + length/3, y1 - length, x1 + length*2/3, y1 - length, n-1);
    box(pDC, x2, y2-length*2/3, x2, y2-length/3, n-1);
    }
    }
    else // (x1 == x2)
    {
    length = y1 - y2;
    pDC->Rectangle(x1, y1, x2 - length, y2);
    if (n>1)
    {
    box(pDC, x1-length/3, y1, x1-length*2/3, y1, n-1);
    box(pDC, x1-length, y1-length/3, x1-length, y1-length*2/3, n-1);
    box(pDC, x2-length*2/3, y2, x2-length/3, y2, n-1);
    }
    }
    }