Midterm Exam (3)
C++ Programming
NCNU CSIE

Date: May 27th, 2009
Time: 14:10-16:10
Open book; turn off computer & mobile phone

  1. Determine whether the following code has syntax errors or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).
    // Class
    #include <iostream>
    using std::cout;

    class CRectangle
    {
    int left;
    int top;
    int right;
    int bottom;
    public:
    CRectangle(int l, int t, int r, int b) : left(l), top(t), right(r), bottom(b) {}
    int Area() { return (right-left) * (bottom - top); }
    };

    int main()
    {
    CRectangle R1(0,0,10,20);
    CRectangle R2 = R1;
    cout << R2.Area();
    }



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

    class CList
    {
    public:
    static int count;
    void Inc() { count++; }
    void Dec() { count--; }
    };

    int main()
    {
    CList L2;
    L2.count = 527;
    L2.Inc();
    cout << L2.count << "\n";
    }

  3. Determine whether the following code has syntax errors or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).
    // const Object of a Class
    #include <iostream>
    using std::cout;

    class CBox
    {
    public:
    int volume;
    };

    int Compare(CBox B1, CBox& B2)
    {
    return (B1.volume > B2.volume);
    }

    int main()
    {
    const CBox cigar; cigar.volume = 10;
    CBox chocolate; chocolate.volume = 20;
    if (Compare(cigar, chocolate))
    cout << "cigar is larger.\n";
    else
    cout << "chocolate is larger than or equal to cigar.\n";
    }


  4. Determine whether the following code has syntax errors 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 CBox
    {
    public:
    int volume;
    char* color;
    CBox(int v, char* c = "Violet"): volume(v)
    {
    color = new char[strlen(c)];
    strcpy(color, c);
    }
    };

    int main()
    {
    CBox cigar(10);
    CBox chocolate(cigar);
    strcpy(chocolate.color, "Cyan");
    cout << chocolate.color << "\n";
    }


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

    #include <iostream>
    using std::cout;

    class CBox
    {
    public:
            int volume;
            CBox() { volume = 10; }
    };

    class CContainer : private CBox
    {
            char* color;
    public:
            CContainer()
            {
                    volume = 20;
                    color = new char[strlen("Violet")];
                    strcpy(color, "Violet");
            }

            CContainer(CContainer& b)
            {
                    color = new char[strlen(b.color)];
                    strcpy(color, b.color);
            }

            void ShowVolume()
            {
                    cout << volume << "\n";
            }
    };

    int main()
    {
            CContainer cigar;
            CContainer chocolate(cigar);
            chocolate.ShowVolume();
    }


  6. Determine whether the following code has syntax errors 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 CList
    {
    public:
    int count;
    CList(int i) : count(i) {}
    CList operator++() { count+=2; return *this; }
    CList operator++(int) { count+=10; return *this; }
    };


    int main()
    {
    CList L2(5);
    L2++;
    cout << L2.count << "\n";
    }


  7. Determine whether the following code has syntax errors or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).
    // Access Control Under Inheritance
    #include <iostream>
    using std::cout;

    class John
    {
    protected:
    int money;
    public:
    John(int i) : money(i) {}
    };

    class Johnson : public John
    {
    public:
    Johnson(int i) : John(i) {}
    void Show() { cout << money << "\n"; }
    };

    int main()
    {
    Johnson(1500);
    Johnson.Show();
    }


  8. Determine whether the following code has syntax errors or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).
    // Friendship
    #include <iostream>
    using std::cout;

    class CBox
    {
    private:
    int volume;
    public:
    CBox(int i) : volume(i) {};
    friend int main();
    };

    int main()
    {
    CBox b1(10);
    cout << b1.volume << "\n";
    }


  9. Determine whether the following code has syntax errors 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 Animal
    {

    public:
    int weight;
    Animal(int w = 5) : weight(w) {} ;
    int Cost() { return weight; }
    void ShowCost() { cout << Cost() << "\n"; }
    };

    class Wolf : public Animal
    {
    public:
    Wolf() : Animal() {} ;
    int Cost() { return weight * 2 ; }
    };

    int main()
    {
    Wolf a1;
    a1.ShowCost();
    }




  10. If we replace the OnDraw() function in P.715 as below, what result will be shown on the screen after you run the program?
    void CSketcherView::OnDraw(CDC* pDC)
    {
    CSketcherDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    if (!pDoc)
    return;

    pDC->Ellipse(50,0, 150, 100);
    pDC->Ellipse(0,50, 100, 150);
    pDC->Ellipse(50,100, 150, 200);
    pDC->Ellipse(100, 50, 200, 150);
    pDC->Rectangle(50, 50, 150, 150);

    }

  11. If we replace the OnDraw()function in P.715 as below, what result will be shown on the screen after you run the program?

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


            CBrush* pOldBrush = static_cast<CBrush*>( pDC->SelectStockObject(NULL_BRUSH) );
            int i;
            int x0 = 100;
            int y0 = 100;
            for (i=1; i<10; i++)
                    pDC->Rectangle(x0-i*10,y0-i*10,x0+i*10,y0+i*10);

    }