科目名稱:資訊系統 與網路導論 | 開課系所:資訊工程 學系 | 任課教師 |
吳坤熹 |
||
系所別: |
年級: |
學號: |
姓名: |
考試日期 |
2008.5.1 |
(考試時間: 8:10-8:25)
Open book; turn off computer & mobile phone
#include <iostream>
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)
{ }
protected:
double m_Length;
double m_Width;
double m_Height;
};
int main()
{
CBox myBox(1, 2, 3);
cout << myBox.m_Height << endl;
}
#include <iostream>
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 << endl << "CBox constructor called";
}
~CBox() { cout << "CBox destructor called" << endl;
}
protected:
double m_Length;
double m_Width;
double m_Height;
friend int main();
};
class CCandyBox: public CBox
{
public:
char* m_Contents;
CCandyBox(double lv, double wv, double hv, char* str = "Candy")
:CBox(lv, wv, hv)
{
// cout << endl << "CCandyBox constructor called";
m_Contents = new char [ strlen(str) + 1 ];
strcpy_s(m_Contents, strlen(str) + 1, str);
}
~CCandyBox()
{
cout << "CCandyBox destructor called" << endl;
}
};
int main()
{
CCandyBox myToffeeBox(2, 3, 4, "Stickjaw Toffee");
CCandyBox* p2 = &myToffeeBox;
CBox* p1 = dynamic_cast<CBox*>(p2);
cout << p1->m_Length << endl;
}