科目名稱:程式設計 | 開課系所:資訊工程學系 | 任課教師 |
吳坤熹 |
||
系所別: |
年級: |
學號: |
姓名: |
考試日期 |
2011.3.25 |
(考試時間: 10:30-10:45)
(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).
// Ex9_01.cpp
// Using a derived class
#include <iostream> // For stream I/O
#include <cstring> // For strlen() and strcpy()
class CBox
{
public:
double m_Length;
double m_Width;
double m_Height;
CBox(double lv = 1.0, double wv = 1.0, double hv = 1.0):
m_Length(lv), m_Width(wv), m_Height(hv){}
};
class CCandyBox: CBox
{ public:
char m_Contents[10];
CCandyBox(char* str = "Candy") // Constructor
{
strcpy_s(m_Contents, 10, str);
}
};
using std::cout;
using std::endl;
int main()
{
CBox myBox(4.0, 3.0, 2.0); // Create CBox object
CCandyBox myCandyBox;
CCandyBox myMintBox("Wafer"); // Create CCandyBox object
cout << endl
<< "myBox occupies " << sizeof myBox // Show how much memory
<< " bytes" << endl // the objects require
<< "myCandyBox occupies " << sizeof myCandyBox
<< " bytes" << endl
<< "myMintBox occupies " << sizeof myMintBox
<< " bytes";
cout << endl
<< "myBox length is " << myBox.m_Length;
myBox.m_Length = 10.0;
cout << endl;
return 0;
}
(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).// Copy Constructor
#include <iostream>
using std::cout;
class CBox
{
public:
int volume;
char* color;
CBox(int v, char* c = "Violet"): volume(v)
{
color = new char[strlen(c)];
strcpy(color, c);
}
};
int main()
{
CBox cigar(10);
CBox chocolate(cigar);
cout << chocolate.color << "\n";
}