國立暨南國際大學 101 學年度第二學期小考試卷
(考試時間: 08:10-08:20)
科目名稱:程式設計 |
開課系所:資訊工程
學系 |
任課教師
|
吳坤熹
|
系所別:
|
年級:
|
學號:
|
姓名:
|
考試日期
|
2013.4.26
|
- (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).
// Static data members
#include <iostream>
using std::cout;
using std::endl;
class CNode
{
public:
int value;
CNode* previous;
static CNode* pLastNode;
CNode(int v) :
value(v), previous(pLastNode)
{ pLastNode = this; }
};
CNode* CNode::pLastNode = NULL;
int main()
{
CNode a(3), b(5), c(7);
CNode* p;
cout << (c.previous)->value << endl;
for (p=&c; p!=NULL; p=p->previous)
cout << p->value;
cout << endl;
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).
// Virtual functions
#include <iostream>
using std::cout;
using std::endl;
class CBase // P.639 Ex.3
{
protected:
int m_anInt;
public:
CBase(int n): m_anInt(n) { cout << "Base constructor\n"; }
virtual void Print() const = 0;
};
class CDerived : protected CBase
{
public:
CDerived(int n = 0): CBase(n)
{ m_anInt=n; cout << "Derived constructor\n"; }
virtual void Print() const { cout << m_anInt << '\n'; }
};
int main()
{
CDerived obj(426);
obj.Print();
return 0;
}