Midterm Exam (3)
C++ Programming
NCNU CSIE

Date: May 18, 2012
Time: 08:30-10:30
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 CBox
    {
    private:
        int m_Length;
        int m_Width;
        int m_Height;
    protect:
        int Length() { return m_Length; }
        int Width() { return m_Width; }
        int Height() { return m_Height; }
    public:
        CBox(int l, int w, int h):
          m_Length(l), m_Width(w), m_Height(h)
          { }
        void ShowHeight()
        { cout << Height(); }
    };

    int main()
    {
        CBox box1(1,2,3);
        box1.ShowHeight();
        return 0;
    }


  2. (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).
    // Inheritance
    #include <iostream>

    class CBox
    {
    public:
    int m_Length;
    int m_Width;
    int m_Height;

    CBox(int lv = 1, int wv = 2, int hv = 3)
    : m_Length(lv), m_Width(wv), m_Height(hv) {}
    int Volumn() { return m_Length * m_Width * m_Height; }
    };

    class CCandyBox: public CBox
    {
    public:
    char m_Contents[10];
    CCandyBox(const char *str = "Candy")
    {
    strcpy(m_Contents, str);
    }
    CCandyBox(int lv, int wv, int hv, char *str)
    : CBox(lv, wv, hv)
    {
    strcpy(m_Contents, str);
    }
    };

    int main()
    {
    CCandyBox myCandyBox(2, 3, 4, "Chocolate");
    CCandyBox myBox;
    std::cout << sizeof(myCandyBox) << std::endl;
    std::cout << myBox.Volumn() << std::endl;
    return 0;
    }

  3. (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).
    // Size of a binary tree
    #include <iostream>

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

    class CBinaryTree
    {
    public:
    int value;
    CBinaryTree* left;
    CBinaryTree* right;
    CBinaryTree(int n, CBinaryTree* p1 = NULL,
    CBinaryTree* p2 = NULL):
    value(n), left(p1), right(p2) {}
    };

    int main()
    {
    CBinaryTree G(0);
    CBinaryTree F(6);
    CBinaryTree E(9);
    CBinaryTree D(0);
    CBinaryTree C(1, &F, &G);
    CBinaryTree B(9, &D, &E);
    CBinaryTree A(2, &B, &C);

    cout << sizeof(A) << endl;
    return 0;
    }


  4. (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).
    // Recursively output a binary tree
    #include <iostream>

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

    class CBinaryTree
    {
    public:
    int value;
    CBinaryTree* left;
    CBinaryTree* right;
    CBinaryTree(int n, CBinaryTree* p1 = NULL,
    CBinaryTree* p2 = NULL):
    value(n), left(p1), right(p2) {}

    void Print()
    {
    if (left) left->Print();
    cout << value;
    if (right) right->Print();
    }
    };

    int main()
    {
    CBinaryTree G(0);
    CBinaryTree F(6);
    CBinaryTree E(9);
    CBinaryTree D(0);
    CBinaryTree C(1, &F, &G);
    CBinaryTree B(9, &D, &E);
    CBinaryTree A(2, &B, &C);

    C.Print();
    return 0;
    }


  5. (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).
    // Friend function
    #include <iostream>

    using std::cout;
    using std::endl;
    using std::ostream;

    class CBinaryTree
    {

    friend ostream& operator<<(ostream& os, CBinaryTree bt)
    {
    bt.Print();
    return os;
    }

    public:
    int value;
    CBinaryTree* left;
    CBinaryTree* right;
    CBinaryTree(int n, CBinaryTree* p1 = NULL,
    CBinaryTree* p2 = NULL):
    value(n), left(p1), right(p2) {}

    void Print()
    {
    cout << value;
    if (left) left->Print();
    if (right) right->Print();
    }
    };

    int main()
    {
    CBinaryTree G(0);
    CBinaryTree F(6);
    CBinaryTree E(9);
    CBinaryTree D(0);
    CBinaryTree C(1, &F, &G);
    CBinaryTree B(9, &D, &E);
    CBinaryTree A(2, &B, &C);

    cout << A << endl;;
    return 0;
    }
  6. (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).
    // Overloading the increment operator
    #include <iostream>
    #include <string>

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

    class Word
    {
    private:

    public:
    string word;
    Word(): word("A") {} ;

    Word& operator++()
    {
    word.insert(0, "B");
    return *this;
    }

    const Word operator++(int)
    {
    Word w = *this;
    word.append("C");
    return w;
    }

    void Print()
    { cout << word << endl; }

    friend ostream& operator<<(ostream& os, Word a) // P.569
    { os << a.word;
    return os;
    }

    };

    int main()
    {
    Word a;
    ++a;
    a++;
    cout << ++a;
    cout << a++;
    cout << endl << a;
    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).
    // Size of an object with static members
    #include <iostream>
    #include <string>

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

    class MyView
    {
    public:
    char type[12];
    MyView* nextView;
    static MyView* firstView;
    static MyView* lastView;

    MyView(const char* str)
    {
    strcpy(type, str);
    if (firstView == NULL)
    firstView = lastView = this;
    else
    {
    lastView->nextView = this;
    lastView = this;
    }
    nextView = NULL;
    }
    };

    MyView* MyView::firstView = NULL;
    MyView* MyView::lastView = NULL;

    int main()
    {
    MyView view1("Bar Chart");
    MyView view2("Line Chart");
    MyView* p;
    for (p=view1.firstView; p; p = p->nextView)
    cout << p->type << endl;
    cout << sizeof(MyView) << endl;
    return 0;
    }

  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).
    // Virtual function
    #include <iostream>

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

    class Window // Base class
    {
    public:
    virtual void Create() // virtual function
    { cout <<"Base class Window"<< endl; }
    };

    class CommandButton : public Window
    {
    public:
    void Create()
    { cout<<"Derived class Command Button"<< endl; }
    };

    int main()
    {
    Window *x, *y;

    x = new Window;
    x->Create();

    y = new CommandButton;
    y->Create(); // y is a pointer to the base class type
    return 0;
    }
  9. (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).
    // Pointer to the base class
    #include <iostream>

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

    class Window // Base class
    {
    public:
    void Create() // This is not declared virtual
    { cout <<"Base class Window"<< endl; }
    };

    class CommandButton : public Window
    {
    public:
    void Create()
    { cout<<"Derived class Command Button"<< endl; }
    };

    int main()
    {
    Window *x, *y;

    x = new Window;
    x->Create();

    y = new CommandButton;
    y->Create(); // y is a pointer to the base class type
    return 0;
    }
  10. (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).
    // typedef
    #include <iostream>
    #include <vector>
    #include <list>

    using std::cout;
    using std::endl;
    using std::vector;
    using std::list;

    typedef list<int>::iterator IT;

    class CMyList: public list<int>
    {
    public:
    CMyList(IT i1, IT i2)
    {
    for (IT iter = i1; iter != i2; iter++)
    push_back(*iter);
    }

    void Print()
    {
    for (IT iter = begin(); iter != end(); iter++)
    cout << *iter << ' ';
    cout << endl;
    }
    };

    int main()
    {
    int phone[] = { 0, 4, 9, 2, 9, 1, 0, 9, 6, 0 };
    vector<int> number(phone, phone+sizeof(phone)/sizeof(phone[0]) );
    list<int> data(number.begin() + 4, number.end());
    CMyList data2(data.begin() + 2, data.end());
    data2.Print();
    return 0;
    }
  11. (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).
    // Vector Capacity
    #include <iostream>
    #include <vector>

    using std::cout;
    using std::endl;
    using std::vector;

    int main()
    {
    vector<int> a;
    a.reserve(6);
    for (int i=3; i<18; i++)
    a.push_back(5);
    cout << a.size() << '\t'
    << a.capacity() << '\n';
    return 0;
    }