Mid-term Exam (3)
C++ Programming
NCNU CSIE

Date:  May 7th, 2013
Time: 14:10-16:00
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).

    // Access Control in a Class
    #include <iostream>
    using std::cout;
    using std::endl;

    class CNode
    {
    private:
        CNode* next;
        int value;
    protect:
        int Value() { return value; }
    public:
        CNode(int v = 0):
          value(v), next(NULL)
          { }
        void ShowValue()
        { cout << Value(); }
    };

    int main()
    {
        CNode node1(7);
        node1.ShowValue();
        return 0;
    }

  2. (10%) What will be the output of the following program?

    // Copy Constructor
    #include <iostream>

    class CBox                          // Class definition at global scope
    {
    public:
        int m_Length;                   // Length of a box in inches
        int m_Width;                    // Width of a box in inches
        int m_Height;                   // Height of a box in inches

        CBox(int lv = 0, int wv = 0, int hv = 0)    // Constructor
            : m_Length(lv), m_Width(wv), m_Height(hv)
        { }

        CBox(const CBox& aBox)            // Copy Constructor
        {
            m_Length = aBox.m_Length;
            m_Width  = aBox.m_Width;
            m_Height = aBox.m_Height * 2;
        }

        int Volume()
        {
            return m_Length * m_Width * m_Height;
        }
    };

    int main()
    {
      CBox box1(7, 1, 5);           // Declare box1 of type CBox
      CBox box2(box1);              // Declare box2 of type CBox
      CBox box3 = box2;             // Declare box3 of type CBox
      CBox box4;                    // Declare box4 of type CBox
      box4 = box3;
      std::cout << box2.Volume() << std::endl;
      std::cout << box3.Volume() << std::endl;
      std::cout << box4.Volume() << std::endl;
      return 0;
    }

  3. (10%) What will be the output of the following program?

    // Inheritance
    #include <iostream>

    class CNode
    {
    public:
            int value;

            CNode(int v = 1) : value(v) {}
            int Value() { return value; }
    };

    class CTextNode: public CNode
    {
    public:
            char m_Contents[12];
            CTextNode(const char *str = "Candy")
            {
                    strcpy(m_Contents, str);
            }
            CTextNode(int v, char *str)
          : CNode(v)
            {
                    strcpy(m_Contents, str);
            }
    };

    int main()
    {
            CTextNode myCandyNode(2, "Chocolate");
            CTextNode myNode;
            std::cout << sizeof(myCandyNode) << std::endl;
            std::cout << myNode.Value() << std::endl;
            return 0;
    }


  4. (10%) What will be the output of the following program?

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

    class CData
    {
        public:
            int* pdata;

        CData(int n=0)
        {
            pdata = new(int);
            *pdata = n;      
        }

        ~CData()
        {
            cout << "Destructor called to release the memory storing "
                    << *pdata << endl;
            delete pdata;
        }

        CData& operator=(const CData& d)
        {
            if (this == &d)
               return *this;
            delete pdata;
            pdata = new int;
            *pdata =  --*(d.pdata);
            return *this;
        }
    };

    int main()
    {
        CData a(3);
        CData b(0);
        CData c(7);
        a = b = c;
        cout << *(a.pdata) << endl;
        return 0;
    }


  5. (10%) What will be the output of the following program?

    // Linked-List

    #include <iostream>
    using std::cout;

    class CList
    {
    public:
        class CNode
        {
        public:
            int value;
            CNode* previous;

            CNode(int v, CNode* p = NULL) : value(v), previous(p) {}
            ~CNode()
            {
                if (previous)
                    delete previous;
                cout << "Destroying " << value << std::endl;
            }
        };

        CNode* front;
        CNode* back;

        CList()
        {
            front = back = NULL;
        }

        CList(int v)
        {
            front = back = new CNode(v);      
        }  

        ~CList()
        {
            if (back != NULL)
                delete back;
        }

        void push(int v)
        {
            back = new CNode(v, back);
        }
          
    };

    int main()
    {
            CList a(5);
            a.push(0); a.push(7);
            return 0;
    }


  6. (10%) What will be the output of the following program?

    //
    Virtual Functions
    #include <iostream>
    #include <string>

    using std::cout;
    using std::endl;
    using std::string;

    class Window // Base class
    {
    public:
        string label;
        void Create() // virtual function
        { label = "Base"; }
    };

    class CommandButton : public Window
    {
    public:
        void Create()
        { label = "Button"; }
    };

    int main()
    {
        Window  *x, *y;   
     
        x = new Window;
        x->Create();
        cout << x->label << endl;

        y = new CommandButton;
        y->Create();    // y is a pointer to the base class type
        cout << y->label << endl;
        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).

    // Destructor
    #include <iostream>
    #include <string>

    using std::cout;
    using std::endl;
    using std::string;

    class Window // Base class
    {
    public:
        string label;
        virtual void Create() // virtual function
        { label = "Base"; }
        virtual ~Window()    // Destructor
        { cout << "Destroying " << label << " Window.\n"; }
    };

    class CommandButton : public Window
    {
    public:
        virtual void Create()
        { label = "Button"; }
        virtual ~CommandButton()
        { cout << "Destroying " << label << "CommandButton.\n"; }
    };

    int main()
    {
        Window  *x, *y;        // x and y are pointers, not objects
     
        x = new Window;
        x->Create();
        cout << x->label << endl;

        y = new CommandButton;
        y->Create();    // y is a pointer to the base class type
        cout << y->label << endl;
        return 0;        // Will any object be destroyed automatically?
    }

  8. (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).

    // Static Data Member
    #include <iostream>
    using std::cout;
    using std::endl;

    class CNode
    {
    public:
        int value;
        CNode* next;
        static CNode* pFirstNode;
        static CNode* pLastNode;

        CNode(int v) :
        value(v), next(NULL)
        {
            if (!pFirstNode)
                pFirstNode = this;
            if (pLastNode)                   
                pLastNode->next = this;
            pLastNode = this;
        }

        int Count()            // Calculate this carefully
        {
            int n = 0;
            CNode* p;
            for (p = pFirstNode; p != pLastNode; p = p->next)
                n++;
            return n;
        }
    };

    CNode* CNode::pFirstNode = NULL;
    CNode* CNode::pLastNode = NULL;

    int main()
    {
        CNode a(5), b(0), c(7);
        CNode* p;
        cout << a.Count() << endl;
        for (p=CNode::pFirstNode; p!=NULL; p=p->next)
            cout << p->value;
        cout << endl;
        return 0;
    }
  9. (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, or the coordinates of centers and boundaries.
    #include <cmath>
    #define PI 3.1415926535

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

    const int R = 100;
    const int N = 4;
    const double theta = 2 * PI / N;
    CPoint center(2*R, 2*R);
    CPoint a[N];
    int i;
    pDC->Ellipse(center.x - R, center.y - R, center.x + R, center.y + R);
    for (i=0; i<N; i++)
    {
    a[i].x = center.x + R * cos(i * theta);
    a[i].y = center.y + R * sin(i * theta);
    }

    pDC->MoveTo( a[0] );
    for (i=1; i<=N; i++)
    pDC->LineTo( a[ i % N ] );
    }
  10. (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, or the coordinates of centers and boundaries.
    #include <cmath>
    #define PI 3.1415926535

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

    const int R = 100;
    const int N = 8;
    const double theta = 2 * PI / N;
    CPoint center(2*R, 2*R);
    CPoint a[N];
    int i;
    pDC->Ellipse(center.x - R, center.y - R, center.x + R, center.y + R);
    for (i=0; i<N; i++)
    {
    a[i].x = center.x + R * cos(i * theta);
    a[i].y = center.y + R * sin(i * theta);
    }

    pDC->MoveTo( a[0] );
    for (i=1; i<=N; i++)
    pDC->LineTo( a[ 3*i % N ] );
    }