國立暨南國際大學 97 學年度第二學期小考試卷

科目名稱:程式設計 開課系所:資訊工程 學系 任課教師
吳坤熹
系所別:
年級:
學號:
姓名:
考試日期
2009.5.20

(考試時間: 14:10-14:40)


  1. (10%) Point out the syntax error in the following code.
    // Variation of Ex9_05.cpp

    #include <iostream> // For stream I/O
    #include <cstring> // For strlen() and strcpy()
    using std::cout;
    using std::endl;

    class CBox // Base class definition
    {
    public:
    // Base class constructor
    CBox(double lv = 1.0, double wv = 1.0, double hv = 1.0):
    m_Length(lv), m_Width(wv), m_Height(hv)
    { cout << endl << "CBox constructor called"; }

    // Copy constructor
    CBox(const CBox& initB)
    {
    cout << endl << "CBox copy constructor called";
    m_Length = initB.m_Length;
    m_Width = initB.m_Width;
    m_Height = initB.m_Height;
    }

    protected:
    double m_Length;
    double m_Width;
    double m_Height;
    };

    class CCandyBox: public CBox
    {
    public:
    char* m_Contents;

    // Derived class function to calculate volume
    double Volume() const
    { return m_Length*m_Width*m_Height; }

    // Constructor to set dimensions and contents
    // with explicit call of CBox constructor
    CCandyBox(double lv, double wv, double hv, char* str = "Candy")
    :CBox(lv, wv, hv) // Constructor
    {
    cout << endl <<"CCandyBox constructor2 called";
    m_Contents = new char[ strlen(str) + 1 ];
    strcpy(m_Contents, str);
    }

    // Constructor to set contents
    // calls default CBox constructor automatically
    CCandyBox(char* str = "Candy") // Constructor
    {
    cout << endl << "CCandyBox constructor1 called";
    m_Contents = new char[ strlen(str) + 1 ];
    strcpy(m_Contents, str);
    }

    // Derived class copy constructor
    CCandyBox(const CCandyBox& initCB)
    : m_Length(initCB.m_Length), m_Width(initCB.m_Width),
    m_Height(initCB.m_Height)
    {
    cout << endl << "CCandyBox copy constructor called";

    // Get new memory
    m_Contents = new char[ strlen(initCB.m_Contents) + 1 ];

    // Copy string
    strcpy(m_Contents, initCB.m_Contents);
    }

    ~CCandyBox() // Destructor
    { delete[] m_Contents; }
    };

    int main()
    {
    CCandyBox chocBox(2.0, 3.0, 4.0, "Chockies"); // Declare and initialize
    CCandyBox chocolateBox(chocBox); // Use copy constructor

    cout << endl
    << "Volume of chocBox is " << chocBox.Volume()
    << endl
    << "Volume of chocolateBox is " << chocolateBox.Volume()
    << endl;

    return 0;
    }



  2. (10%) Point out the syntax error in the following code.

    // Friend functions
    #include <iostream>
    using std::cout;

    class CCarton
    {
    public:
    CCarton(const CBottle& aBottle);
    double Volume();
    private:
    double m_Length; // Carton length
    double m_Width; // Carton width
    double m_Height; // Carton height
    };

    class CBottle
    { // Let the carton constructor in
    friend CCarton::CCarton(const CBottle& aBottle);
    public:
    CBottle(double height, double diameter)
    {
    m_Height = height;
    m_Diameter = diameter;
    }
    private:
    double m_Height; // Bottle height
    double m_Diameter; // Bottle diamter
    };

    CCarton::CCarton(const CBottle& aBottle)
    {
    m_Height = aBottle.m_Height; // Bottle height
    m_Length = 4.0*aBottle.m_Diameter; // Four rows of ...
    m_Width = 3.0*aBottle.m_Diameter; // ... three bottles
    }

    double CCarton::Volume()
    { return m_Length * m_Width * m_Height; }

    int main()
    {
    CBottle beer(3.0, 5.0);
    CCarton aCarton(beer);
    cout << aCarton.Volume() << "\n";
    }






  3. (10%) If we replace the OnDraw() function in P.715 as below, please draw a figure to depict what result we shall obtain.

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

        int x0 = 10;
        int y0 = 210;
        POINT p1, p2;
        for (int i=0; i<=10; i++)
        {
            p1.x = i*20 + x0; p1.y = y0;
            p2.x = x0;        p2.y = y0 - (10-i)*20;
            pDC->MoveTo(p1);
            pDC->LineTo(p2);
        }
    }