科目名稱:程式設計 | 開課系所:資訊工程 學系 | 考試日期 | 2015.6.17 | ||
系所別: |
年級: |
學號: |
姓名: |
考試時間 | 8:30-10:30 |
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 |
// Destructor (P.328)
#include <iostream>
class CData
{
private:
int value;
public:
CData(int n): value(n) {}
~CData()
{
std::cout << "Destructor called for object " << value << std::endl;
}
};
int main()
{
CData a(6);
CData* pb(new CData(17));
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).
// Assignment Operator (P.340)
#include <iostream>
class CData
{
private:
int value;
public:
CData(int n=0): value(n) {};
CData& operator=(CData& a)
{
value = a.value--;
return *this;
}
void Print()
{
std::cout << value << std::endl;
}
};
int main()
{
CData a;
CData b(5), c(7);
(a = b) = c;
a.Print(); b.Print(); c.Print();
return 0;
}
(10%) If we replace the OnDraw() function in P.671 as below, what result will be shown on the screen after you run the program? Please specify the
coordinates of endpoints of each segment, or the coordinates of centers and boundaries.
void CSketcherView::OnDraw(CDC* pDC)
{
CSketcherDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
for (int i=0; i<=10; i++)
pDC->Rectangle(300+i*10, 300+i*10, 200-i*10, 200-i*10);
}
void CEx16View::OnBnClickedMybutton()
{
CString greeting[] = { CString("Good morning"), CString("Good afternoon"), CString("Good evening") };
CString name[] = { CString("Alice"), CString("Bob"), CString("Charlie") };
static int i = 0;
int j = 2;
i = (i + 1) % 3;
j = (j + 1) % 3;
GetDlgItem(IDC_MyButton)->SetWindowTextW(greeting[i] + ", " + name[j]);
}