Final Exam
C++ Programming
NCNU CSIE

Date: June 22nd, 2012
Time: 08:30-09:30
Open book; turn off computer & mobile phone

  1. (10%) Determine whether the following code has syntax errors or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).

    // Define a class
    #include <iostream>
    using std::cout;
    using std::endl;

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

    int main()
    {
      CBox box1;                   // Declare box1 of type CBox

      box1.m_Height = 3.0;         // Define the values
      box1.m_Length = 7.0;         // of the members of
      box1.m_Width  = 2.0;         // the object box1 

      cout << "Volume of box1 = "
           <<  box1.m_Height * box1.m_Length * box1.m_Width
           << endl;
      return 0;
    }



  2. (10%) Determine whether the following code has syntax errors 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;
    using std::endl;

    class CBox // Class definition at global scope
    {
    public:
    // Constructor definition
    explicit CBox(double lv, double bv = 2.0, double hv = 3.0)
    {
    m_Length = lv; // Set values of
    m_Width = bv; // data members
    m_Height = hv;
    }

    CBox(CBox& initB)
    {
    m_Length = initB.m_Length;
    m_Width = initB.m_Width;
    m_Height = initB.m_Height;
    }

    private: // What would happen if data members are private?
    double m_Length; // Length of a box in inches
    double m_Width; // Width of a box in inches
    double m_Height; // Height of a box in inches
    };

    int main()
    {
    CBox box1(5.0);
    CBox box2(box1);
    CBox box3 = box2;
    cout << box3.Volume() << endl;
    return 0;
    }


  3. (10%) Determine whether the following code has syntax errors 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:
    void Create()
    { cout <<"Base class Window" << endl; }
    };

    class CommandButton : public Window
    {
    public:
    virtual void Create() // "Try to Override a C++ function"
    { cout<<"Derived class Command Button" << endl; }
    };

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

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

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


  4. (10%) Determine whether the following code has syntax errors or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).
    // Destructor
    #include <iostream>
    using std::cout;
    using std::endl;

    class CBox // Class definition at global scope
    {
    public:
    // Constructor definition
    explicit CBox(double lv, double bv = 2.0, double hv = 3.0)
    {
    m_Length = lv; // Set values of
    m_Width = bv; // data members
    m_Height = hv;
    }

    CBox(CBox& initB)
    {
    m_Length = initB.m_Length;
    m_Width = initB.m_Width;
    m_Height = initB.m_Height;
    }

    ~CBox()
    {
    cout << "Destroy a " << m_Length << " x "
    << m_Width << " x " << m_Height << " box.\n";
    }

    double m_Length; // Length of a box in inches
    double m_Width; // Width of a box in inches
    double m_Height; // Height of a box in inches
    };

    int main()
    {
    CBox box1(5.0);
    CBox box2(1.0, 2.0);
    CBox box3 = box2;
    box3.m_Length = 3.0;
    return 0;
    }
  5. (10%) Determine whether the following code has syntax errors or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).

    // Using a static member of a class

    #include <iostream>
    using std::cout;

    class CBox
    {
    private:
        static int count;
        static void ShowIt() { cout << count << '\n'; }
        // Can it be a private member function?
    public:
        CBox()  { ++count; }
        ~CBox() { --count; }
    };

    int CBox::count = 0;

    int main()
    {
        CBox cigar[2];
        cigar[0].ShowIt();
        CBox candybox;
        CBox::ShowIt();
        return 0;
    }


  6. (10%) Determine whether the following code has syntax errors or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).

    // Overloaded assignment operator for CBox objects
    #include <iostream>
    using std::cout;

    class CBox
    {
        private:
            int weight;
        public:
            void ShowWeight() const
            { cout << weight << "\n"; }

            CBox(const int w = 0)   // Constructor definition
            { weight = w; }

            CBox operator=(const CBox& aBox)
            {
                if (this == &aBox)  // Check adresses, if equal
                    return *this;   // return the 1st operand

                // Copy 2nd operand to 1st
                weight = aBox.weight;

                return *this;
            }
    };

    int main()
    {
        CBox box1(10);
        CBox box2;
        CBox box3(box1);
        (box3 = box2) = box1;
        box3.ShowWeight();
        box3 = box2 = box1;
        box3.ShowWeight();
        return 0;
    }