科目名稱:程式設計 | 開課系所:資訊工程 學系 | 任課教師 |
吳坤熹 |
||
系所別: |
年級: |
學號: |
姓名: |
考試日期 |
2009.5.20 |
(考試時間: 8:10-8:40)
// Variation of Ex9_05.cpp
#include <iostream> // For stream I/O
#include <cstring> // For strlen() and strcpy()
using std::cout;
using std::endl;
class CBox // Base class definition
{
public:
// Base class constructor
CBox(double lv = 1.0, double wv = 1.0, double hv = 1.0):
m_Length(lv), m_Width(wv), m_Height(hv)
{ cout << endl << "CBox constructor called"; }
// Copy constructor
CBox(const CBox& initB)
{
cout << endl << "CBox copy constructor called";
m_Length = initB.m_Length;
m_Width = initB.m_Width;
m_Height = initB.m_Height;
}
protected:
double m_Length;
double m_Width;
double m_Height;
};
class CCandyBox: public CBox
{
public:
char* m_Contents;
// Derived class function to calculate volume
double Volume() const
{ return m_Length*m_Width*m_Height; }
// Constructor to set dimensions and contents
// with explicit call of CBox constructor
CCandyBox(double lv, double wv, double hv, char* str = "Candy")
:CBox(lv, wv, hv) // Constructor
{
cout << endl <<"CCandyBox constructor2 called";
m_Contents = new char[ strlen(str) + 1 ];
strcpy(m_Contents, str);
}
// Constructor to set contents
// calls default CBox constructor automatically
CCandyBox(char* str = "Candy") // Constructor
{
cout << endl << "CCandyBox constructor1 called";
m_Contents = new char[ strlen(str) + 1 ];
strcpy(m_Contents, str);
}
// Derived class copy constructor
CCandyBox(const CCandyBox& initCB)
: CBox(initCB.m_Length, initCB.m_Width, initCB.m_Height)
{
cout << endl << "CCandyBox copy constructor called";
// Get new memory
m_Contents = new char[ strlen(initCB.m_Contents) + 1 ];
// Copy string
strcpy(m_Contents, initCB.m_Contents);
}
~CCandyBox() // Destructor
{ delete[] m_Contents; }
};
int main()
{
CCandyBox chocBox(2.0, 3.0, 4.0, "Chockies"); // Declare and initialize
CCandyBox chocolateBox(chocBox); // Use copy constructor
cout << endl
<< "Volume of chocBox is " << chocBox.Volume()
<< endl
<< "Volume of chocolateBox is " << chocolateBox.Volume()
<< endl;
return 0;
}
(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).
// Friend functions
#include <iostream>
using std::cout;
class CBottle; // Class Prototype
class CCarton
{
public:
CCarton(const CBottle& aBottle);
double Volume();
private:
double m_Length; // Carton length
double m_Width; // Carton width
double m_Height; // Carton height
};
class CBottle
{ // Let the carton constructor in
friend CCarton::CCarton(const CBottle& aBottle);
public:
CBottle(double height, double diameter)
{
m_Height = height;
m_Diameter = diameter;
}
private:
double m_Height; // Bottle height
double m_Diameter; // Bottle diamter
};
CCarton::CCarton(const CBottle& aBottle)
{
m_Height = aBottle.m_Height; // Bottle height
m_Length = 4.0*aBottle.m_Diameter; // Four rows of ...
m_Width = 3.0*aBottle.m_Diameter; // ... three bottles
}
double CCarton::Volume()
{ return m_Length * m_Width * m_Height; }
int main()
{
CBottle beer(3.0, 5.0);
CCarton aCarton(beer);
cout << aCarton.Volume() << "\n";
}