Final Exam
C++ Programming
NCNU CSIE

Date: June 11th, 2013
Time: 14:30-16: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).

    // Defining a struct
    #include<iostream>
    using std::cout;

    struct Rational
    {
        int numerator;
        int denominator;
    }

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

    int main()
    {
        Rational A = { 1, 3 };
        Print(A);
        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).
    // Defining a class 
    #include<iostream>
    using std::cout;

    class CRational
    {
    public:
    // Constructor
    CRational(int p = 0, int q = 1): numerator(p), denominator(q) { }

    int numerator;
    int denominator;

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



    int main()
    {
    CRational A = {1, 3};
    A.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).
    // Friend function
    #include<iostream>
    using std::cout;

    class CRational
    {
    public:
    // Constructor
    CRational(int p = 0, int q = 1): numerator(p), denominator(q) { }

    protected:
    int numerator;
    int denominator;

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



    int main()
    {
    CRational A(3);
    A.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).
    // Copy Constructor
    #include<iostream>
    using std::cout;

    class CRational
    {
    public:
    int numerator;
    int denominator;

    // Constructor
    CRational(int p = 0, int q = 1): numerator(p), denominator(q)
    { cout << "Constructor called.\n"; }
    // Copy Constructor
    CRational(CRational &a): numerator(1), denominator(1)
    { cout << "Copy constructor called.\n"; }

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

    int main()
    {
    CRational A;
    A.Print();
    CRational B = A;
    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).

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

    class CRational
    {
    public:
    int numerator;
    int denominator;

    // Constructor
    CRational(int p = 0, int q = 1): numerator(p), denominator(q)
    { }
    // Copy Constructor
    CRational(CRational &a): numerator(1), denominator(1)
    { }
    CRational& operator=(CRational c)
    {
    // Note that passing-by-value will copy an CRational object to c,
    // which will invoke the copy constructor. See pp.405-406
    numerator = c.numerator;
    denominator = c.denominator;
    return *this;
    }
    void Print()
    { cout << numerator << '/' << denominator << '\n'; }
    };

    int main()
    {
    CRational A(1, 3);
    CRational B;
    B.Print();
    B = A;
    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).
    // String Objects
    #include <iostream>
    #include <string>
    using std::cout;
    using std::string;

    int main()
    {
    string title = "China's super-secret space city";
    size_t offset = 0;
    for (int i=0; i<3; i++)
    offset = title.find('s', offset + 1);

    title.replace(offset,
    title.find(' ', offset) - offset,
    "beautiful");
    cout << title << '\n';
    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).

    // 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; }
    };

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

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

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



  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*2/3, k, k/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 = x1 - x2;
    pDC->Rectangle(x1, y1, x2, y2 + length);
    if (n>1)
    {
    box(pDC, x1, y1+length/3, x1, y1+2*length/3, n-1);
    box(pDC, x1 - length/3, y2 + length, x1 - length*2/3, y2 + length, n-1);
    box(pDC, x2, y1+length*2/3, x2, y1+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, x1-length*2/3, y2, x1-length/3, y2, n-1);
    }
    }
    }
  9. (10%) Consider an MFC application with the following code.  What string will be shown in the edit box after the user presses the button?

    void CView::OnBnClickedButton1()
    {
        CString str;
        str.Format(_T("%06.3f"), 3.14159);
        // The function _T() makes your string
        // suitable for either ASCII code or Unicode
        GetDlgItem(IDC_EDIT1)->SetWindowTextW(str);
    }

    edit box

  10. (10%) After you design a form and place some radio buttons, how do you change the "tab order" of controls?