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

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

(考試時間: 8:10-8:30)
Open book; turn off computer & mobile phone

  1. (10%) Point out the problem of the following code and use an example to explain why.
    // Header file CandyBox.h in project Ex9_01
    #pragma once
    #include "Box.h"
    class CCandyBox: CBox
    {
    public:
    char* m_Contents;

    CCandyBox(char* str = "Candy") // Constructor
    {
    m_Contents = new char[ strlen(str) + 1 ];
    strcpy_s(m_Contents, strlen(m_Contents), str);
    }

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


  2. (10%) What will be the output of the following code?
    #include <iostream>
    using std::cout;
    using std::endl;

    class CBox
    {
    public:
    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"; }

    ~CBox() { cout << "CBox destructor called" << endl; }

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

    class CCandyBox: public CBox
    {
    public:
    char* m_Contents;
    CCandyBox(double lv, double wv, double hv, char* str = "Candy")
    :CBox(lv, wv, hv)
    {
    cout << endl << "CCandyBox constructor called";
    m_Contents = new char [ strlen(str) + 1 ];
    strcpy_s(m_Contents, strlen(str) + 1, str);
    }

    ~CCandyBox()
    {
    cout << "CCandyBox destructor called" << endl;
    }
    };

    int main()
    {
    CCandyBox myCandyBox(1, 2, 3);
    CCandyBox myToffeeBox(2, 3, 4, "Stickjaw Toffee");
    cout << endl;
    }