Mid-Term Exam (2)
C++ Programming
NCNU CSIE

Date: April 14th, 2010
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).

    // Accessing Private Class Members
    #include <iostream>
    using std::cout;
    class CNumber
    {
    int n;

    public:
    CNumber(int i=0): n(i) {}
    void Set(int i) { n=i; }
    int Get() { return n; }
    };

    int main()
    {
    CNumber N1(1);
    N1.Set(5);
    cout << N1.Get() << "\n";
    }

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

    // Const Member Function
    #include <iostream>
    using std::cout;
    class CNumber
    {
            int n;

    public:
            CNumber(int i=0): n(i) {}
            void Set(int i) const
                    { n=i; }
            int Get() { return n; }
    };

    int main()
    {
            CNumber N1(1);
            N1.Set(5);
            cout << N1.Get() << "\n";
    }

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

    // Copy Constructor
    #include <iostream>
    using std::cout;

    class CBox {

    public:
       int weight;

       CBox(int w=0) : weight(w)
       { cout << "Constructor called.\n"; }
       CBox(CBox& aBox)
       { cout << "Copy Constructor called.\n";
         this->weight = aBox.weight + 5;
       }
    };

    int main()
    {
       CBox box1(10);
       CBox box2; box2 = box1;
       CBox box3 = box1;
       cout << box2.weight << "\n";
       cout << box3.weight << "\n";
    }

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

    // Operator Overloading
    #include <iostream>
    using std::cout;
    class CRectangle
    {
    int left, top, right, bottom;

    int Area() const
    { return (right-left)*(bottom-top); }
    public:
    CRectangle(int x1, int y1, int x2, int y2):
    left(x1), top(y1), right(x2), bottom(y2) {}
    bool operator>(const CRectangle& aRect) const
    { return Area() > aRect.Area(); }
    };

    int main()
    {
    CRectangle pool(10, 20, 30, 50);
    CRectangle hut(20,30, 45, 50);
    if (pool > hut)
    cout << "The pool is bigger.\n";
    else
    cout << "The hut is bigger.\n";
    }
  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).

    // Destructor
    #include <iostream>
    using std::cout;
    class CNumber
    {
            int value;
            char* name;

    public:
            CNumber(int n = 0, char* str = "Zero"): value(n)
            {
                    name = new char[strlen(str) + 1];
                    strcpy(name, str);
            }
            ~CNumber()
            {
                    cout << "Destroying " << name << " with value " << value << "\n";
                    delete[] name;
            }
    };

    int main()
    {
            CNumber n0;
            CNumber n5(5, "Five");
    }


  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 Decrement Operator
    #include <iostream>
    using std::cout;

    char* Digits[] = { "Zero", "One", "Two", "Three", "Four",
                        "Five", "Six", "Seven", "Eight", "Night" };

    class CNumber
    {
            int value;
            char* name;

    public:
            CNumber(int n = 0): value(n)
            { name = Digits[n];     }
            CNumber& operator--()   // Prefix decrement operator
            {
                    --value;
                    return *this;
            }
            void Show()
            { cout << name << "\n"; }
    };


    int main()
    {
            CNumber i(5);   i.Show();
            --i; i.Show();
    }


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

    // Derived Class
    #include <iostream>
    using std::cout;

    class CBox
    {

            int m_Length;
            int m_Width;
            int m_Height;

    public:
            CBox(int lv, int wv, int hv): m_Length(lv), m_Width(wv), m_Height(hv) { }
            int Volume() { return m_Length * m_Width * m_Height; }
    };

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

    int main()
    {
            CCandyBox myCandy(2, 3, 4, "Wafer");
            cout << myCandy.Volume() << '\n';
    }


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

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

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

    public:
            CBox(int lv, int wv, int hv): m_Length(lv), m_Width(wv), m_Height(hv) { }
            int Volume() { return m_Length * m_Width * m_Height; }
    };

    class CCandyBox: protected CBox
    {
    private:
            char m_Contents[20];
    public:
            CCandyBox(int lv, int wv, int hv, char* str = "Candy"): CBox(lv, wv, hv)
            { strcpy(m_Contents, str); }
    friend int main();
    };

    int main()
    {
            CCandyBox myCandy(2, 2, 2, "Mint");
            cout << myCandy.Volume() << '\n';
    }


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

    // Virtual Function
    #include <iostream>
    using std::cout;

    class Window // Base class for C++ virtual function example
    {
      public:
         void Create() // virtual function for C++ virtual function example
         {
              cout << "Base class Window\n";
         }
    };

    class CommandButton : public Window
    {
      public:
         void Create()
         {
             cout<<"Derived class Command Button - Overridden C++ virtual function\n";
         }
    };

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

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

        y = new CommandButton();
        y->Create();
    }

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

    // C++ Preprocessor Directives
    #include <iostream>
    using std::cout;
    #define         AAA     5
    #define         F(x)    printf("The value of " #x " is %d.\n", x)
    int main()
    {
            int i;
            for (i=0; i<AAA; i++)
                    ;
            F(i);
    }