Midterm Exam (3)
C++ Programming
NCNU CSIE

Date: May 25th, 2011
Time: 14:10-15: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).

    #include <iostream>

    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 };
        CRECT bRect = { 10, 10, 110, 110 };
        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).

    // Operator Overloading
    #include <iostream>
    using std::cout;
    using std::endl;

    class CComplex
    {
    public:
    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(5,2);
    CComplex c2(5,0);
    CComplex c3 = c2 + c1;
    c1.Show();
    c2.Show(); // "cout << 3.0" will display "3".
    c3.Show();
    }



  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).
    // Virtual Function
    #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; }
    };

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

    class Window // Base class for C++ virtual function example
    {
    public:
    virtual void Create() // virtual function for C++ virtual function example
    { cout <<"Base class Window" << endl; }
    };

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

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

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

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


  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;

    // Select the brush
    CBrush* pOldBrush = (CBrush*)pDC->SelectStockObject(NULL_BRUSH);
    for (int i=10; i<60; i+=10)
    pDC->Rectangle(i, 60-i, 110-i, 50+i);
    }


  6. (10%) If we replace the OnDraw() function in P.953 as below, what expression should be filled in the blanks if we want to obtain the 10 circles in the following figure?

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

        for (int i=0; i<10; i++)
            pDC->Ellipse(100+i*50, 100,
    __________, 200);
    }

    Circles