Midterm Exam (3)
C++ Programming
NCNU CSIE

Date: May 27th, 2009
Time: 08:30-10:30
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:
    int Area() { return (right-left) * (bottom - top); }
    };

    int main()
    {
    CRectangle R1(0,0,10,20);
    cout << R1.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 CList::count = 527;

    int main()
    {
    CList L2;
    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).
    // Reference to Class Object
    #include <iostream>
    using std::cout;

    class CBox
    {
    public:
    int volume;
    };

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

    int main()
    {
    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);
    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).

    // Copy Constructor in a Derived Class
    #include <iostream>
    using std::cout;

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

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

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

    void ShowColor()
    {
    cout << color << "\n";
    }
    };

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

  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++; return *this; }
    CList operator--() { count--; 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
    {
    protect:
    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 person(1500);
    person.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() : volume(20) {};
    };

    int main()
    {
    friend CBox::CBox();
    CBox b1;
    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) {} ;
    virtual int Cost() { return weight; }
    void ShowCost() { cout << Cost() << "\n"; }
    };

    class Wolf : public Animal
    {
    public:
    Wolf() : Animal() {} ;
    virtual 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->Rectangle(50, 50, 150, 150);
    pDC->Arc(50,0, 150, 100, 150, 50, 50, 50);
    pDC->Arc(0,50, 100, 150, 50, 50, 50, 150);
    pDC->Arc(50,100, 150, 200, 50, 150, 150, 150 );
    pDC->Arc(100, 50, 200, 150, 150, 150, 150, 50);
    }
  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;

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