Midterm Exam (2)
C++ Programming
NCNU CSIE

Date: April 8th, 2011
Time: 08:30-10:10
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).

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

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

    int m_Length;
    int m_Width;
    int m_Height;
    };

    int main()
    {
    CBox myBox(4, 8, 3);

    cout << myBox.Volume() << endl;
    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).

    #include <iostream>          // static data member
    #include <cstring>
    using std::cout;
    using std::endl;

    class CMessage
    {
    private:
    char* pmessage; // Pointer to object text string


    public:
    static int objectCount;
    // Constructor definition
    CMessage(const char* text = "Default message")
    {
    pmessage = new char[strlen(text) + 1]; // Allocate space for text
    strcpy(pmessage, text); // Copy text to new memory
    cout << ++objectCount << " : " << pmessage << endl;
    }

    ~CMessage()
    { delete[] pmessage;
    objectCount--;
    }
    };

    int CMessage::objectCount = 0;

    void f1()
    {
    CMessage msg1("Sunday");
    CMessage msg2("Monday");
    }

    void f2()
    {
    CMessage msg2("Tuesday");
    CMessage msg3("Wednesday");
    }

    int main()
    {
    f1(); f2();
    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).
    #include <iostream>	// Assume the default pack size is 8 bytes in Visual C++
    using std::cout;
    using std::endl;

    class Node
    {
    Node* left;
    short value;
    Node* right;
    };

    class C2
    {
    Node* left;
    double value;
    Node* right;
    };

    int main()
    {
    cout << "Node: " << sizeof(Node) << endl;
    cout << "C2: " << sizeof(C2) << endl;
    return 0;
    }

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

    int gcd(int a, int b)
    {
    if (b == 0) return a;
    else return gcd(b, a % b);
    }

    class CRational
    {
    public:
    int numerator; // 分子
    int denominator; // 分母

    CRational(int p=0, int q=1) : numerator(p), denominator(q)
    { if (q==0)
    { cout << "Error! The denominator cannot be 0.\n";
    exit(1);
    }
    }

    void Print()
    { Reduce();
    cout << numerator;
    if (denominator != 1)
    cout << "/" << denominator;
    cout << endl;
    }

    void Reduce()
    { int i = gcd(numerator, denominator);
    numerator /= i;
    denominator /=i;
    }
    };

    int main()
    {
    CRational a(12,8);
    CRational b(3,6);
    a.Print(); b.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).
    #include <iostream>			
    using std::cout;
    using std::endl;

    int gcd(int a, int b)
    {
    if (b == 0) return a;
    else return gcd(b, a % b);
    }

    class CRational
    {
    public:
    int numerator; // 分子
    int denominator; // 分母

    CRational(int p=0, int q=1) : numerator(p), denominator(q)
    { if (q==0)
    { cout << "Error! The denominator cannot be 0.\n";
    exit(1);
    }
    }

    void Print() const // This line is different. Does it matter?
    { Reduce();
    cout << numerator;
    if (denominator != 1)
    cout << "/" << denominator;
    cout << endl;
    }

    void Reduce()
    { int i = gcd(numerator, denominator);
    numerator /= i;
    denominator /=i;
    }
    };

    int main()
    {
    CRational a(12,8);
    CRational b(3,6);
    a.Print(); b.Print();
    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).
    #include <iostream>		// Inheritance
    #include <cstring>
    using std::cout;
    using std::endl;

    class CBox
    {
    public:
    CBox(int lv = 2, int wv = 3, int hv = 5):
    m_Length(lv), m_Width(wv), m_Height(hv){}
    private:
    int m_Length;
    int m_Width;
    int m_Height;
    };

    class CCandyBox: CBox
    {
    public:
    char m_Contents[10];

    CCandyBox(char* str = "Candy") // Constructor
    { strcpy(m_Contents, str); }

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

    int main()
    {
    CCandyBox myCandyBox;

    cout << myCandyBox.Volume() << 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).

    #include <iostream>
    #include <cstring>
    using std::cout;
    using std::endl;

    class CBox
    {
    public:
        CBox(int lv = 2, int wv = 3, int hv = 5):
                                    m_Length(lv), m_Width(wv), m_Height(hv){}
    protect:
        int m_Length;
        int m_Width;
        int m_Height;
    };

    class CCandyBox: CBox
    {
      public:
        char m_Contents[10];

        CCandyBox(char* str = "Candy")               // Constructor
        {     
          strcpy(m_Contents, str);
        }
        int Volume() const
        { return m_Length * m_Width * m_Height; }
    };

    int main()
    {
      CCandyBox myCandyBox;

      cout << myCandyBox.Volume() << 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).
    #include <iostream>		// Operator overloading	
    using std::cout;
    using std::endl;

    class CRational
    {
    public:
    int numerator; // 分子
    int denominator; // 分母

    CRational(int p=0, int q=1)
    : numerator(p), denominator(q)
    {
    if (q==0)
    {
    cout << "Error! The denominator cannot be 0.\n";
    exit(1);
    }
    }

    void Print()
    {
    cout << numerator;
    if (denominator != 1)
    cout << "/" << denominator;
    cout << endl;
    }

    CRational& operator=(const CRational x)
    {
    if (this == &x)
    return *this;
    numerator = x.numerator;
    denominator = x.denominator;
    return *this;
    }
    };

    int main()
    {
    CRational a;
    CRational b(3);
    CRational c(1,7);
    (a = b) = c; a.Print();
    a = b = c; a.Print();
    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).
    #include <iostream>
    #include <cstring>
    using std::cout;
    using std::endl;

    class CBox
    {
    public:
    CBox(int lv = 2, int wv = 3, int hv = 5):
    m_Length(lv), m_Width(wv), m_Height(hv){}
    int Volume() const
    { return m_Length * m_Width * m_Height; }

    int m_Length;
    int m_Width;
    int m_Height;
    };

    class CCandyBox: protected CBox
    {
    public:
    char m_Contents[10];

    CCandyBox(char* str = "Candy") // Constructor
    { strcpy(m_Contents, str); }
    };

    int main()
    {
    CCandyBox myCandyBox;

    cout << myCandyBox.Volume() << endl;
    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).

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

    class CBox                             // Base class
    {
      public:
        // Function to show the volume of an object
        void ShowVolume() const
        {
          cout << "CBox usable volume is " << Volume() << endl;
        }

        // Function to calculate the volume of a CBox object
        double Volume() const
        { return m_Length*m_Width*m_Height; }

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

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

    class CGlassBox: public CBox           // Derived with protected specifier
    {
      public:
        // Function to calculate volume of a CGlassBox
        // allowing 15% for packing
        double Volume() const
        { return 0.5*m_Length*m_Width*m_Height; }

        // Constructor
        CGlassBox(double lv, double wv, double hv): CBox(lv, wv, hv){}
    };

    int main()
    {
      CGlassBox myGlassBox(2.0, 3.0, 4.0);  // Declare derived box of same size
      CBox* pBox(NULL);       // Declare a pointer to base class objects

      pBox = &myGlassBox;        // Set pointer to derived class object
      pBox->ShowVolume();        // Display volume of derived box
      cout << pBox->Volume() << endl;
      return 0;
    }