科目名稱:程式設計 | 開課系所:資訊工程 學系 | 任課教師 |
吳坤熹 |
||
系所別: |
年級: |
學號: |
姓名: |
考試日期 |
2008.4.24 |
(考試時間: 8:10-8:30)
Open book; turn off computer & mobile phone
// Header file CandyBox.h in project Ex9_01
#pragma once
#include "Box.h"
class CCandyBox: CBox
{
public:
char* m_Contents;
CCandyBox(char* str = "Candy") // Constructor
{
m_Contents = new char[ strlen(str) + 1 ];
strcpy_s(m_Contents, strlen(m_Contents), str);
}
~CCandyBox() // Destructor
{ delete[] m_Contents; };
};
#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;
};
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 myCandyBox(1, 2, 3);
CCandyBox myToffeeBox(2, 3, 4, "Stickjaw Toffee");
cout << endl;
}