科目名稱:程式設計 | 開課系所:資訊工程 學系 | 任課教師 |
吳坤熹 |
||
系所別: |
年級: |
學號: |
姓名: |
考試日期 |
2009.4.29 |
(考試時間: 14:10-14:30)
// Private Inheritance
#include <iostream>
#include <cstring>
using std::cout;
using std::endl;
class CBox
{
public:
CBox(double lv = 1.0, double wv = 1.0, double hv = 1.0):
m_Length(lv), m_Width(wv), m_Height(hv)
{ cout << "CBox constructor called.\n"; }
double Volume() { return m_Length*m_Width*m_Height; }
private:
double m_Length;
double m_Width;
double m_Height;
};
class CCrate: private CBox
{
public:
int m_nBottles;
CCrate(int n = 12) : m_nBottles(n) // Constructor
{ cout << "CCrate constructor called.\n"; }
};
class CBeerCrate: private CCrate
{
public:
char* m_Beer;
CBeerCrate(char* str = "Heineken") // Constructor
{
m_Beer = new char [ strlen(str) + 1 ];
strcpy(m_Beer, str);
cout << "CBeerCrate constructor called.\n";
}
~CBeerCrate()
{
delete [] m_Beer;
}
};
int main()
{
CBeerCrate myBeer("Taiwan Beer");
cout << myBeer.m_Beer << "\n";
}
(10%) Will it still be valid if the function header of the CBeerCrate::CBeerCrate() constructor modified as
CBeerCrate(const char* str = "Heineken")
(10%) Will it still be valid if the function header of the CBeerCrate::CBeerCrate() constructor modified as
CBeerCrate(const char* str = "Heineken") const